如何在ansible ios_config模块中处理预期的提示

时间:2018-05-30 09:29:04

标签: networking ansible cisco

我正在尝试使用Ansible(ios_config模块)在cisco ios设备上实现几个简单的命令。

特别是,我想删除用户个人资料,但需要在提示时回答,我收到超时错误...

我注意到ios_command模块中有提示/应答参数,但似乎ios_config模块中不支持它。

有没有人遇到过类似的问题?

Ansible Task:

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

来自Cisco设备的输出:

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

Playbook输出:

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}

3 个答案:

答案 0 :(得分:2)

从Ansible 2.4开始,有一个ios_user模块可用于创建,编辑和删除用户。

使用state: absent删除特定用户

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

完整的文档和更多示例可在以下网址找到:https://docs.ansible.com/ansible/latest/modules/ios_user_module.html

_command模块和提示

各种_command模块,包括ios_command,都支持传递提示。

例如:

  - 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"

有关更多信息,请参见https://docs.ansible.com/ansible/latest/modules/ios_command_module.html

答案 1 :(得分:0)

提示等待确认,所以你需要用第二行确认命令,所以你可能不得不这样做。

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

答案 2 :(得分:-1)

我也试过这个。

在执行每一行之后,似乎ios_config模块正在寻找主机名(config)#前缀。这就是为什么第二行根本没有处理,我得到了相同的通知 - 超时。