32去2 发表于 2016-3-3 08:45:02

ansible的条件判断、迭代执行、tags

在ansible中支持条件判断,这使我们操作更加灵活
使用when进行条件测试
示例1:
将 testservers 组中的其中一台主机上的 httpd 服务卸载掉,另外主机不卸载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

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

PLAY ************************************************************

GATHERING FACTS ***************************************************************
ok:
ok:

TASK: ***********************************************
skipping:
changed:

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

#




示例2:
也可以使用 or 进行 或 判断,这里将组内的两台主机的 httpd 服务都卸载,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

# 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 == '192.168.100.132')
   
# ansible-playbook http.yml

PLAY ************************************************************

GATHERING FACTS ***************************************************************
ok:
ok:

TASK: ***********************************************
changed:
changed:

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   

# 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

#




保存结果几乎所有的模块都是会outputs一些东西,甚至debug模块也会.大多数我们会使用的结果变量是changed.这个changed变量决定了是否要直接handlers和输出的颜色是什么.然而,结果变量还有其他的用途,譬如我需要保存我的结果变量,然后在我们的playbook的其他地方使用.
示例:
分别在一台主机上创建一个目录,另外主机不存在此目录,根据此目录的存在情况,做出不同操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

# 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
      
# ansible-playbook re.yml

PLAY ************************************************************

GATHERING FACTS ***************************************************************
ok:
ok:

TASK: **************************************************************
changed:
failed: => {"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: ***********************************************************
skipping:
ok:

TASK: ********************************************************
skipping:
ok:

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   

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

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

# 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

#




ansible 中的循环操作:
当有需要重复性执行的任务时,可以使用迭代机制,格式为:将需要迭代的内容定义为Item变量引用,并通过with_items 语句来指明迭代的元素列表即可
示例1、
在 testservers 组中一台主机上安装lamp

1
2
3
4
5
6
7
8
9
10
11
12
# 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")
#




上面的语句等同于:

1
2
3
4
5
6
7
8
9
10
11
12
- 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 方式编写也可以,等同于如下:

1
2
3
4
5
6
7
8
9
10
- 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'}




开始执行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# ansible-playbook item.yml

PLAY ************************************************************

GATHERING FACTS ***************************************************************
ok:
ok:

TASK: ******************************************************
skipping:
changed: => (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、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 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命令。
如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# ansible-playbook tag.yml --tags="A1,A3"

PLAY ************************************************************

GATHERING FACTS ***************************************************************
ok:
ok:

TASK: ***************************************************************
changed:
changed:

TASK: ***************************************************************
changed:
changed:

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命令。
如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# ansible-playbook tag.yml --skip-tags="A2"

PLAY ************************************************************

GATHERING FACTS ***************************************************************
ok:
ok:

TASK: ***************************************************************
changed:
changed:

TASK: ***************************************************************
changed:
changed:

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文件、启动服务、如果配置有变化就重启服务,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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
#




当我们的配置文件有修改的时候,可以指定tags,这样就会只执行copy config file 任务,前面就不会再执行
结果如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# ansible-playbook http.yml --tags=modify

PLAY ************************************************************

GATHERING FACTS ***************************************************************
ok:
ok:

TASK: ******************************************************
changed:
changed:

NOTIFIED: *********************************************
changed:
changed:

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



页: [1]
查看完整版本: ansible的条件判断、迭代执行、tags