【Ansible學(xué)習(xí)】- 常用文件操作模塊之file模塊

簡(jiǎn)介

file模塊,文件屬性模塊,主要用于設(shè)置已存在文件、符號(hào)鏈接或者目錄的屬性,或刪除文件/符號(hào)鏈接/目錄。

模塊參數(shù)

名稱 必選 默認(rèn)值 可選值 備注
follow no no yes/no 是否遵循目的機(jī)器中的文件系統(tǒng)鏈接
force no yes yes/no 強(qiáng)制執(zhí)行
group no 設(shè)置文件/目錄的所屬組
mode no 設(shè)置文件權(quán)限,模式實(shí)際上是八進(jìn)制數(shù)字(如0644),少了前面的零可能會(huì)有意想不到的結(jié)果。從版本1.8開始,可以將模式指定為符號(hào)模式(例如u+rwxu=rw,g=r,o=r
owner no 設(shè)置文件/目錄的所屬用戶
path yes 目標(biāo)文件/目錄,也可以用dest,name代替
recurse no no yes/no 是否遞歸設(shè)置屬性(僅適用于state=directory
src no 要鏈接到的文件路徑(僅適用于state=link
state no file file/link/directory/hard/touch/absent 若果是directory,所有的子目錄將被創(chuàng)建(如果它們不存在);若是file,文件將不會(huì)被創(chuàng)建(如果文件不存在);link表示符號(hào)鏈接;若是absent,目錄或文件會(huì)被遞歸刪除;touch代表生成一個(gè)空文件;hard代表硬鏈接;
unsafe_writes no yes/no 是否以不安全的方式進(jìn)行,可能導(dǎo)致數(shù)據(jù)損壞

示例

設(shè)置文件權(quán)限 - owner\group\mode

[root@centos7 ~]# ansible test -m file -a "path=/root/test.sh owner=liuhao group=liuhao mode=0777"
172.20.21.121 | SUCCESS => {
    "changed": true, 
    "gid": 1000, 
    "group": "liuhao", 
    "mode": "0777", 
    "owner": "liuhao", 
    "path": "/root/test.sh", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 23, 
    "state": "file", 
    "uid": 1000
}
設(shè)置文件權(quán)限
設(shè)置文件權(quán)限

創(chuàng)建空文件 - state=touch

[root@centos7 ~]# ansible test -m file -a "path=/tmp/liuhao_testfile state=touch mode=0644"
172.20.21.121 | SUCCESS => {
    "changed": true, 
    "dest": "/tmp/liuhao_testfile", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
創(chuàng)建空文件
創(chuàng)建空文件

創(chuàng)建目錄 - state=directory

目錄是遞歸創(chuàng)建的

[root@centos7 ~]# ansible test -m file -a "path=/tmp/liuhao/test/2018 state=directory mode=0644"
172.20.21.121 | SUCCESS => {
    "changed": true, 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "path": "/tmp/liuhao/test/2018", 
    "secontext": "unconfined_u:object_r:user_tmp_t:s0", 
    "size": 6, 
    "state": "directory", 
    "uid": 0
}
創(chuàng)建目錄
創(chuàng)建目錄

目標(biāo)文件不存在

state=file時(shí)會(huì)報(bào)錯(cuò),但是state=absent不會(huì)報(bào)錯(cuò),執(zhí)行狀態(tài)也不會(huì)變化。

[root@centos7 ~]# ansible test -m file -a "path=/tmp/liuhao/test/2018/1 state=file mode=0644"
172.20.21.121 | FAILED! => {
    "changed": false, 
    "msg": "file (/tmp/liuhao/test/2018/1) is absent, cannot continue", 
    "path": "/tmp/liuhao/test/2018/1", 
    "state": "absent"
}
[root@centos7 ~]# ansible test -m file -a "path=/tmp/liuhao/test/2018/1 state=absent mode=0644"
172.20.21.121 | SUCCESS => {
    "changed": false, 
    "path": "/tmp/liuhao/test/2018/1", 
    "state": "absent"
}

刪除文件或者目錄 - state=absent

[root@centos7 ~]# ansible test -m file -a "path=/tmp/liuhao state=absent"
172.20.21.121 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/liuhao", 
    "state": "absent"
}

[root@centos7 ~]# ansible test -m file -a "path=/tmp/liuhao_testfile state=absent"
172.20.21.121 | SUCCESS => {
    "changed": true, 
    "path": "/tmp/liuhao_testfile", 
    "state": "absent"
}

如果覺得有用,歡迎關(guān)注我的微信,有問題可以直接交流:

你的關(guān)注是對(duì)我最大的鼓勵(lì)!
你的關(guān)注是對(duì)我最大的鼓勵(lì)!
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容