自动化运维、大数据、Docker

实例学习ansible系列(5)常用模块之copy

知识点:使用copy模块,可以实现向目标机器进行远程copy的能力。

使用参数说明

参数 说明
src 被复制到远程主机的本地对象文件或者文件夹,可以是绝对路径,也可以是相对路径。
dest 被复制到远程主机的本地对象文件或者文件夹
mode 复制对象的设定权限
backup 在文件存在的时候可以选择覆盖之前,将源文件备份.设定值:yes/no 缺省为yes
force 是否强制覆盖.设定值:yes/no 缺省为no
其余请自行ansible-doc -s copy

使用实例

使用ansible的copy的module将ttt.sh文件copy到远程的目标机上并命名为hello.sh

[root@host31 ~]# ansible host32 -m command -a /tmp/hello.sh
host32 | FAILED | rc=2 >>
[Errno 2] No such file or directory

[root@host31 ~]# ansible host32 -m copy -a "src=/tmp/ttt.sh dest=/tmp/hello.sh mode=0750"
host32 | SUCCESS => {
    "changed": true,
    "checksum": "098994f5d86562667b71ec90d13904eedf1be5f1",
    "dest": "/tmp/hello.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "fcc7e6c36e7a19db4b69fab163e03a36",
    "mode": "0750",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 31,
    "src": "/root/.ansible/tmp/ansible-tmp-1469870735.2-12356407661121/source",
    "state": "file",
    "uid": 0
}
[root@host31 ~]# ansible host32 -m command -a /tmp/hello.sh
host32 | SUCCESS | rc=0 >>
hello world

[root@host31 ~]#

force使用实例

default的情况下,force是yes的,所以什么都不写,文件存在的情况是会被覆盖的,如下所示。

[root@host31 ~]# ansible host32 -m raw -a "ls -l /tmp/hello.sh"
host32 | SUCCESS | rc=0 >>
-rwxr-x---. 1 root root 31 Jul 30 05:25 /tmp/hello.sh


[root@host31 ~]# touch /tmp/ttt
[root@host31 ~]# ll /tmp/ttt
-rw-r--r--. 1 root root 0 Jul 30 05:39 /tmp/ttt
[root@host31 ~]# ansible host32 -m copy -a "src=/tmp/ttt dest=/tmp/hello.sh"
host32 | SUCCESS => {
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/tmp/hello.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    "mode": "0750",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 0,
    "src": "/root/.ansible/tmp/ansible-tmp-1469871615.42-248549508847058/source",
    "state": "file",
    "uid": 0
}
[root@host31 ~]# ansible host32 -m raw -a "ls -l /tmp/hello.sh"
host32 | SUCCESS | rc=0 >>
-rwxr-x---. 1 root root 0 Jul 30 05:40 /tmp/hello.sh


[root@host31 ~]#

明确写成force=no,此时将不会被覆盖。

[root@host31 ~]# ansible host32 -m shell -a "ls -l /tmp/hello.sh"
host32 | SUCCESS | rc=0 >>
-rwxr-x---. 1 root root 0 Jul 30 05:40 /tmp/hello.sh

[root@host31 ~]# ll /tmp/ttt.sh
-rwxr-x---. 1 root root 31 Jul 30 03:32 /tmp/ttt.sh
[root@host31 ~]# ansible host32 -m copy -a "src=/tmp/ttt.sh dest=/tmp/hello.sh force=no"
host32 | SUCCESS => {
    "changed": false,
    "dest": "/tmp/hello.sh",
    "src": "/tmp/ttt.sh"
}
[root@host31 ~]# ansible host32 -m shell -a "ls -l /tmp/hello.sh"
host32 | SUCCESS | rc=0 >>
-rwxr-x---. 1 root root 0 Jul 30 05:40 /tmp/hello.sh

[root@host31 ~]#

backup使用实例

覆盖的动作作出之前,其会真正覆盖之前,会作出一个带时间戳的文件作为backup文件

[root@host31 ~]# ansible host32 -m copy -a "src=/tmp/ttt.sh dest=/tmp/hello.sh backup=yes"
host32 | SUCCESS => {
    "backup_file": "/tmp/hello.sh.2016-07-30@05:59:50~",
    "changed": true,
    "checksum": "098994f5d86562667b71ec90d13904eedf1be5f1",
    "dest": "/tmp/hello.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "fcc7e6c36e7a19db4b69fab163e03a36",
    "mode": "0750",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 31,
    "src": "/root/.ansible/tmp/ansible-tmp-1469872789.7-172371209052357/source",
    "state": "file",
    "uid": 0
}
[root@host31 ~]# ansible host32 -m shell -a "ls -l /tmp/hello*"
host32 | SUCCESS | rc=0 >>
-rwxr-x---. 1 root root 31 Jul 30 05:59 /tmp/hello.sh
-rwxr-x---. 1 root root  0 Jul 30 05:40 /tmp/hello.sh.2016-07-30@05:59:50~ -〉此文件为backup文件
-rw-r--r--. 1 root root 12 Jul 29 10:18 /tmp/helloworld

[root@host31 ~]#

本文转载自 实例学习ansible系列(5)常用模块之copy

赞(0) 打赏
蜷缩的蜗牛 , 版权所有丨如未注明 , 均为原创丨 转载请注明蜷缩的蜗牛 » 实例学习ansible系列(5)常用模块之copy
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏