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

在ansible中支持條件判斷,這使咱們操做更加靈活php

使用when進行條件測試node

示例1:mysql

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

[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:shell

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

[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的其餘地方使用.bash

示例:ide

分別在一臺主機上建立一個目錄,另外主機不存在此目錄,根據此目錄的存在狀況,作出不一樣操做測試

[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 中的循環操做:spa

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

示例一、

在 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。

示例 一、

[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都被執行

相關文章
相關標籤/搜索