实例学习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 ~]#
   
   
  • 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 ~]#

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/qq_43679903/article/details/87293424