自动化运维、大数据、Docker

实例学习ansible系列(15)playbook的条件和循环

Ansible中有众多的模块,可以写playbook,同时里面也可以写条件判断和循环,这样基本上脚本能做的事情ansible大体都可以作了。条件判断使用when,循环使用with_items,接下来看一下如何使用的简单实例。

条件判断playbook实例

[root@host31 ~]# cat hello.playbook
- hosts: host31
  gather_facts: true
  tasks:
    - name:  say redhat hello task
      shell: echo "RedHat"  `date` by `hostname` >> /tmp/hello.log
      when:  ansible_os_family ==  "RedHat"
    - name:  say other linux hello task
      shell: echo "Not RedHat"  `date` by `hostname` >> /tmp/hello.log
      when:  ansible_os_family !=  "RedHat"
[root@host31 ~]#

事前确认

[root@host31 ~]# ll /tmp/hello.log
ls: cannot access /tmp/hello.log: No such file or directory
[root@host31 ~]#

执行playbook

[root@host31 ~]# ansible-playbook hello.playbook

PLAY [host31] ******************************************************************

TASK [setup] *******************************************************************
ok: [host31]

TASK [say redhat hello task] ***************************************************
changed: [host31]  -〉此task的条件判断when条件达成,所以changed

TASK [say other linux hello task] **********************************************
skipping: [host31] -〉此task的条件判断when条件未达成,所以skip了

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

[root@host31 ~]#

结果输出文件确认
[root@host31 ~]# cat /tmp/hello.log
RedHat Sun Jul 31 09:37:08 EDT 2016 by host31
[root@host31 ~]#

循环实例

[root@host31 ~]# cat hello.playbook
- hosts: host31
  gather_facts: true
  tasks:
    - name:  say redhat hello task
      shell: echo {{item}}  `date` by `hostname` >> /tmp/hello.log
      with_items:
        - message item1
        - message item2
        - message item3
        - message item4
        - message item5
[root@host31 ~]#

事前确认

[root@host31 ~]# ll /tmp/hello.log
ls: cannot access /tmp/hello.log: No such file or directory
[root@host31 ~]#

执行playbook

[root@host31 ~]# ansible-playbook hello.playbook

PLAY [host31] ******************************************************************

TASK [setup] *******************************************************************
ok: [host31]

TASK [say redhat hello task] ***************************************************
changed: [host31] => (item=message item1)
changed: [host31] => (item=message item2)
changed: [host31] => (item=message item3)
changed: [host31] => (item=message item4)
changed: [host31] => (item=message item5)

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

[root@host31 ~]#

结果输出文件确认
[root@host31 ~]# cat /tmp/hello.log
message item1 Sun Jul 31 09:50:10 EDT 2016 by host31
message item2 Sun Jul 31 09:50:10 EDT 2016 by host31
message item3 Sun Jul 31 09:50:10 EDT 2016 by host31
message item4 Sun Jul 31 09:50:11 EDT 2016 by host31
message item5 Sun Jul 31 09:50:11 EDT 2016 by host31
[root@host31 ~]#

本文转载自 实例学习ansible系列(15)playbook的条件和循环

赞(0) 打赏
蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明蜷缩的蜗牛 » 实例学习ansible系列(15)playbook的条件和循环
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏