How to setup a Yes in a Yes or No to repeat and entry

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

Hi, your playbook is mostly finished :smiley:

The last step is, to separate yours into two files.

# loop_vlan_input.yml
---
- name: Prompt for VLAN number
  ansible.builtin.pause:
    prompt: "Please enter a VLAN number"
  register: vlan_number

- name: Prompt for VLAN name
  ansible.builtin.pause:
    prompt: "Please enter a name for VLAN {{ vlan_number.user_input }}"
  register: vlan_name

- name: Add VLAN data to list
  ansible.builtin.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
  ansible.builtin.pause:
    prompt: "Do you want to add another VLAN? (yes/no)"
  register: add_more

- name: Repeat VLAN configuration loop
  ansible.builtin.include_tasks: loop_vlan_input.yml
  when: add_more.user_input | lower == 'yes'

Then include this file from your playbook:

# Add_a_vlan_interactive.yaml
---
- name: Add VLAN and Name
  hosts: Ansible4_Switch
  gather_facts: false
  vars:
    vlan_data: []
  tasks:
    - name: Ask users to input vlans to be created
      ansible.builtin.include_tasks: loop_vlan_input.yml

    - name: Display VLAN data
      ansible.builtin.debug:
        var: vlan_data

Hope this helps :smiley:

1 Like

@kurokobo - thank you so much for your assistance. That worked great and I was able to add multiple variables and then retrieve them to add to my task. Your help is greatly appreciated.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.