In this lab, you will learn how to use prompts in an Ansible playbook. Prompts allow you to request input from the user running the playbook, enabling customization without altering the playbook's code.
- Understand how to use
vars_prompt
to gather user input. - Combine prompted and predefined variables in a task.
- Run a playbook and provide input through prompts.
- Basic understanding of YAML syntax.
- Ansible installed on your Ansible machine.
- Access to one or more hosts configured for management with Ansible.
Estimated Time: 30 minutes
-
On your Ansible machine, open a text editor and create a new file named
prompt_play.yml
. -
Add the following content to the file:
--- - hosts: all name: A simple play with prompts gather_facts: no vars_prompt: - name: firstName prompt: Enter your first name private: no default: John - name: lastName prompt: Enter your last name private: no default: Doe vars: yourAge: 18 tasks: - name: Display the input values debug: msg: "Hello {{ firstName }} {{ lastName }}, you are {{ yourAge }} years old."
Explanation:
vars_prompt
: Defines variables to be prompted from the user during playbook execution.vars
: Stores predefined static variables.tasks
: Executes actions, such as displaying messages that combine prompted and predefined variables.
-
Save the
prompt_play.yml
file. -
Execute the playbook using the following command:
ansible-playbook prompt_play.yml
-
When prompted, enter a first name and last name, or press Enter to accept the default values (
John
andDoe
). -
Review the output. The message will include the entered names and the predefined age.
- Modify the default values in the
vars_prompt
section and rerun the playbook. - Add new prompted variables and include them in tasks to explore further functionality.
Congratulations! You have successfully completed a lab on using prompts in Ansible playbooks. This technique is essential for creating interactive playbooks that require user input. Keep exploring to become more proficient with Ansible's versatile features. 👏