ansible的條件判斷、迭代執行、tags

在ansible中支持條件判斷,這使我們操作更加靈活

使用when進行條件測試

示例1:

將 testservers 組中的其中一臺主機上的 httpd 服務卸載掉,另外主機不卸載

[root@node1 ansible]# ansible testservers -m shell -a 'rpm -q httpd'
192.168.100.131 | success | rc=0 >>
httpd-2.2.15-29.el6.centos.x86_64

192.168.100.132 | success | rc=0 >>
httpd-2.2.15-47.el6.centos.3.x86_64

[root@node1 ansible]# cat http.yml 
- hosts: testservers
  remote_user: root
  tasks:
  - name: uninstall httpd service
    yum: name=httpd state=absent
    when: ansible_nodename == "v2.lansgg.com"
    
[root@node1 ansible]# ansible-playbook http.yml 

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [uninstall httpd service] *********************************************** 
skipping: [192.168.100.132]
changed: [192.168.100.131]

PLAY RECAP ******************************************************************** 
192.168.100.131            : ok=2    changed=1    unreachable=0    failed=0   
192.168.100.132            : ok=1    changed=0    unreachable=0    failed=0   

[root@node1 ansible]# ansible testservers -m shell -a 'rpm -q httpd'
192.168.100.132 | success | rc=0 >>
httpd-2.2.15-47.el6.centos.3.x86_64

192.168.100.131 | FAILED | rc=1 >>
package httpd is not installed

[root@node1 ansible]#

示例2:

也可以使用 or 進行 或 判斷,這裏將組內的兩臺主機的 httpd 服務都卸載,

[root@node1 ansible]# ansible testservers -m shell -a 'rpm -q httpd'
192.168.100.132 | success | rc=0 >>
httpd-2.2.15-47.el6.centos.3.x86_64

192.168.100.131 | success | rc=0 >>
httpd-2.2.15-29.el6.centos.x86_64

[root@node1 ansible]# cat http.yml 
- hosts: testservers
  remote_user: root
  tasks:
  - name: uninstall httpd service
    yum: name=httpd state=absent
    when: (ansible_nodename == "v2.lansgg.com") or ( ansible_all_ipv4_addresses[0] == '192.168.100.132')
    
[root@node1 ansible]# ansible-playbook http.yml 

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [uninstall httpd service] *********************************************** 
changed: [192.168.100.131]
changed: [192.168.100.132]

PLAY RECAP ******************************************************************** 
192.168.100.131            : ok=2    changed=1    unreachable=0    failed=0   
192.168.100.132            : ok=2    changed=1    unreachable=0    failed=0   

[root@node1 ansible]# ansible testservers -m shell -a 'rpm -q httpd'
192.168.100.131 | FAILED | rc=1 >>
package httpd is not installed

192.168.100.132 | FAILED | rc=1 >>
package httpd is not installed

[root@node1 ansible]#

保存結果

幾乎所有的模塊都是會outputs一些東西,甚至debug模塊也會.大多數我們會使用的結果變量是changed.這個changed變量決定 了是否要直接handlers和輸出的顏色是什麼.然而,結果變量還有其他的用途,譬如我需要保存我的結果變量,然後在我們的playbook的其他地方使用.

示例:

分別在一臺主機上創建一個目錄,另外主機不存在此目錄,根據此目錄的存在情況,做出不同操作

[root@node1 ansible]# ansible testservers -m shell -a 'ls -l /nono'
192.168.100.131 | success | rc=0 >>
total 0

192.168.100.132 | FAILED | rc=2 >>
ls: cannot access /nono: No such file or directory

[root@node1 ansible]# cat re.yml 
- hosts: testservers
  remote_user: root
  tasks:
   - name: ls /nono
     shell: /bin/ls /nono
     register: result
     ignore_errors: True
   - name: test result
     copy: content="ok" dest=/tmp/test
     when: result.rc == 0
   - name: test no result
     copy: content="no ok" dest=/tmp/test
     when: result.rc != 0
     
[root@node1 ansible]# ansible-playbook re.yml 

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [ls /nono] ************************************************************** 
changed: [192.168.100.131]
failed: [192.168.100.132] => {"changed": true, "cmd": "/bin/ls /nono", "delta": "0:00:00.004437", "end": "2016-03-02 12:56:55.409736", "rc": 2, "start": "2016-03-02 12:56:55.405299", "warnings": []}
stderr: /bin/ls: cannot access /nono: No such file or directory
...ignoring

TASK: [test result] *********************************************************** 
skipping: [192.168.100.132]
ok: [192.168.100.131]

TASK: [test no result] ******************************************************** 
skipping: [192.168.100.131]
ok: [192.168.100.132]

PLAY RECAP ******************************************************************** 
192.168.100.131            : ok=3    changed=1    unreachable=0    failed=0   
192.168.100.132            : ok=3    changed=1    unreachable=0    failed=0   

[root@node1 ansible]# ansible testservers -m shell -a 'ls -l /tmp/test'
192.168.100.131 | success | rc=0 >>
-rw-r--r-- 1 root root 2 Mar  2 12:55 /tmp/test

192.168.100.132 | success | rc=0 >>
-rw-r--r-- 1 root root 5 Mar  2 12:55 /tmp/test

[root@node1 ansible]# ansible testservers -m shell -a 'cat /tmp/test'
192.168.100.131 | success | rc=0 >>
ok

192.168.100.132 | success | rc=0 >>
no ok

[root@node1 ansible]#

ansible 中的循環操作:

當有需要重複性執行的任務時,可以使用迭代機制,格式爲:將需要迭代的內容定義爲Item變量引用,並通過with_items 語句來指明迭代的元素列表即可

示例1、

在 testservers 組中一臺主機上安裝lamp

[root@node1 ansible]# cat item.yml 
- hosts: testservers
  remote_user: root
  tasks:
   - name: yum install lamp
     yum: name=`item` state=present
     with_items:
         - httpd
         - mysql-server
         - php
     when: (ansible_nodename == "v2.lansgg.com")
[root@node1 ansible]#

上面的語句等同於:

- hosts: testservers
  remote_user: root
  tasks:
  - name: install httpd
    yum: name=httpd state=present
    when: (ansible_nodename == "v2.lansgg.com")
  - name: install mysql-server
    yum: name=mysql-server state=present
    when: (ansible_nodename == "v2.lansgg.com")
  - name: install php
    yum: name=php state=present
    when: (ansible_nodename == "v2.lansgg.com")

上面的語句 hash 方式編寫也可以,等同於如下:

- hosts: testservers
  remote_user: root
  tasks:
   - name: install httpd
     yum: name=`item`.`name` state=`item`.`state`
     when: (ansible_nodename == "v2.lansgg.com")
     with_items:
      - {name: 'httpd', state: 'present'}
      - {name: 'mysql-server', state: 'present'}
      - {name: 'php', state: 'present'}

開始執行:

[root@node1 ansible]# ansible-playbook item.yml 

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [yum install lamp] ****************************************************** 
skipping: [192.168.100.132]
changed: [192.168.100.131] => (item=httpd,mysql-server,php)

PLAY RECAP ******************************************************************** 
192.168.100.131            : ok=2    changed=1    unreachable=0    failed=0   
192.168.100.132            : ok=1    changed=0    unreachable=0    failed=0

ansible 的 tags 功能

ansible的playbool中有一個關鍵字,叫做tags。tags是什麼?就是打標籤。tags可以和一個play(就是很多個task)或者一 個task進行捆綁。然後,ansible-playbook提供了“--skip-tags”和“--tags” 來指明是跳過特定的tags還是執行特定的tags。

示例 1、

[root@node1 ansible]# cat tag.yml
- hosts: testservers
  remote_user: root
  tasks:
   - name: echo A1
     command: echo A1
     tags:
      - A1
   - name: echo A2
     command: echo A2
     tags:
      - A2
   - name: echo A3
     command: echo A3
     tags:
      - A3

當執行  ansible-playbook tag.yml --tags="A1,A3  ,則只會執行A1和A3的echo命令。

如下:

[root@node1 ansible]# ansible-playbook tag.yml --tags="A1,A3"

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [echo A1] *************************************************************** 
changed: [192.168.100.131]
changed: [192.168.100.132]

TASK: [echo A3] *************************************************************** 
changed: [192.168.100.131]
changed: [192.168.100.132]

PLAY RECAP ******************************************************************** 
192.168.100.131            : ok=3    changed=2    unreachable=0    failed=0   
192.168.100.132            : ok=3    changed=2    unreachable=0    failed=0

當執行  ansible-playbook tag.yml --skip-tags="A2" ,同樣只會執行 A1和A3的echo命令。

如下:

[root@node1 ansible]# ansible-playbook tag.yml --skip-tags="A2"

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [echo A1] *************************************************************** 
changed: [192.168.100.132]
changed: [192.168.100.131]

TASK: [echo A3] *************************************************************** 
changed: [192.168.100.131]
changed: [192.168.100.132]

PLAY RECAP ******************************************************************** 
192.168.100.131            : ok=3    changed=2    unreachable=0    failed=0   
192.168.100.132            : ok=3    changed=2    unreachable=0    failed=0

示例 2 、

我們安裝 httpd 服務的流程爲 安裝服務、copy文件、啓動服務、如果配置有變化就重啓服務,

[root@node1 ansible]# cat http.yml 
- hosts: testservers
  remote_user: root
  tasks:
  - name: install httpd package
    yum: name=httpd state=present
  - name: start httpd service
    service: name=httpd enabled=true state=started
  - name: copy config file
    copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify:
       restart httpd service
    tags:
     - modify
  handlers:
   - name: restart httpd service
     service: name=httpd state=restarted
[root@node1 ansible]#

當我們的配置文件有修改的時候,可以指定tags,這樣就會只執行copy config file 任務,前面就不會再執行

結果如下:


[root@node1 ansible]# ansible-playbook http.yml --tags=modify

PLAY [testservers] ************************************************************ 

GATHERING FACTS *************************************************************** 
ok: [192.168.100.131]
ok: [192.168.100.132]

TASK: [copy config file] ****************************************************** 
changed: [192.168.100.131]
changed: [192.168.100.132]

NOTIFIED: [restart httpd service] ********************************************* 
changed: [192.168.100.131]
changed: [192.168.100.132]

PLAY RECAP ******************************************************************** 
192.168.100.131            : ok=3    changed=2    unreachable=0    failed=0   
192.168.100.132            : ok=3    changed=2    unreachable=0    failed=0

特殊 tags

系統中內置的特殊tags:

  always、tagged、untagged、all 是四個系統內置的tag,有自己的特殊意義

  always: 指定這個tag 後,task任務將永遠被執行,而不用去考慮是否使用了--skip-tags標記

  tagged: 當 --tags 指定爲它時,則只要有tags標記的task都將被執行,--skip-tags效果相反

  untagged: 當 --tags 指定爲它時,則所有沒有tag標記的task 將被執行,--skip-tags效果相反

  all: 這個標記無需指定,ansible-playbook 默認執行的時候就是這個標記.所有task都被執行


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章