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"