1. file模块
功能:为被控端创建文件或目录,设定权限属性;
主要参数如下:
参数 | 说明 |
---|---|
path | 指定远程服务器的路径,也可以写成‘dest’,‘name’ |
state | 状态,可以将值设定为directory表示创建目录,设定为touch表示创建文件,设定为link表示创建软连接,设定为hard表示创建硬连接,设定为absent表示删除目录文件或链接 |
mode | 文件复制到远程并设定权限,默认file=644,directory=755 |
owner | 文件复制到远程并设定属主,默认为root |
group | 文件复制到远程并设定属组,默认为root |
recurese | 递归修改 |
-
示例一:创建文件
/root/f1.sh
,并设定属主、属组、权限:[root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/f1.sh owner=root group=root mode=755 state=touch' 192.168.20.23 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/root/f1.sh", "gid": 0, "group": "root", "mode": "0755", "owner": "root", "size": 0, "state": "file", "uid": 0 } 192.168.20.22 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/root/f1.sh", "gid": 0, "group": "root", "mode": "0755", "owner": "root", "size": 0, "state": "file", "uid": 0 }
-
示例二:修改上例中文件的属主属组为
xu
:[root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/f1.sh owner=xu group=xu' 192.168.20.22 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1000, "group": "xu", "mode": "0755", "owner": "xu", "path": "/root/f1.sh", "size": 0, "state": "file", "uid": 1000 } 192.168.20.23 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 1000, "group": "xu", "mode": "0755", "owner": "xu", "path": "/root/f1.sh", "size": 0, "state": "file", "uid": 1000 } [root@nginx03 ~]# ll /root/f1.sh -rwxr-xr-x 1 xu xu 0 Aug 1 22:22 /root/f1.sh
-
示例三:创建目录
/root/test/
,并设定属主、属组、权限[root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/test owner=root group=root mode=755 state=directory' 192.168.20.23 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/root/test", "size": 6, "state": "directory", "uid": 0 } 192.168.20.22 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/root/test", "size": 6, "state": "directory", "uid": 0 } [root@nginx03 ~]# ll /root/test/ -d drwxr-xr-x 2 root root 6 Aug 1 22:26 /root/test/
-
示例四:为
/root/f1.sh
创建软链接文件/root/f1.sh.link
:[root@xuzhichao ~]# ansible NginxWebs -m file -a 'src=/root/f1.sh dest=/root/f1.sh.link state=link' 192.168.20.23 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/root/f1.sh.link", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 11, "src": "/root/f1.sh", "state": "link", "uid": 0 } 192.168.20.22 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/root/f1.sh.link", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 11, "src": "/root/f1.sh", "state": "link", "uid": 0 } [root@nginx03 ~]# ll /root/f1.sh* -rwxr-xr-x 1 xu xu 0 Aug 1 22:22 /root/f1.sh lrwxrwxrwx 1 root root 11 Aug 1 22:28 /root/f1.sh.link -> /root/f1.sh
-
示例五:删除以上示例中创建的目录和文件:
[root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/f1.sh.link state=absent' 192.168.20.23 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "/root/f1.sh.link", "state": "absent" } 192.168.20.22 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "/root/f1.sh.link", "state": "absent" } [root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/f1.sh state=absent' [root@xuzhichao ~]# ansible NginxWebs -m file -a 'path=/root/test state=absent'
-
示例六:也可以删除一级目录(挂载点),会报错,但是可以清楚数据:
[root@localhost /data] #ansible app -m file -a 'dest=/data/ state=absent' 192.168.169.130 | FAILED! => { "changed": false, <==报错 [root@localhost /data] #ansible app -a 'ls -l /data/' total 0 <==数据已经清空
-
示例七:递归授权目录,类似于
-R
的作用:[root@manger ~]# ansible webservers -m file -a "path=/tmp/foo state=directory owner=root group=root mode=777 recurse=yes"