簡介
-
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)建谓苟。如果src 和dest 是文件官脓,如果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
}
- 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
}
- 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
}
源目錄未以/
結(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
}
如果覺得有用缩焦,歡迎關(guān)注我的微信读虏,有問題可以直接交流: