千家信息网

六、playbook循环、playbook中的条件判断、pl

发表于:2025-02-05 作者:千家信息网编辑
千家信息网最后更新 2025年02月05日,一、playbook循环# vi /etc/ansible/while.yml //加入如下内容---- hosts: yw02 user: root tasks: - name: chan
千家信息网最后更新 2025年02月05日六、playbook循环、playbook中的条件判断、pl

一、playbook循环

# vi /etc/ansible/while.yml //加入如下内容---- hosts: yw02  user: root  tasks:    - name: change mode for files      file: path=/tmp/{{ item }} mode=600      with_items:        - 1.txt        - 2.txt        - 3.txt

说明: 这里用到了一个file模块,后面是路径,如果是一个文件,可以写/tmp/1.txt,多个文件,可以用一个循环,with_items为循环的对象

执行:ansible-playbook while.yml

报错:对方机器没有这三个文件,在mode前增加state=touch,创建一个。

再执行:ansible-playbook while.yml

执行成功,先创建,再定义它的权限。


二、playbook中的条件判断

# vi /etc/ansible/when.yml //加入如下内容---- hosts: testhost  user: root  gather_facts: True  tasks:    - name: use when      shell: touch /tmp/when.txt      when: ansible_ens33.ipv4.address == "192.168.149.131"

说明:

这里hosts写testhost,写一台机器就没有意义了。

这里收集了facts,这行也可以删除,默认就是收集,接下来要用到它了。

ansible yw02 -m setup 可以查看到所有的facter信息。

when就是一个条件判断,当这个条件成立的时候,才会执行这个shell。

从facts收集的信息里找出ansible_ens33下的ipv4的address是否为该IP,条件成立,则执行shell,如果没有这个条件,则会直接执行。

当有分级时,每一级下面的要打点,没有分级就直接写等号了。

when不仅仅是针对facts,还可以针对其他情况,比如判断文件、目录是否存在。

"ansible_facts":            //最总的一级"ansible_all_ipv4_addresses":[  //判断条件从这一级开始,这里是一个数组,把所有的ipv4列出来,但是它下面还有ipv6有两个,要判断的话得需判断两个,所以这不是想要的。            "192.168.98.134",             "192.168.149.132"        ], ..."ansible_ens33": {            "active": true,             "device": "ens33", ... "hw_timestamp_filters": [],             "ipv4": {                "address": "192.168.149.132",                 "broadcast": "192.168.149.255",                 "netmask": "255.255.255.0",                 "network": "192.168.149.0"            }, ...
[root@fuxi01 ansible]# ansible-playbook when.ymlPLAY [testhost] **********************************************************************************************************************TASK [Gathering Facts] ***************************************************************************************************************fatal: [yw02]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host yw02 port 22: No route to host", "unreachable": true}ok: [yw03]ok: [127.0.0.1]TASK [use when] **********************************************************************************************************************skipping: [127.0.0.1] [WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to use command because file isinsufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of thismessage.changed: [yw03]PLAY RECAP ***************************************************************************************************************************127.0.0.1                  : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   yw02                       : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0   yw03                       : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


三、playbook中的handlers

执行task之后,服务器发生变化之后要执行的一些操作,比如我们修改了配置文件后,需要重启一下服务。

# vi /etc/ansible/handlers.yml//加入如下内容---- name: handlers test  hosts: yw02  user: root  tasks:    - name: copy file      copy: src=/etc/passwd dest=/tmp/aaa.txt      notify: test handlers  handlers:    - name: test handlers      shell: echo "111111" >> /tmp/aaa.txt

说明:只有copy模块真正执行成功后,才会去调用下面的handlers相关的操作。也就是说如果1.txt和2.txt内容是一样的,并不会去执行handlers里面的shell相关命令。 这种比较适合配置文件发生更改后,重启服务的操作。类似于command1 && command2(handlers),前面的命令执行成功后,才执行这个handler,需要用notify关联起来。

0