设为首页 收藏本站
查看: 1067|回复: 0

[经验分享] ansible的条件判断、迭代执行、tags

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-3-3 08:45:02 | 显示全部楼层 |阅读模式
在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
[iyunv@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

[iyunv@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"
     
[iyunv@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   

[iyunv@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

[iyunv@node1 ansible]#



示例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
[iyunv@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

[iyunv@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')
     
[iyunv@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   

[iyunv@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

[iyunv@node1 ansible]#



保存结果几乎所有的模块都是会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
[iyunv@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

[iyunv@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
      
[iyunv@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   

[iyunv@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

[iyunv@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

[iyunv@node1 ansible]#



ansible 中的循环操作:
当有需要重复性执行的任务时,可以使用迭代机制,格式为:将需要迭代的内容定义为Item变量引用,并通过with_items 语句来指明迭代的元素列表即可
示例1、
在 testservers 组中一台主机上安装lamp
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@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")
[iyunv@node1 ansible]#



上面的语句等同于:
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
[iyunv@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、
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@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命令。
如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[iyunv@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命令。
如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[iyunv@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文件、启动服务、如果配置有变化就重启服务,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@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
[iyunv@node1 ansible]#



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

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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-185579-1-1.html 上篇帖子: ansible模块yum、services、setup 下篇帖子: ansible的roles介绍和实战
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表