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

簡介

  • copy模塊用于將本地或遠(yuǎn)程機(jī)器上的文件拷貝到遠(yuǎn)程主機(jī)上。

模塊參數(shù)

名稱 必選 默認(rèn)值 可選值 備注
backup no no yes/no 在覆蓋之前將原文件備份,備份文件包含時(shí)間信息
content no 當(dāng)用content代替src參數(shù)的時(shí)候,可以把文檔的內(nèi)容設(shè)置到特定的值
dest yes 目標(biāo)絕對(duì)路徑。如果src是一個(gè)目錄,dest也必須是一個(gè)目錄。如果dest是不存在的路徑,并且如果dest以/結(jié)尾或者src是目錄,則dest被創(chuàng)建。如果srcdest是文件,如果dest的父目錄不存在,任務(wù)將失敗
follow no no yes/no 是否遵循目的機(jī)器中的文件系統(tǒng)鏈接
force no yes yes/no 當(dāng)內(nèi)容不同于源時(shí),將替換遠(yuǎn)程文件。設(shè)置為no,則只有在目標(biāo)不存在的情況下才會(huì)傳輸文件
group no 設(shè)置文件/目錄的所屬組,將被饋送到chown
local_follow no yes yes/no 是否遵循本地機(jī)器中的文件系統(tǒng)鏈接
mode no 設(shè)置文件權(quán)限,模式實(shí)際上是八進(jìn)制數(shù)字(如0644),少了前面的零可能會(huì)有意想不到的結(jié)果。從版本1.8開始,可以將模式指定為符號(hào)模式(例如u+rwx或u=rw,g=r,o=r)
owner no 設(shè)置文件/目錄的所屬用戶,將被饋送到chown
remote_src(2.0+) no no yes/no 如果yes它會(huì)從目標(biāo)機(jī)上搜索src文件
src no 將本地路徑復(fù)制到遠(yuǎn)程服務(wù)器; 可以是絕對(duì)路徑或相對(duì)的。如果是一個(gè)目錄,它將被遞歸地復(fù)制。如果路徑以/結(jié)尾,則只有該目錄下內(nèi)容被復(fù)制到目的地,如果沒有使用/來結(jié)尾,則包含目錄在內(nèi)的整個(gè)內(nèi)容全部復(fù)制
unsafe_writes no yes/no 是否以不安全的方式進(jìn)行,可能導(dǎo)致數(shù)據(jù)損壞
validate no None 復(fù)制前是否檢驗(yàn)需要復(fù)制目的地的路徑

示例

  • 拷貝前備份
[root@centos7 ~]# ansible test -m copy -a "src=test.sh backup=yes dest=/root"
172.20.21.121 | SUCCESS => {
    "backup_file": "/root/test.sh.4315.2018-01-12@13:35:35~", 
    "changed": true, 
    "checksum": "e989084b3f4610a41811c5ea280b14f7c5e855f5", 
    "dest": "/root/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "7c211ce4c7941a5bb064e77d69e3d9ff", 
    "mode": "0755", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 23, 
    "src": "/root/.ansible/tmp/ansible-tmp-1515735334.86-21848883747071/source", 
    "state": "file", 
    "uid": 0
}
  • src和dest都是文件,若dest的文件的父目錄不存在將報(bào)錯(cuò)
[root@centos7 ~]# ansible test -m copy -a "src=test.sh dest=/root/liuhao/test"
172.20.21.121 | FAILED! => {
    "changed": false, 
    "checksum": "e989084b3f4610a41811c5ea280b14f7c5e855f5", 
    "msg": "Destination directory /root/liuhao does not exist"
}
[root@centos7 ~]# ansible test -m copy -a "src=test.sh dest=/root/liuhao/"
172.20.21.121 | SUCCESS => {
    "changed": true, 
    "checksum": "e989084b3f4610a41811c5ea280b14f7c5e855f5", 
    "dest": "/root/liuhao/test.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "7c211ce4c7941a5bb064e77d69e3d9ff", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 23, 
    "src": "/root/.ansible/tmp/ansible-tmp-1515736119.26-238832413210409/source", 
    "state": "file", 
    "uid": 0
}
  • 設(shè)置文件權(quán)限
[root@centos7 ~]# ansible test -m copy -a "src=test.sh dest=/root dest=/tmp owner=liuhao group=liuhao mode=0644"
172.20.21.121 | SUCCESS => {
    "changed": true, 
    "checksum": "e989084b3f4610a41811c5ea280b14f7c5e855f5", 
    "dest": "/tmp/test.sh", 
    "gid": 1000, 
    "group": "liuhao", 
    "md5sum": "7c211ce4c7941a5bb064e77d69e3d9ff", 
    "mode": "0644", 
    "owner": "liuhao", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 23, 
    "src": "/root/.ansible/tmp/ansible-tmp-1515735466.22-33633697447932/source", 
    "state": "file", 
    "uid": 1000
}
目標(biāo)機(jī)器上的結(jié)果
目標(biāo)機(jī)器上的結(jié)果
  • content參數(shù)
[root@centos7 ~]# ansible test -m copy -a "content='liuhao \n test\n' dest=/root/liuhaotest"
172.20.21.121 | SUCCESS => {
    "changed": true, 
    "checksum": "bd3aa5bf19112271f30c07be425f9a5c08463568", 
    "dest": "/root/liuhaotest", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "7585dc638fd8e219c453c3b1330c7e14", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:admin_home_t:s0", 
    "size": 14, 
    "src": "/root/.ansible/tmp/ansible-tmp-1515735713.37-60072089981042/source", 
    "state": "file", 
    "uid": 0
}
content參數(shù)
content參數(shù)
  • force參數(shù)
[root@centos7 ~]# ansible test -m copy -a "src=test.sh dest=/root/liuhaotest force=no"
172.20.21.121 | SUCCESS => {
    "changed": false, 
    "dest": "/root/liuhaotest", 
    "src": "/root/test.sh"
}
  • src是目錄時(shí)
    源目錄以/結(jié)尾,只拷貝了目錄下的內(nèi)容:
[root@centos7 test]# ansible test -m copy -a "src=/root/test/ dest=/tmp/"
172.20.21.121 | SUCCESS => {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/tmp/1", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 0, 
    "src": "/root/.ansible/tmp/ansible-tmp-1515736521.16-258766767883601/source", 
    "state": "file", 
    "uid": 0
}
只拷貝了目錄下的內(nèi)容
只拷貝了目錄下的內(nèi)容

源目錄未以/結(jié)尾,直接將src目錄本身拷貝到目的地:

[root@centos7 test]# ansible test -m copy -a "src=/root/test dest=/tmp/"
172.20.21.121 | SUCCESS => {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/tmp/test/1", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 0, 
    "src": "/root/.ansible/tmp/ansible-tmp-1515736532.2-82893359525841/source", 
    "state": "file", 
    "uid": 0
}
直接將src目錄本身拷貝到目的地
直接將src目錄本身拷貝到目的地

如果覺得有用,歡迎關(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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