Ansible运维监控

【Ansible学习】- 常用文件操作模块之copy模块

2018-01-12  本文已影响28人  hoxis

简介

模块参数

名称 必选 默认值 可选值 备注
backup no no yes/no 在覆盖之前将原文件备份,备份文件包含时间信息
content no 当用content代替src参数的时候,可以把文档的内容设置到特定的值
dest yes 目标绝对路径。如果src是一个目录,dest也必须是一个目录。如果dest是不存在的路径,并且如果dest以/结尾或者src是目录,则dest被创建。如果srcdest是文件,如果dest的父目录不存在,任务将失败
follow no no yes/no 是否遵循目的机器中的文件系统链接
force no yes yes/no 当内容不同于源时,将替换远程文件。设置为no,则只有在目标不存在的情况下才会传输文件
group no 设置文件/目录的所属组,将被馈送到chown
local_follow no yes yes/no 是否遵循本地机器中的文件系统链接
mode no 设置文件权限,模式实际上是八进制数字(如0644),少了前面的零可能会有意想不到的结果。从版本1.8开始,可以将模式指定为符号模式(例如u+rwx或u=rw,g=r,o=r)
owner no 设置文件/目录的所属用户,将被馈送到chown
remote_src(2.0+) no no yes/no 如果yes它会从目标机上搜索src文件
src no 将本地路径复制到远程服务器; 可以是绝对路径或相对的。如果是一个目录,它将被递归地复制。如果路径以/结尾,则只有该目录下内容被复制到目的地,如果没有使用/来结尾,则包含目录在内的整个内容全部复制
unsafe_writes no yes/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
}
[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
}
目标机器上的结果目标机器上的结果
[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参数content参数
[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"
}
[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
}
直接将src目录本身拷贝到目的地直接将src目录本身拷贝到目的地
上一篇下一篇

猜你喜欢

热点阅读