例項學習ansible系列(5)常用模組之copy
阿新 • • 發佈:2018-11-02
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!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/[email protected]: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