ansible-playbook-logic逻辑实现
1.when
根据某一判断条件(变量、执行结果等)来实现逻辑
查看whwen_ansible.yml
[root@localhost logic]# cat whwen_ansible.yml --- - hosts: webservers remote_user: root tasks: - name: test debug: msg: "测试" when: ansible_os_family == "Debian"
执行whwen_ansible.yml
[root@localhost logic]# ansible-playbook -C whwen_ansible.yml PLAY [webservers] ******************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************** ok: [43.143.98.52] ok: [192.168.235.152] TASK [test] ******************************************************************************************************** skipping: [43.143.98.52] skipping: [192.168.235.152] PLAY RECAP ******************************************************************************************************** 192.168.235.152 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 43.143.98.52 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
修改whwen_ansible.yml中when: ansible_os_family == "RedHat",再次查看whwen_ansible.yml
[root@localhost logic]# cat whwen_ansible.yml --- - hosts: webservers remote_user: root tasks: - name: test debug: msg: "测试" when: ansible_os_family == "RedHat"
执行whwen_ansible.yml
[root@localhost logic]# ansible-playbook -C whwen_ansible.yml PLAY [webservers] ************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************* ok: [43.143.98.52] ok: [192.168.235.152] TASK [test] ******************************************************************************************************** ok: [192.168.235.152] => { "msg": "测试" } ok: [43.143.98.52] => { "msg": "测试" } PLAY RECAP ******************************************************************************************************** 192.168.235.152 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 43.143.98.52 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible_os_family
是一个facts的一个变量。要查看ansible_os_family
变量的值,你可以使用ansible all -m setup | grep ansible_os_family
命令。这个命令将在所有主机上运行setup
模块,并从输出中过滤出ansible_os_family
的值。或者是
ansible all -m setup -a 'filter=ansible_os_family'