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 directo