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
LinuxUbuntuまたはmacOSにAnsibleをインストールします。 チュートリアルに従って、詳細な手順を確認してください。
ステップ2
このチュートリアルの基本とファイルの場所を理解しましょう。
- 構成ファイルの場所:/etc/ansible/ansible.cfg
- スクリプトファイル:/opt/ashah/crunchify.yml
- ホストファイル:/ opt / ashah / hosts
- コピーするファイル:/opt/ashah/crunchify.txt(テストファイル)
ステップ-3
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 |
ステップ-4
ファイルコンテンツ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を使用してリモートロケーションにファイルを対処する質問がある場合はお知らせください。