实例:修改 /tmp/ 目录下的 1.txt 和 2.txt 文件属性 1)新建实验文件 touch /tmp/{1.txt,2.txt} //在 testhost 组中的所有主机上操 2)编辑配置文件
1
2
3
4
5
6
7
8
9
10
| [iyunv@web9 ansible]# 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
| [iyunv@web9 ansible]# ansible-playbook loop.yml
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [127.0.0.1]
ok: [web10.gz.com]
ok: [172.7.15.111]
TASK [change mode for files] ***************************************************
changed: [127.0.0.1] => (item=1.txt)
changed: [127.0.0.1] => (item=2.txt)
changed: [127.0.0.1] => (item=3.txt)
ok: [web10.gz.com] => (item=1.txt)
changed: [web10.gz.com] => (item=2.txt)
changed: [web10.gz.com] => (item=3.txt)
ok: [172.7.15.111] => (item=1.txt)
ok: [172.7.15.111] => (item=2.txt)
ok: [172.7.15.111] => (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
| [iyunv@web10 ~]# 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。
|
|