分享一下我老师大神的人工智能教程!零基础,通俗易懂!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 ~]#