无聊的IT

导航

1-5 ansible playbook 条件判断

 

 

#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.162
remote_user : root
tasks:
- 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.162
remote_user : root
tasks:
- 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.162
remote_user : root
tasks:
- file: dest=/tmp/bb state=directory
- copy: src={{item}} dest=/tmp/bb owner=root mode=600
with_fileglob:
- test/*

(jiankong_venv) [yy1s@rbtnode1 playbook]$ ansible-playbook ./f10.yml

 

 

#条件结合循环语句

(jiankong_venv) [yy1s@rbtnode1 playbook]$ cat f7.yml
---
- hosts : 144.131.252.162
remote_user : root
tasks:
- 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

 

  

posted on 2019-02-13 09:42  秦寒  阅读(169)  评论(0)    收藏  举报