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 |
ステップ-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
ansiblePlaybookを実行します。
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スクリプトの実行で問題が発生した場合は、お知らせください。