Ansible: localhost에서 원격 호스트로 파일, 디렉토리 또는 스크립트를 복사하는 방법은 무엇입니까?
게시 됨: 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(테스트 파일)
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
: 가능한 플레이북 -b -vvv -u root crunchify.yml -kkkk -extra-vars "crunchify-group" -i 호스트
모든 매개변수를 이해합시다.
- -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을 사용하여 원격 위치에 파일을 복사하는 데 질문이 있으면 알려주십시오.