Ansible:如何使用 vars_prompt 和命令行接受用戶輸入?
已發表: 2020-06-08 我開始使用 Ansible 已經幾個月了。 在本教程中,如果您可能希望在運行 ansible playbook 時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 |
第三步
創建文件crunchify.yml
文件,我們將在其中使用vars_prompt
部分來獲取用戶輸入。
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 }}' |
第四步
運行 ansible 劇本。
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'" |
如果您在運行 ansible 腳本時遇到任何問題,請告訴我們。