我们在编写playbook的时候,不可避免的要执行一些重复性操作,比如指安装软件包,批量创建用户,操作某个目录下的所有文件等。正如我们所说,ansible一门简单的自动化语言,所以流程控制、循环语句这些编程语言的基本元素它同样都具备。
loop关键字说明
在playbook中使用循环,直接使用loop关键字即可。
示例1:
启动httpd和postfix服务
item调用loop中的变量
vim test.yml
---
- hosts: web
tasks:
- name: postfix and httpd are running
service:
name: "{{item}}"
state: started
loop:
- postfix
- httpd
[root@tdm2 playbook]# ansible-playbook test1.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [182.92.243.89]
TASK [postfix and httpd are running] ***********************************************************************************changed: [182.92.243.89] => (item=postfix)
changed: [182.92.243.89] => (item=httpd)
PLAY RECAP *************************************************************************************************************182.92.243.89 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
示例2:
也可以将loop循环的列表提前赋值给一个变量,然后在循环语句中调用
[root@tdm2 playbook]# cat test_service.yml
test_service:
- httpd
- postfix
vim test.yml
---
- hosts: web
vars_files:
- test_service.yml
tasks:
- name: postfix and httpd are running
service:
name: "{{item}}"
state: started
loop: "{{test_service}}"
[root@tdm2 playbook]# ansible-playbook test.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [182.92.243.89]
TASK [postfix and httpd are running] ***********************************************************************************changed: [182.92.243.89] => (item=httpd)
changed: [182.92.243.89] => (item=postfix)
PLAY RECAP *************************************************************************************************************182.92.243.89 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
示例3
vim test.yml
---
- hosts: web
tasks:
- name: add www group
group:
name: www
- name: add users
user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
loop:
- { name: 'test1', groups: 'wheel' }
- { name: 'test2', groups: 'www' }
[root@tdm2 playbook]# ansible-playbook test3.yml
PLAY [web] *************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************ok: [182.92.243.89]
TASK [add www group] ***************************************************************************************************ok: [182.92.243.89]
TASK [add users] ******************************************************************************************************ok: [182.92.243.89] => (item={u'name': u'test1', u'groups': u'wheel'})
ok: [182.92.243.89] => (item={u'name': u'test2', u'groups': u'www'})
PLAY RECAP *************************************************************************************************************182.92.243.89 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0