Ansible: vars_prompt 및 명령줄을 사용하여 사용자 입력을 수락하는 방법은 무엇입니까?
게시 됨: 2020-06-08 Ansible을 사용하기 시작한 지 몇 달이 지났습니다. 이 튜토리얼에서는 가능한 플레이북을 실행하는 동안 prompt the user
에게 특정 입력을 요청하려는 경우 vars_prompt ansible section
을 사용하는 방법을 살펴보겠습니다.
시작하자:
1 단계
hosts
파일을 생성하고 /Users/Shared/ansible
폴더에 넣습니다.
1 2 3 4 5 6 7 8 |
[ local ] localhost ansible_connection = local ansible_python_interpreter = python [ defaults ] host_key_checking = false [ crunchify ] localhost |
2 단계
기본 ansible.cfg
파일을 같은 폴더에 넣습니다.
1 2 3 4 |
[ defaults ] ansible_ssh_user = crunchify inventory = hosts host_key_checking = false |
3단계
vars_prompt
섹션을 사용하여 사용자 입력을 받을 파일 crunchify.yml
파일을 만듭니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
--- - hosts : crunchify connection : local vars_prompt : - name : website prompt : "What is your website?" private : no - name : country prompt : "Enter Country Name?" private : no tasks : - debug : msg : 'Website name: {{ website }}. Country name: {{ country }}' |
4단계
실행 가능한 플레이북을 실행합니다.
1 2 3 4 |
bash - 3.2 $ pwd / Users / Shared / ansible bash - 3.2 $ ansible - playbook - vvv crunchify . 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 |
bash - 3.2 $ ansible - playbook - vvv crunchify . yml ansible - playbook 2.9.9 config file = / Users / Shared / ansible / ansible . cfg configured module search path = [ u '/Users/Shared/.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 , Apr 17 2020 , 18 : 29 : 03 ) [ GCC 4.2.1 Compatible Apple LLVM 11.0.3 ( clang - 1103.0.29.20 ) ( - macos10 . 15 - objc - Using / Users / Shared / ansible / ansible . cfg as config file host_list declined parsing / Users / Shared / ansible / hosts as it did not pass its verify_file ( ) method script declined parsing / Users / Shared / ansible / hosts as it did not pass its verify_file ( ) method auto declined parsing / Users / Shared / ansible / hosts as it did not pass its verify_file ( ) method Parsed / Users / Shared / ansible / hosts inventory source with ini plugin PLAYBOOK : crunchify . yml * *************************************************************************************************************************************** 1 plays in crunchify . yml What is your website ? : Crunchify . com Enter Country Name ? : United States PLAY [ crunchify ] *********************************************************************************************************************************************** TASK [ Gathering Facts ] ***************************************************************************************************************************************** task path : / Users / Shared / ansible / crunchify . yml : 2 < localhost > ESTABLISH LOCAL CONNECTION FOR USER : Shared < localhost > EXEC / bin / sh - c 'echo ~Shared && sleep 0' < localhost > EXEC / bin / sh - c '( umask 77 && mkdir -p "` echo /Users/Shared/.ansible/tmp `"&& mkdir /Users/Shared/.ansible/tmp/ansible-tmp-1591586311.68-34952-72441309640701 && echo ansible-tmp-1591586311.68-34952-72441309640701="` echo /Users/Shared/.ansible/tmp/ansible-tmp-1591586311.68-34952-72441309640701 `" ) && sleep 0' Using module file / Library / Python / 2.7 / site - packages / ansible / modules / system / setup . py < localhost > PUT / Users / Shared / . ansible / tmp / ansible - local - 34946d45ikK / tmpuEtZGL TO / Users / Shared / . ansible / tmp / ansible - tmp - 1591586311.68 - 34952 - 72441309640701 / AnsiballZ_setup . py < localhost > EXEC / bin / sh - c 'chmod u+x /Users/Shared/.ansible/tmp/ansible-tmp-1591586311.68-34952-72441309640701/ /Users/Shared/.ansible/tmp/ansible-tmp-1591586311.68-34952-72441309640701/AnsiballZ_setup.py && sleep 0' < localhost > EXEC / bin / sh - c 'python /Users/Shared/.ansible/tmp/ansible-tmp-1591586311.68-34952-72441309640701/AnsiballZ_setup.py && sleep 0' < localhost > EXEC / bin / sh - c 'rm -f -r /Users/Shared/.ansible/tmp/ansible-tmp-1591586311.68-34952-72441309640701/ > /dev/null 2>&1 && sleep 0' ok : [ localhost ] META : ran handlers TASK [ debug ] *************************************************************************************************************************************************** task path : / Users / Shared / ansible / crunchify . yml : 16 ok : [ localhost ] = > { "msg" : "Website name: Crunchify.com. Country name: United States" } META : ran handlers META : ran handlers PLAY RECAP * **************************************************************************************************************************************************** localhost : ok = 2 changed = 0 unreachable = 0 failed = 0 skipped = 0 rescued = 0 ignored = 0 |

명령줄을 통해 사용자 프롬프트를 건너뛰고 인수를 수락하는 방법:
1 |
bash - 3.2 $ ansible - playbook - vvv crunchify . yml - e "website='Crunchify.com' country='United States'" |
가능한 스크립트를 실행하는 데 문제가 있으면 알려주십시오.