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

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       
 

知识点:使用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.shhost32 | 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.shhost32 | SUCCESS | rc=0 >>hello world[root@host31 ~]#
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

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 ~]#
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
 

明确写成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 ~]#
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

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 ~]#
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/sdfsdfytre/article/details/83578656