In my previous Ansible-related posts, both playbooks showed that the user would have to provide variable names and their respective values using the -e or -extra-vars command option. While that works, the user would have to memorize the variable names. If the playbook requires calling multiple roles with variable names, then memorizing all those variable names can be a nightmare. To make it easier for the users, we can leverage vars_prompt. Today, we are exploring how to accept user input in Ansible.
This post contains affiliate links, which means I may earn a commission if you click through and make a purchase. This comes at no additional cost to you. Full disclosure here.
The playbook
Here is an example of an Ansible playbook that utilizes vars_prompt feature.
---
- name: Prompt for variables and run New VSYS checklist
hosts: localhost
gather_facts: false
connection: local
vars_prompt:
- name: panfw
prompt: "Enter the firewall hostname"
private: no
- name: vsysnum
prompt: "Enter the VSYS number"
private: no
- name: vsysname
prompt: "Enter the VSYS name"
private: no
- name: zonename
prompt: "Enter the zone name"
private: no
- name: vlannum
prompt: "Enter the VLAN number"
private: no
- name: aenum
prompt: "Enter the ae number"
private: no
- name: ipadd1
prompt: "Enter the first IP address (ipv4)"
private: no
- name: ipadd2
prompt: "Enter the second IP address (optional, leave blank if none)"
private: no
- name: ipv6add
prompt: "Enter the IPv6 address (optional, leave blank if none)"
private: no
tasks:
- name: Add panfw to the inventory
ansible.builtin.add_host:
name: "{{ panfw }}"
vsysnum: "{{ vsysnum }}"
vsysname: "{{ vsysname }}"
zonename: "{{ zonename }}"
vlannum: "{{ vlannum }}"
aenum: "{{ aenum }}"
ipadd1: "{{ ipadd1 }}"
ipadd2: "{{ ipadd2 }}"
ipv6add: "{{ ipv6add }}"
groups:
- dynamic
- name: New VSYS checklist
hosts: dynamic
gather_facts: false
connection: local
roles:
- role: panos.check-vsys
- role: panos.check-zone
- role: panos.check-vlan
- role: panos.check-ae
- role: panos.check-ipv4
- role: panos.check-ipv6
when: ipv6add is defined and ipv6add != ""Want to learn more about Ansible?
Learn how to automate using Ansible
Breaking it down
Since I covered the first section twice in previous posts, let’s skip that part and move on to the vars_prompt section. Additionally, since this section primarily consists of different prompts and variable names, I will only explain one section in detail.
vars_prompt:
- name: panfw
prompt: "Enter the firewall hostname"
private: noAs the name implies, the vars_prompt interactively prompts the user for input values.
The name: panfw defines the variable name that will store the user’s input.
With the prompt: "Enter the firewall hostname" section, the user will see the message in quotes.
The private: no section means the input will be displayed on the screen as the user types, rather than hidden like a password.
Do you find this content useful?
If so, consider buying me a coffee!
Tasks section
This task dynamically adds a new host to Ansible’s in-memory inventory during playbook execution. Furthermore, we are creating host variables that will only apply to the specified firewall. Subsequently, all the values are grouped into the group called dynamic. This group name will carry all the user input values for the next play.
tasks:
- name: Add panfw to the inventory
ansible.builtin.add_host:
name: "{{ panfw }}"
vsysnum: "{{ vsysnum }}"
vsysname: "{{ vsysname }}"
zonename: "{{ zonename }}"
vlannum: "{{ vlannum }}"
aenum: "{{ aenum }}"
ipadd1: "{{ ipadd1 }}"
ipadd2: "{{ ipadd2 }}"
ipv6add: "{{ ipv6add }}"
groups:
- dynamicNotice that the {{ panfw }} is associated with the name: instead of using panfw as the name. The name: is the parameter that makes it the host identifier. Ansible will then use this in the next play as the target host.
Second play
After gathering values from the user and assigning those values to their respective variables, this play will reference the group called dynamic from the previous play.
- name: New VSYS checklist
hosts: dynamic
gather_facts: false
connection: local
roles:
- role: panos.check-vsys
- role: panos.check-zone
- role: panos.check-vlan
- role: panos.check-ae
- role: panos.check-ipv4
- role: panos.check-ipv6
when: ipv6add is defined and ipv6add != ""As mentioned, Ansible knows to use the value assigned to the name parameter as the target host. Then, pass the other values of the other variables to the Ansible roles where they are applicable.
Running the playbook
As you can see, the -e command option is no longer required to execute the playbook. All that’s necessary is for the user to execute the ansible-playbook command and specify the playbook they intend to run. It eliminates the need to memorize the variable names.
$ ansible-playbook prompt-panos-new-vsys-checklist.yml
Enter the firewall hostname: pa5250
Enter the VSYS number: 3
Enter the VSYS name: andrew
Enter the zone name: trust-andrew
Enter the VLAN number: 600
Enter the ae number: 1
Enter the first IP address (ipv4): 10.1.1.1/24
Enter the second IP address (optional, leave blank if none):
Enter the IPv6 address (optional, leave blank if none):
PLAY [Prompt for variables and run New VSYS checklist] ***************************************************************************************
TASK [Add panfw to the inventory] ************************************************************************************************************
changed: [localhost]
PLAY [New VSYS checklist] ********************************************************************************************************************
<-- Output omitted for brevity -->
PLAY RECAP ***********************************************************************************************************************************
pa5250 : ok=32 changed=0 unreachable=0 failed=0 skipped=20 rescued=0 ignored=0
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 Final words
Using vars_prompt into your Ansible playbooks significantly enhances user experience by eliminating the need to memorize variable names and ensuring a more interactive and user-friendly approach. Furthermore, this feature reduces the likelihood of errors caused by incorrect variable name inputs.
As you continue to develop and refine your Ansible playbooks, consider leveraging this feature for scenarios where user input is required. Moreover, consider adding some tasks to perform input validation. It will streamline your automation workflows and make your playbooks more accessible to users of varying expertise levels. Remember, the goal is to create efficient, maintainable, and user-friendly automation solutions, and vars_prompt is one of the valuable tools in achieving that.
Need help with your network automation?
I can help develop an Ansible playbook based on your needs.
Related posts
Disclosure
AndrewRoderos.com is a participant of a few referral programs, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to company websites.