It’s been few months I started playing with Ansible. In this tutorial, we will go over how to use vars_prompt ansible section
if you may wish to prompt the user
for certain input while running ansible playbook.
Let’s get started:
Step-1
Create hosts
file and put it under folder called /Users/Shared/ansible
.
[local] localhost ansible_connection=local ansible_python_interpreter=python [defaults] host_key_checking=false [crunchify] localhost
Step-2
Put your default ansible.cfg
file under the same folder.
[defaults] ansible_ssh_user=crunchify inventory=hosts host_key_checking=false
Step-3
Create file crunchify.yml
file in which we will use vars_prompt
section to get user input.
--- - 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 }}'
Step-4
Run ansible playbook.
bash-3.2$ pwd /Users/Shared/ansible bash-3.2$ ansible-playbook -vvv crunchify.yml
Checkout console output:
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
How to skip user prompt and accept arguments via command line:
bash-3.2$ ansible-playbook -vvv crunchify.yml -e "website='Crunchify.com' country='United States'"
Please let us know if you face any issue running ansible script.