cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
430
Views
2
Helpful
2
Replies

how to deal with prompts when using ios_config ansible module

pcook10
Level 1
Level 1

Trying to use ios_config module, with 'src' parameter pointing to a set of dynamically generated lines of configuration. We put these all together and send it to a final module which uses ios_config to try and configure the device. For one such scenario on one platform, i'm getting following prompt which is causing the workflow to time out;
Static entry in use, do you want to delete child entries? [no]:
(adding a clear ip nat trans forced command before the remove command doesn't clear the prompt appearing)
From what i can see, there doesn't seem to be a way to handle prompts within the ios_config module, and the other suggestions ive come across, which are to use cli_command or ios_command modules don't seem to allow me to specify a 'src' file, instead require the commands to be defined within the task. Is there a workaround? 




1 Accepted Solution

Accepted Solutions

Handling interactive prompts like "Static entry in use, do you want to delete child entries? [no]:" can be tricky with modules like ios_config since it doesn’t have a built-in way to respond to such prompts. For this scenario, I suggest a different approach using a combination of cli_command and ios_config modules.

Solution Approach

  1. Use the cli_command Module: Use the cli_command module to perform the specific command that triggers the prompt and pass the required input using the wait_for and prompt options. This allows us to simulate a manual response to the interactive prompt.

  2. Leverage ignore_errors: You can use ignore_errors on this specific command task so that the overall playbook execution is not halted if the command fails or the prompt appears unexpectedly.

  3. Continue with ios_config: After handling the prompt, continue using ios_config for the rest of your configuration commands.

Here’s an example of how this could be structured in a playbook:

 

---
- name: Handle Interactive Prompt and Apply Configuration
  hosts: switches
  gather_facts: no
  connection: network_cli
  tasks:
    - name: Remove static NAT entry with prompt handling
      cli_command:
        command: "no ip nat inside source static tcp 10.1.1.1 80 192.168.1.1 80"
        prompt: "Static entry in use, do you want to delete child entries?"
        answer: "yes"
        wait_for: 5
      register: nat_removal_result
      ignore_errors: yes

    - name: Validate NAT removal
      debug:
        msg: "{{ nat_removal_result }}"

    - name: Apply configuration from file using ios_config
      ios_config:
        src: "/path/to/generated/config"
      when: nat_removal_result is succeeded

 

Explanation

  1. cli_command:

    • This module is used to send the command that triggers the prompt (no ip nat inside...).
    • The prompt and answer fields are used to respond to the interactive prompt with "yes".
    • The wait_for field ensures the task waits for the specified time to confirm the command’s response.
  2. register and ignore_errors:

    • The output is registered to a variable nat_removal_result and errors are ignored.
    • You can inspect the output using a debug statement for troubleshooting.
  3. ios_config after Prompt Handling:

    • Once the prompt is handled, proceed to use ios_config with your dynamic configuration file.

View solution in original post

2 Replies 2

@pcook10 so what you are seeing is kinda of expected, ios_config module in ansible is designed for bulk configuration changes but lacks built-in prompt handling sadly. Check out https://docs.ansible.com/ansible/latest/collections/ansible/netcommon/cli_command_module.html he module is your best bet for interacting with prompts as you can use a loop to feed it commands from your dynamically generated configuration file.

 
Hope this helps.
Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

Handling interactive prompts like "Static entry in use, do you want to delete child entries? [no]:" can be tricky with modules like ios_config since it doesn’t have a built-in way to respond to such prompts. For this scenario, I suggest a different approach using a combination of cli_command and ios_config modules.

Solution Approach

  1. Use the cli_command Module: Use the cli_command module to perform the specific command that triggers the prompt and pass the required input using the wait_for and prompt options. This allows us to simulate a manual response to the interactive prompt.

  2. Leverage ignore_errors: You can use ignore_errors on this specific command task so that the overall playbook execution is not halted if the command fails or the prompt appears unexpectedly.

  3. Continue with ios_config: After handling the prompt, continue using ios_config for the rest of your configuration commands.

Here’s an example of how this could be structured in a playbook:

 

---
- name: Handle Interactive Prompt and Apply Configuration
  hosts: switches
  gather_facts: no
  connection: network_cli
  tasks:
    - name: Remove static NAT entry with prompt handling
      cli_command:
        command: "no ip nat inside source static tcp 10.1.1.1 80 192.168.1.1 80"
        prompt: "Static entry in use, do you want to delete child entries?"
        answer: "yes"
        wait_for: 5
      register: nat_removal_result
      ignore_errors: yes

    - name: Validate NAT removal
      debug:
        msg: "{{ nat_removal_result }}"

    - name: Apply configuration from file using ios_config
      ios_config:
        src: "/path/to/generated/config"
      when: nat_removal_result is succeeded

 

Explanation

  1. cli_command:

    • This module is used to send the command that triggers the prompt (no ip nat inside...).
    • The prompt and answer fields are used to respond to the interactive prompt with "yes".
    • The wait_for field ensures the task waits for the specified time to confirm the command’s response.
  2. register and ignore_errors:

    • The output is registered to a variable nat_removal_result and errors are ignored.
    • You can inspect the output using a debug statement for troubleshooting.
  3. ios_config after Prompt Handling:

    • Once the prompt is handled, proceed to use ios_config with your dynamic configuration file.