#when 语句
(jiankong_venv) [yy1s@rbtnode1 playbook]$ cat f6.yml ---- hosts : 144.131.252.144,144.131.252.142 remote_user : root vars :
touch_file : imoocc.file tasks :
- name : touch flag file
command : \"touch /tmp/this_is_{{ansible_distribution}}_system\"
when: (ansible_distribution == \"CentOS\" and ansible_distribution_major_version == \"6\") or (ansible_distribution == \"CentOS\" and ansible_distribution_major_version == \"7\")
(jiankong_venv) [yy1s@rbtnode1 playbook]$ ansible-playbook ./f4.yml
http://www.imooc.com/article/22753 循环语句可以参考
#with_items循环
(jiankong_venv) [yy1s@rbtnode1 playbook]$ cat f8.yml ---- hosts : 144.131.252.162remote_user : roottasks:
- name: add several users
user: name={{ item.name }} state=present groups={{ item.groups }}with_items:
- { name: 'testuser1', groups: 'yy1s' }- { name: 'testuser2', groups: 'yy1s' }
(jiankong_venv) [yy1s@rbtnode1 playbook]$ ansible-playbook ./f8.yml
#字典循环
(jiankong_venv) [yy1s@rbtnode1 playbook]$ cat f9.yml ---- hosts : 144.131.252.162remote_user : roottasks:
- name: add several users
user: name={{ item.key }} state=present groups={{ item.value }}with_dict:
{ 'testuser3' : 'wheel', 'testuser4' : 'root' }
(jiankong_venv) [yy1s@rbtnode1 playbook]$ ansible-playbook ./f9.yml
#⽂件copy
(jiankong_venv) [yy1s@rbtnode1 playbook]$ cat f10.yml
---- hosts : 144.131.252.162remote_user : roottasks:
- file: dest=/tmp/bb state=directory
- copy: src={{item}} dest=/tmp/bb owner=root mode=600with_fileglob:- test/*
(jiankong_venv) [yy1s@rbtnode1 playbook]$ ansible-playbook ./f10.yml
#条件结合循环语句
(jiankong_venv) [yy1s@rbtnode1 playbook]$ cat f7.yml ---- hosts : 144.131.252.162remote_user : roottasks:
- debug: msg=\"{{item.key}} is the winner\"
with_dict: {'jeson':{'english':60, 'chinese':30}, 'tom':{'english':30,'chinese':20}}when: item.value.english >=60
(jiankong_venv) [yy1s@rbtnode1 playbook]$ ansible-playbook ./f7.yml
因篇幅问题不能全部显示,请点此查看更多更全内容