Ansible:如何將文件、目錄或腳本從本地主機複製到遠程主機?
已發表: 2019-02-24 作為 IT 管理部門的一部分,人們處理lots of scripts
和命令行工具。 我敢肯定,您每天都會遇到將文件或腳本複製到數千台遠程主機的情況。
IT 部門的人不只是登錄到每一個thousands of hosts
並手動運行腳本。 這就是 Ansible 出現的原因。 Ansible 是一款非常強大的 IT 管理解決方案,可供我們免費使用。
在本教程中,我們將介紹使用 Ansible 將文件複製到遠程主機的步驟。
我們要做什麼?
將文件crunchify.txt
文件從位置/opt/ashah/
到遠程主機192.66.129.83
的目標目錄/opt/ashah/
。
第1步
在 Linux Ubuntu 或 macOS 上安裝 Ansible。 按照教程了解詳細步驟。
第2步
讓我們了解本教程的一些基礎知識和文件位置:
- 配置文件位置:/etc/ansible/ansible.cfg
- 腳本文件:/opt/ashah/crunchify.yml
- 主機文件:/opt/ashah/hosts
- 要復制的文件:/opt/ashah/crunchify.txt(測試文件)
第三步
crunchify.yml
文件內容。
1 2 3 4 5 6 7 8 |
root @ localhost : / opt / ashah # cat crunchify.yml --- - hosts : crunchify - group tasks : - name : copy file to remote host copy : src : / opt / ashah / crunchify . txt dest : / opt / ashah |
第四步
hosts
文件內容。 確保將 IP 地址更改為遠程主機。
1 2 3 4 |
root @ localhost : / opt / ashah # cat hosts #crunchify-group as a sample [ crunchify - group ] 192.66.129.83 |
第 5 步
使用以下內容創建文件crunchify.txt
。
1 2 3 4 |
root @ localhost : / opt / ashah # cat crunchify.txt This is sample example copying file to remote location using Ansible . More ansible tutorials at https : //crunchify.com/tag/ansible/ |
第 6 步
運行以下命令將文件複製到遠程主機:
-
Command
:ansible-playbook -b -vvv -u root crunchify.yml -kkkk –extra-vars “crunchify-group” -i hosts
讓我們了解所有參數:
- -b :使用 become 運行操作(與 —become 相同)。
- -vvv :詳細模式(-vvv 更多,-vvvv 啟用連接調試)。
- -u :以該用戶身份連接(默認=無)。
- -kkkk : 詢問連接密碼。
- –extra-vars :如果文件名以 @ 開頭,則將其他變量設置為 key=value 或 YAML/JSON。
- -i :指定清單主機路徑或逗號分隔的主機列表。
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
root @ localhost : / opt / ashah # ansible-playbook -b -vvv -u root crunchify.yml -kkkk --extra-vars "crunchify-group" -i hosts Using / etc / ansible / ansible . cfg as config file SSH password : / opt / ashah / hosts did not meet host_list requirements , check plugin documentation if this is unexpected / opt / ashah / hosts did not meet script requirements , check plugin documentation if this is unexpected Parsed / opt / ashah / hosts inventory source with ini plugin PLAYBOOK : crunchify . yml * ************************************************************************************************************************ 1 plays in crunchify . yml PLAY [ crunchify - group ] ********************************************************************************************************************************* TASK [ Gathering Facts ] ************************************************************************************************************************ < 192.66.129.83 > ESTABLISH SSH CONNECTION FOR USER : root < 192.66.129.83 > SSH : EXEC sshpass - d9 ssh - C - o ControlMaster = auto - o ControlPersist = 60s - o User = root - o ConnectTimeout = 10 - o ControlPath =/ root / . ansible / cp / 5fca43321f 192.66.129.83 '/bin/sh -c ' "'" 'echo ~root && sleep 0' "'" '' < 192.66.129.83 > ( 0 , '/root\n' , '' ) < 192.66.129.83 > ESTABLISH SSH CONNECTION FOR USER : root < 192.66.129.83 > SSH : EXEC sshpass - d9 ssh - C - o ControlMaster = auto - o ControlPersist = 60s - o User = root - o ConnectTimeout = 10 - o ControlPath =/ root / . ansible / cp / 5fca43321f 192.66.129.83 '/bin/sh -c ' "'" '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791 `" && echo ansible-tmp-1550966284.57-6253496643791="` echo /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791 `" ) && sleep 0' "'" '' < 192.66.129.83 > ( 0 , 'ansible-tmp-1550966284.57-6253496643791=/root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791\n' , '' ) Using module file / usr / lib / python2 . 7 / dist - packages / ansible / modules / system / setup . py < 192.66.129.83 > PUT / root / . ansible / tmp / ansible - local - 32435HXzj7o / tmpjFxUXp TO / root / . ansible / tmp / ansible - tmp - 1550966284.57 - 6253496643791 / AnsiballZ_setup . py < 192.66.129.83 > SSH : EXEC sshpass - d9 sftp - o BatchMode = no - b - - C - o ControlMaster = auto - o ControlPersist = 60s - o User = root - o ConnectTimeout = 10 - o ControlPath =/ root / . ansible / cp / 5fca43321f '[192.66.129.83]' < 192.66.129.83 > ( 0 , 'sftp> put /root/.ansible/tmp/ansible-local-32435HXzj7o/tmpjFxUXp /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791/AnsiballZ_setup.py\n' , '' ) < 192.66.129.83 > ESTABLISH SSH CONNECTION FOR USER : root < 192.66.129.83 > ESTABLISH SSH CONNECTION FOR USER : root < 192.66.129.83 > SSH : EXEC sshpass - d9 ssh - C - o ControlMaster = auto - o ControlPersist = 60s - o User = root - o ConnectTimeout = 10 - o ControlPath =/ root / . ansible / cp / 5fca43321f 192.66.129.83 '/bin/sh -c ' "'" 'rm -f -r /root/.ansible/tmp/ansible-tmp-1550966284.57-6253496643791/ > /dev/null 2>&1 && sleep 0' "'" '' < 192.66.129.83 > ( 0 , '' , '' ) ok : [ 192.66.129.83 ] TASK [ deploy topology ] ******************************************************************************************************************************************** ok : [ 192.66.129.83 ] = > { "changed" : false , "checksum" : "289b0c36372210c92bf028f2a19d57333af4d63c" , "dest" : "/opt/ashah/crunchify.txt" , "diff" : { "after" : { "path" : "/opt/ashah/crunchify.txt" } , "before" : { "path" : "/opt/ashah/crunchify.txt" } } , "gid" : 0 , "group" : "root" , "invocation" : { "module_args" : { "_diff_peek" : null , "_original_basename" : "crunchify.txt" , "access_time" : null , "access_time_format" : "%Y%m%d%H%M.%S" , "attributes" : null , "backup" : null , "content" : null , "delimiter" : null , "dest" : "/opt/ashah" , "directory_mode" : null , "follow" : true , "force" : false , "group" : null , "mode" : null , "modification_time" : null , "modification_time_format" : "%Y%m%d%H%M.%S" , "owner" : null , "path" : "/opt/ashah/crunchify.txt" , "recurse" : false , "regexp" : null , "remote_src" : null , "selevel" : null , "serole" : null , "setype" : null , "seuser" : null , "src" : null , "state" : "file" , "unsafe_writes" : null } } , "mode" : "0644" , "owner" : "root" , "path" : "/opt/ashah/crunchify.txt" , "size" : 21 , "state" : "file" , "uid" : 0 } PLAY RECAP * ******************************************************************************************************************************************************* 192.66.129.83 : ok = 2 changed = 0 unreachable = 0 failed = 0 |
而已。 您已成功複製文件。

如何驗證文件是否已復製到新的遠程主機?
只需登錄到遠程主機並檢查位置/opt/ashah
,您將看到文件crunchify.txt
如何將目錄內容複製到另一個目錄?
只需更改crunchify.yml
文件的src
和desc
值。 這是一個更新的 crunchify.yml 文件。
1 2 3 4 5 6 7 8 |
root @ localhost : / opt / ashah # cat crunchify.yml --- - hosts : crunchify - group tasks : - name : copy all files from folder to remote host copy : src : / opt / ashah / dest : / opt / ashah / |
/opt/ashah/
文件夾下的所有文件現在將被複製到遠程主機。
如果您對使用 Ansible 將文件處理到遠程位置有任何疑問,請告訴我。