Ansible: Ansible Playbook을 실행하는 동안 파일을 삭제/재작성하는 방법은 무엇입니까?
게시 됨: 2019-08-29Ansible은 의심할 여지 없이 전 세계 500대 기업에서 사용하는 최고의 인프라 자동화 도구입니다.
Crunchify에서 Ansible에 대한 다양한 튜토리얼을 공개했습니다. 모든 자세한 자습서는 Ansible Archive를 살펴보십시오. 다음은 상위 3개 튜토리얼입니다.
- localhost에서 원격 호스트로 파일, 디렉토리 또는 스크립트를 복사하는 방법은 무엇입니까?
- Ansible을 사용하여 Amazon EC2 VM을 생성하는 방법은 무엇입니까?
- 원격 호스트에서 명령을 실행하고 명령 결과(로그)를 다시 가져오는 방법은 무엇입니까?
이 튜토리얼에서는 제가 일상적으로 사용하는 3가지 팁을 살펴보겠습니다.
Ansible을 사용하여 파일을 삭제하고 다시 만드는 방법은 무엇입니까?
1 단계
Mac 또는 Linux OS에서 Ansible을 올바르게 설정했는지 확인하십시오.
2 단계
crunchify-delete-recreate-ansible.yml
파일 생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
--- - name : Delete and Re - Create crunchify . txt file from current directory . hosts : local connection : local gather_facts : True tasks : - name : delete file ignore_errors : yes file : state : absent path : crunchify . txt - name : Ansible create file if it doesn ' t exist example ignore_errors : yes file : path : "crunchify.txt" state : touch |
3단계
hosts
파일을 열고 내용을 아래에 넣으십시오.
1 2 |
[ local ] localhost ansible_connection = local ansible_python_interpreter = python |
4단계
Ansible playbook
을 실행합니다.
1 |
ansible - playbook - vvv - i . / hosts crunchify - delete - recreate - ansible . yml |
결과는 다음과 같습니다.
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
WARNING : Executing a script that is loading libcrypto in an unsafe way . This will fail in a future version of macOS . Set the LIBRESSL_REDIRECT_STUB_ABORT = 1 in the environment to force this into an error . ansible - playbook 2.7.8 config file = None configured module search path = [ u '/Users/crunchify/.ansible/plugins/modules' , u '/usr/share/ansible/plugins/modules' ] ansible python module location = / Library / Python / 2.7 / site - packages / ansible executable location = / usr / local / bin / ansible - playbook python version = 2.7.16 ( default , Jul 14 2019 , 03 : 47 : 49 ) [ GCC 4.2.1 Compatible Apple LLVM 11.0.0 ( clang - 1100.0.32.4 ) ( - macos10 . 15 - objc - s No config file found ; using defaults / Users / crunchify / Documents / ansible / hosts did not meet host_list requirements , check plugin documentation if this is unexpected / Users / crunchify / Documents / ansible / hosts did not meet script requirements , check plugin documentation if this is unexpected Parsed / Users / crunchify / Documents / ansible / hosts inventory source with ini plugin PLAYBOOK : crunchify - delete - recreate - ansible . yml * *************************************************************************************************************** 1 plays in crunchify - delete - recreate - ansible . yml PLAY [ Delete and Re - Create crunchify . txt file from current directory . ] ***************************************************************************************** TASK [ Gathering Facts ] ***************************************************************************************************************************************** task path : / Users / crunchify / Documents / ansible / crunchify - delete - recreate - ansible . yml : 2 < localhost > ESTABLISH LOCAL CONNECTION FOR USER : crunchify < localhost > EXEC / bin / sh - c 'echo ~crunchify && sleep 0' < localhost > EXEC / bin / sh - c '( umask 77 && mkdir -p "` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779 `" && echo ansible-tmp-1567010309.76-127517206399779="` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779 `" ) && sleep 0' Using module file / Library / Python / 2.7 / site - packages / ansible / modules / system / setup . py < localhost > PUT / Users / crunchify / . ansible / tmp / ansible - local - 36562FsxMwP / tmp5SGRSa TO / Users / crunchify / . ansible / tmp / ansible - tmp - 1567010309.76 - 127517206399779 / AnsiballZ_setup . py < localhost > EXEC / bin / sh - c 'chmod u+x /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779/ /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779/AnsiballZ_setup.py && sleep 0' < localhost > EXEC / bin / sh - c 'python /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779/AnsiballZ_setup.py && sleep 0' < localhost > EXEC / bin / sh - c 'rm -f -r /Users/crunchify/.ansible/tmp/ansible-tmp-1567010309.76-127517206399779/ > /dev/null 2>&1 && sleep 0' ok : [ localhost ] META : ran handlers TASK [ delete file ] ********************************************************************************************************************************************* task path : / Users / crunchify / Documents / ansible / crunchify - delete - recreate - ansible . yml : 8 < localhost > ESTABLISH LOCAL CONNECTION FOR USER : crunchify < localhost > EXEC / bin / sh - c 'echo ~crunchify && sleep 0' < localhost > EXEC / bin / sh - c '( umask 77 && mkdir -p "` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750 `" && echo ansible-tmp-1567010310.35-144146434104750="` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750 `" ) && sleep 0' Using module file / Library / Python / 2.7 / site - packages / ansible / modules / files / file . py < localhost > PUT / Users / crunchify / . ansible / tmp / ansible - local - 36562FsxMwP / tmpvL0NpJ TO / Users / crunchify / . ansible / tmp / ansible - tmp - 1567010310.35 - 144146434104750 / AnsiballZ_file . py < localhost > EXEC / bin / sh - c 'chmod u+x /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750/ /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750/AnsiballZ_file.py && sleep 0' < localhost > EXEC / bin / sh - c 'python /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750/AnsiballZ_file.py && sleep 0' < localhost > EXEC / bin / sh - c 'rm -f -r /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.35-144146434104750/ > /dev/null 2>&1 && sleep 0' changed : [ localhost ] = > { "changed" : true , "diff" : { "after" : { "path" : "crunchify.txt" , "state" : "absent" } , "before" : { "path" : "crunchify.txt" , "state" : "file" } } , "invocation" : { "module_args" : { "_diff_peek" : null , "_original_basename" : null , "access_time" : null , "access_time_format" : "%Y%m%d%H%M.%S" , "attributes" : null , "backup" : null , "content" : null , "delimiter" : null , "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" : "crunchify.txt" , "recurse" : false , "regexp" : null , "remote_src" : null , "selevel" : null , "serole" : null , "setype" : null , "seuser" : null , "src" : null , "state" : "absent" , "unsafe_writes" : null } } , "path" : "crunchify.txt" , "state" : "absent" } TASK [ Ansible create file if it doesn 't exist example] ********************************************************************************************************* task path: /Users/crunchify/Documents/ansible/crunchify-delete-recreate-ansible.yml:14 <localhost> ESTABLISH LOCAL CONNECTION FOR USER: crunchify <localhost> EXEC /bin/sh -c ' echo ~ crunchify && sleep 0 ' <localhost> EXEC /bin/sh -c ' ( umask 77 && mkdir - p "` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.67-150889598387355 `" && echo ansible - tmp - 1567010310.67 - 150889598387355 = "` echo /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.67-150889598387355 `" ) && sleep 0 ' Using module file /Library/Python/2.7/site-packages/ansible/modules/files/file.py <localhost> PUT /Users/crunchify/.ansible/tmp/ansible-local-36562FsxMwP/tmpim0wPm TO /Users/crunchify/.ansible/tmp/ansible-tmp-1567010310.67-150889598387355/AnsiballZ_file.py <localhost> EXEC /bin/sh -c ' chmod u + x / Users / crunchify / . ansible / tmp / ansible - tmp - 1567010310.67 - 150889598387355 / / Users / crunchify / . ansible / tmp / ansible - tmp - 1567010310.67 - 150889598387355 / AnsiballZ_file . py && sleep 0 ' <localhost> EXEC /bin/sh -c ' python / Users / crunchify / . ansible / tmp / ansible - tmp - 1567010310.67 - 150889598387355 / AnsiballZ_file . py && sleep 0 ' <localhost> EXEC /bin/sh -c ' rm - f - r / Users / crunchify / . ansible / tmp / ansible - tmp - 1567010310.67 - 150889598387355 / > / dev / null 2 > &1 && sleep 0 ' changed : [ localhost ] = > { "changed" : true , "dest" : "crunchify.txt" , "diff" : { "after" : { "atime" : 1567010310.795358 , "mtime" : 1567010310.795358 , "path" : "crunchify.txt" , "state" : "touch" } , "before" : { "atime" : 1567010310.795286 , "mtime" : 1567010310.795286 , "path" : "crunchify.txt" , "state" : "absent" } } , "gid" : 20 , "group" : "staff" , "invocation" : { "module_args" : { "_diff_peek" : null , "_original_basename" : null , "access_time" : null , "access_time_format" : "%Y%m%d%H%M.%S" , "attributes" : null , "backup" : null , "content" : null , "delimiter" : null , "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" : "crunchify.txt" , "recurse" : false , "regexp" : null , "remote_src" : null , "selevel" : null , "serole" : null , "setype" : null , "seuser" : null , "src" : null , "state" : "touch" , "unsafe_writes" : null } } , "mode" : "0644" , "owner" : "crunchify" , "size" : 0 , "state" : "file" , "uid" : 502 } META : ran handlers META : ran handlers PLAY RECAP * **************************************************************************************************************************************************** localhost : ok = 3 changed = 2 unreachable = 0 failed = 0 |
그리고 그게 다야. 위의 Ansible 플레이북은 crunchify.txt
라는 기존 파일을 삭제하고 빈 콘텐츠로 새 파일을 생성합니다.

평소와 같이 Ansible Playbook에서 실행되는 모든 쿼리나 문제에 대해 알려주십시오.