Hi All,
I’m new to Ansible and am learning as I go. I’m trying to create a playbook that will prompt the user to add a new VLAN number and Name to be populated on all my switches (in an inventory file). It then prompts them if they want to add another VLAN. If they type “yes” it should prompt them with the questions again and add. Is it possible to do this in Ansible? I am struggling with the looping feature. I can get it to add a single update, but not multiple.
Current playbook:
-
name: Add VLAN and Name
hosts: Ansible4_Switch
gather_facts: no
vars:
vlan_data:tasks:
-
name: VLAN configuration loop
block:-
name: Prompt for VLAN number
pause:
prompt: “Please enter a VLAN number”
register: vlan_number -
name: Prompt for VLAN name
pause:
prompt: “Please enter a name for VLAN {{ vlan_number.user_input }}”
register: vlan_name -
name: Add VLAN data to list
set_fact:
vlan_data: “{{ vlan_data + [{‘vlan_id’: vlan_number.user_input, ‘name’: vlan_name.user_input}] }}” -
name: Ask if user wants to add another VLAN
pause:
prompt: “Do you want to add another VLAN? (yes/no)”
register: add_more -
name: Repeat VLAN configuration loop
when: add_more.user_input | lower == ‘yes’
include_tasks: Add_a_vlan_interactive.yaml
-
-
name: Configure VLANs on Cisco switches
ios_vlans:
config: “{{ vlan_data }}”
state: replaced
-