ansible playbook循环
实例:修改 /tmp/ 目录下的 1.txt 和 2.txt 文件属性1)新建实验文件touch /tmp/{1.txt,2.txt} //在 testhost 组中的所有主机上操2)编辑配置文件1
2
3
4
5
6
7
8
9
10
# vim loop.yml
---
- hosts: testhost
user: root
tasks:
- name: change mode for files
file: path=/tmp/{{ item }} mode=600 owner=root group=root
with_items:
- 1.txt
- 2.txt
3)执行配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# ansible-playbook loop.yml
PLAY ***************************************************************************
TASK *******************************************************************
ok:
ok:
ok:
TASK ***************************************************
changed: => (item=1.txt)
changed: => (item=2.txt)
changed: => (item=3.txt)
ok: => (item=1.txt)
changed: => (item=2.txt)
changed: => (item=3.txt)
ok: => (item=1.txt)
ok: => (item=2.txt)
ok: => (item=3.txt)
PLAY RECAP *********************************************************************
127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0
172.7.15.111 : ok=2 changed=0 unreachable=0 failed=0
web10.gz.com : ok=2 changed=1 unreachable=0 failed=0
查看效果
1
2
3
4
5
6
# ls -l /tmp/
总用量 20
-rw-------. 1 root root 1273 4月22 04:49 1.txt
-rw-------. 1 root root 0 4月22 04:49 2.txt
-rw-------. 1 root root 0 4月22 04:49 3.txt
注意:可看到权限为 600,主和组都为root。
页:
[1]