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

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       
 

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 ~]#
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
 

事前确认

[root@host31 ~]# ll /tmp/hello.logls: cannot access /tmp/hello.log: No such file or directory[root@host31 ~]#
  
  
  • 1
  • 2
  • 3
 

执行playbook

[root@host31 ~]# ansible-playbook hello.playbookPLAY [host31] ******************************************************************TASK [setup] *******************************************************************ok: [host31]TASK [say redhat hello task] ***************************************************changed: [host31]  -〉此task的条件判断when条件达成,所以changedTASK [say other linux hello task] **********************************************skipping: [host31] -〉此task的条件判断when条件未达成,所以skip了PLAY RECAP *********************************************************************host31                     : ok=2    changed=1    unreachable=0    failed=0[root@host31 ~]#
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
 

结果输出文件确认
  [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 ~]#
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
 

事前确认

[root@host31 ~]# ll /tmp/hello.logls: cannot access /tmp/hello.log: No such file or directory[root@host31 ~]#
  
  
  • 1
  • 2
  • 3
 

执行playbook

[root@host31 ~]# ansible-playbook hello.playbookPLAY [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 ~]#
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
 

结果输出文件确认
  [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 ~]#

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值