1. ansible-playbook添加判斷
when相當於shell腳本里的if 判斷,when語句就是用來實現這個功能的,它是一個jinja2的語法,但是不需要雙大括號,用法很簡單
1.1) 示例1:
1 [root@test-1 when]# vim when_test1.yaml 2 [root@test-1 when]# cat when_test1.yaml 3 --- 4 - hosts: web1 5 gather_facts: yes 6 7 tasks: 8 - name: "IP if" 9 debug: msg={{ansible_default_ipv4.address}} 10 when: ansible_default_ipv4.address == '192.168.200.133' 11 12 # 注意 Ansible facts和vars 比如 ansible_os_family 應能被引用 13 # 直接寫,不帶雙大括號。
1.2) 示例2:使用括號對條件進行分組
1 [root@test-1 when]# vim when_test2.yaml 2 [root@test-1 when]# cat when_test2.yaml 3 --- 4 - hosts: localhost 5 gather_facts: yes 6 7 tasks: 8 - name: "shut down CentOS 6 and Debian 7 systems" 9 command: ls -a 10 when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "6") or 11 (ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "7")
1.3) 示例3:所有需要為true的多條件判讀(邏輯“and”) 也可以指定為列表
1 [root@test-1 when]# vim when_test3.yaml 2 [root@test-1 when]# cat when_test3.yaml 3 --- 4 - hosts: localhost 5 gather_facts: yes 6 7 tasks: 8 - name: "shut down CentOS 7 systems" 9 command: ls -a 10 when: 11 - ansible_facts['distribution'] == "CentOS" 12 - ansible_facts['distribution_major_version'] == "7"
2. 案例:
2.1) ansible-playbook進行when判斷是centos還是Ubuntu系統后在安裝http
1 [root@test-1 when]# vim when_mc.yaml 2 [root@test-1 when]# cat when_mc.yaml 3 --- 4 - hosts: localhost 5 gather_facts: yes 6 7 tasks: 8 - name: "update apache version - yum" 9 yum: name=httpd state=present 10 when: 11 - ansible_pkg_mgr == 'yum' 12 notify: restart httpd 13 14 - name: "Update apache version - apt" 15 apt: name=apache2 state=present update_cache=yes 16 when: 17 - ansible_pkg_mgr == 'apt' 18 notify: restart apache2 19 20 21 handlers: 22 - name: restart apache2 23 service: name=apache2 state=restarted 24 - name: restart httpd 25 service: name=httpd state=restarted
2.2) 配置文件檢查
1 [root@test-1 when]# ansible-playbook --syntax-check when_mc.yaml 2 3 playbook: when_mc.yaml
2.3) 執行遠程安裝腳本
1 [root@test-1 when]# ansible-playbook when_mc.yaml 2 3 PLAY [localhost] ******************************************************************************************************************************* 4 5 TASK [Gathering Facts] ************************************************************************************************************************* 6 ok: [192.168.200.131] 7 8 TASK [update apache version - yum] ************************************************************************************************************* 9 changed: [192.168.200.131] 10 11 TASK [Update apache version - apt] ************************************************************************************************************* 12 skipping: [192.168.200.131] 13 14 RUNNING HANDLER [restart httpd] **************************************************************************************************************** 15 changed: [192.168.200.131] 16 17 PLAY RECAP ************************************************************************************************************************************* 18 192.168.200.131 : ok=3 changed=2 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0