ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
copy模块用于复制文件到远程主机 ## 1. 模块相关选项 ### backup: > 在覆盖之前,将源文件备份,备份文件包含时间信息。有两个选项值:yes|no ### content: > 用于替代“src”,可以直接设定指定文件的值 ### dest: > 必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录 ### directory_mode: > 递归设定目录的权限,默认为系统默认权限 ### force: > 如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes ### others: > 所有的file模块里的选项都可以在这里使用 ### src: > 被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。 > > 如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync。 ## 2、实例 1、拷贝前备份 ``` # 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 } ``` 2、设置属性 设置用户,用户组,访问权限 ``` 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 } ``` 3、content参数直接指定文件的内容(代替src) 在远程主机上创建/root/liuhaotest文件,利用content指定文件内容为 'liuhao \n test\ ``` [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 } ``` 3、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 } ```