下面對file模組常用引數介紹一下,然後結合例項講解用法。
path引數:這引數是必須的,它指明瞭被管理的檔案或資料夾的路徑
state引數:這個引數很靈活,不同的模組都會有state引數,模組不一樣它的值也不一樣,後面回結合例項說明state引數不同的值作用和它的作用。
src引數:做軟連結或硬連結的時候指定連結的原始檔
例1:在/tmp目錄下建立檔案mytest 許可權位0644
[root@localhost ~]# ansible 192.168.233.167 -m file -a "path=/tmp/mytest state=touch mode=0644"192.168.233.167 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/tmp/mytest", "gid": 0, "group": "root", "mode": "0644", "owner": "root", "size": 0, "state": "file", "uid": 0}
例2: 刪除剛才建立的mytest檔案
[root@localhost ~]# ansible 192.168.233.167 -m file -a "path=/tmp/mytest state=absent"192.168.233.167 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "path": "/tmp/mytest", "state": "absent"}
例3: 在/tmp目錄下建立資料夾dirtest 許可權是0755
[root@localhost ~]# ansible 192.168.233.167 -m file -a "path=/tmp/dirtest state=directory mode=0755"192.168.233.167 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0755", "owner": "root", "path": "/tmp/dirtest", "size": 4096, "state": "directory", "uid": 0}
例4: 建立軟連線相當Linux命令"ln -s"
[root@localhost ~]# ansible 192.168.233.167 -m file -a "src=/tmp/dirtest dest=/tmp/testlink state=link"192.168.233.167 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/tmp/testlink", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 12, "src": "/tmp/dirtest", "state": "link", "uid": 0}
以上就是file模組常用的幾種方式了。
主要就是state值的區別,下面總結一下:
touch:建立檔案
directory:建立目錄
link:建立軟連結。如果建立硬連結state的值就是hard
注意:mode值的前面的0不要省略掉,要寫成"0755"或"0644"而不是"755"和"644"。
具體為什麼我也不知道,官方是這樣說的。可能會引起bug吧。(純屬個人猜測)