FireFighter
FireFighter

Reputation: 7

How to handle expected prompt in ansible ios_config module

I am trying to implement couple simple commands on cisco ios devices using Ansible (ios_config module).

Especially, I want to remove user profile, but it requires to answer on a prompt and I am getting timeout error...

I have noticed that there are prompt/answer parameters in ios_command module, but it seems that it is not supported in ios_config module.

Has anyone run into the similar problem?

Ansible Task:

  - name: remove user on remote devices
    ios_config:
      lines:
        - no username testuser
      provider: "{{ provider }}"

Output from Cisco device:

Cisco_Router(config)#no username testuser
This operation will remove all username related configurations with same name.Do you want to continue? [confirm]

Playbook output:

TASK [remove user on remote devices] *************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible.module_utils.connection.ConnectionError: timeout trying to send command: end
fatal: [Cisco_Router]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_3_OlXK/ansible_module_ios_config.py\", line 583, in <module>\n    main()\n  File \"/tmp/ansible_3_OlXK/ansible_module_ios_config.py\", line 512, in main\n    load_config(module, commands)\n  File \"/tmp/ansible_3_OlXK/ansible_modlib.zip/ansible/module_utils/network/ios/ios.py\", line 168, in load_config\n  File \"/tmp/ansible_3_OlXK/ansible_modlib.zip/ansible/module_utils/connection.py\", line 149, in __rpc__\nansible.module_utils.connection.ConnectionError: timeout trying to send command: end\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}

Upvotes: 0

Views: 4617

Answers (3)

Gundalow
Gundalow

Reputation: 83

Starting with Ansible 2.4 there is an ios_user module that can be used to create, edit and remove users.

Removing a specific user with state: absent

- name: set user view/role
  ios_user:
    name: testuser
    state: absent
  provider: "{{ provider }}"

The full documentation and further examples can be found at: https://docs.ansible.com/ansible/latest/modules/ios_user_module.html

_command modules and prompts

The various _command modules, including ios_command support passing prompts.

For example:

  - name: run commands that require answering a prompt
    ios_command:
      commands:
        - command: 'clear counters GigabitEthernet0/1'
          prompt: 'Clear "show interface" counters on this interface \[confirm\]'
          answer: 'y'
        - command: 'clear counters GigabitEthernet0/2'
          prompt: '[confirm]'
          answer: "\r"

See https://docs.ansible.com/ansible/latest/modules/ios_command_module.html for further info.

Upvotes: 3

FireFighter
FireFighter

Reputation: 7

I have tried this as well.

It seems that ios_config module is looking for a hostname(config)# prefix after executing each line. Thats why second line is not processing at all and I got the same notification - timeout.

Upvotes: -1

Baptiste Mille-Mathias
Baptiste Mille-Mathias

Reputation: 2179

the prompt waits for a confirmation it seems so you need to confirm the command with a second line, so you likely have to do that.

  - name: remove user on remote devices
    ios_config:
      lines:
        - no username testuser
        - yes
      provider: "{{ provider }}"

Upvotes: 0

Related Questions