【Ansible】04

4 篇文章 0 订阅
4 篇文章 0 订阅
本文详细介绍了Ansible中如何使用任务块(block)组织任务,包括条件性安装和启动nginx,以及rescue和always的作用。此外,还展示了loop循环和角色(role)的使用,以实现playbook的复用和配置管理的灵活性。
摘要由CSDN通过智能技术生成

【Ansible】03

任务块

block任务块

  • 使用 block 可以将多个任务合并为一个组
  • 可以将整个 block任务组 , 一起控制是否要执行
# 如果webservers组中的主机系统发行版是Rocky,则安装并启动nginx
[root@pubserver ansible]# vim block1.yml
---
- name: block tasks
  hosts: webservers
  tasks:
    - name: define a group of tasks
      block:
        - name: install nginx   # 通过yum安装nginx
          yum:
            name: nginx
            state: present

        - name: start nginx     # 通过service启动nginx服务
          service:
            name: nginx
            state: started
            enabled: yes
      when: ansible_distribution=="Rocky"   # 条件为真才会执行上面的任务

[root@pubserver ansible]# ansible-playbook block1.yml

rescue 和 always

  • block 组成任务组
  • resuce 判定任务组中是否有失败组 ( failed ) , 失败则执行 resuce 中的任务
  • always 不与任务组的成功与否关联 , 总是执行任务
[root@pubserver ansible]# vim block2.yml
---
- name: block test
  hosts: webservers
  tasks:
    - name: block / rescue / always test1
      block:
        - name: touch a file
          file:
            path: /tmp/test1.txt
            state: touch
            
      rescue:
        - name: touch file test2.txt
          file:
            path: /tmp/test2.txt
            state: touch

      always:
        - name: touch file test3.txt
          file:
            path: /tmp/test3.txt
            state: touch


# 执行playbook web1上将会出现/tmp/test1.txt和/tmp/test3.txt
[root@pubserver ansible]# ansible-playbook block2.yml
[root@web1 ~]# ls /tmp/test*.txt
/tmp/test1.txt  /tmp/test3.txt


# 修改上面的playbook,使block中的任务出错
[root@web1 ~]# rm -f /tmp/test*.txt
[root@pubserver ansible]# vim block2.yml
---
- name: block test
  hosts: webservers
  tasks:
    - name: block / rescue / always test1
      block:
        - name: touch a file
          file:
            path: /tmp/abcd/test11.txt
            state: touch

      rescue:
        - name: touch file test22.txt
          file:
            path: /tmp/test22.txt
            state: touch

      always:
        - name: touch file test33.txt
          file:
            path: /tmp/test33.txt
            state: touch

# 因为web1上没有/tmp/abcd目录,所以block中的任务失败。但是playbook不再崩溃,而是执行rescue中的任务。always中的任务总是执行
[root@pubserver ansible]# ansible-playbook block2.yml
[root@web1 ~]# ls /tmp/test*.txt
/tmp/test22.txt  /tmp/test33.txt

loop 循环

  • 相当于 shell 中的 for循环
  • ansible 中循环用到的变名名称是固定的 , item
# 在test组中的主机上创建5个目录/tmp/{aaa,bbb,ccc,ddd,eee}
[root@pubserver ansible]# vim loop1.yml
---
- name: use loop
  hosts: webservers
  tasks:
    - name: create directory
      file:
        path: /tmp/{{item}}
        state: directory
      loop: [aaa,bbb,ccc,ddd,eee]

# 上面写法,也可以改为:
---
- name: use loop
  hosts: webservers
  tasks:
    - name: create directory
      file:
        path: /tmp/{{item}}
        state: directory
      loop: 
        - aaa
        - bbb
        - ccc
        - ddd
        - eee

[root@pubserver ansible]# ansible-playbook loop1.yml


# 使用复杂变量。创建zhangsan用户,密码是123;创建lisi用户,密码是456
# item是固定的,用于表示循环中的变量
# 循环时,loop中每个-后面的内容作为一个整体赋值给item。
# loop中{}中的内容是自己定义的,写法为key:val
# 取值时使用句点表示。如下例中取出用户名就是{{item.uname}}
[root@pubserver ansible]# vim loop_user.yml
---
- name: create users
  hosts: webservers
  tasks:
    - name: create multiple users
      user:
        name: "{{item.uname}}"
        password: "{{item.upass|password_hash('sha512')}}"
      loop:
        - {"uname": "zhangsan", "upass": "123"}
        - {"uname": "lisi", "upass": "456"}

[root@pubserver ansible]# ansible-playbook  loop_user.yml

role 角色

  • role角色 , 可以实现playbook重复使用
  • 角色role相当于把任务打散 , 放到不同的目录中
  • 再把一些固定的值 , 如用户名 , 软件包 , 服务等 , 用变量来表示
  • role 角色定义好之后, 可以在其他playbook中直接调用
1. 书写不同内容到对应目录的 main.yml
1) 创建motd模板文件 [ roles/motd/templates/motd ]
[root@pubserver ansible]# vim roles/motd/templates/motd
Hostname: {{ansible_hostname}}
Date: {{ansible_date_time.date}}
Contact to: {{admin}}
2) 创建变量 [ roles/motd/vars/main.yml ]
[root@pubserver ansible]# vim roles/motd/vars/main.yml  # 追加一行
admin: zzg@tedu.cn
3) 创建任务 [ roles/motd/tasks/main.yml ]
[root@pubserver ansible]# vim roles/motd/tasks/main.yml  # 追加
- name: modify motd
  template:
    src: motd      # 这里的文件,自动到templates目录下查找
    dest: /etc/motd
2. 创建 playbook , 调用motd角色
[root@pubserver ansible]# vim role_motd.yml
---
- name: modify motd with role
  hosts: webservers
  roles:
    - motd
3. 执行playbook
[root@pubserver ansible]# ansible-playbook role_motd.yml 
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值