简介
-
copy
模块用于将本地或远程机器上的文件拷贝到远程主机上。
模块参数
名称 | 必选 | 默认值 | 可选值 | 备注 |
backup | no | no |
| 在覆盖之前将原文件备份,备份文件包含时间信息 |
content | no | 当用content代替src参数的时候,可以把文档的内容设置到特定的值 | ||
dest | yes | 目标绝对路径。如果 | ||
follow | no | no |
| 是否遵循目的机器中的文件系统链接 |
force | no | yes |
| 当内容不同于源时,将替换远程文件。设置为 |
group | no | 设置文件/目录的所属组,将被馈送到chown | ||
local_follow | no | yes |
| 是否遵循本地机器中的文件系统链接 |
mode | no | 设置文件权限,模式实际上是八进制数字(如0644),少了前面的零可能会有意想不到的结果。从版本1.8开始,可以将模式指定为符号模式(例如u+rwx或u=rw,g=r,o=r) | ||
owner | no | 设置文件/目录的所属用户,将被馈送到chown | ||
remote_src(2.0+) | no | no |
| 如果yes它会从目标机上搜索src文件 |
src | no | 将本地路径复制到远程服务器; 可以是绝对路径或相对的。如果是一个目录,它将被递归地复制。如果路径以 | ||
unsafe_writes | no |
| 是否以不安全的方式进行,可能导致数据损坏 | |
validate | no | None | 复制前是否检验需要复制目的地的路径 |
示例
- 拷贝前备份
[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的文件的父目录不存在将报错
[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
}
- 设置文件权限
[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参数
[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参数
[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是目录时
源目录以/
结尾,只拷贝了目录下的内容:
[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
}
源目录未以/
结尾,直接将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
}