【问题标题】:Ansible iosxr_command save the output even when command syntax error即使命令语法错误,Ansible iosxr_command 也会保存输出
【发布时间】:2021-06-22 15:55:55
【问题描述】:

当使用 iosxr_command 模块向 cisco 设备发送任意命令并保存输出时,即使设备返回命令语法错误,如何保持输出,而不是认为任务失败?

剧本:

- name: run command and save output
  iosxr_command:
    commands: 
      - "invalid syntax command"
    register: output

预期保存的输出:

RP/0/RSP0/CPU0:IOSXR_ROUTER#invalid syntax command
                              ^
% Invalid input detected at '^' marker.

代替:

TASK [run command and save output] 
*****************************************************************************************************************
failed: [IOSXR_ROUTER] (item=invalid syntax command) => {"changed": false, "item": "invalid command", 
"msg": "invalid syntax command\r\n\r      
^\r\n% Invalid input detected at '^' marker.\r\nRP/0/RSP0/CPU0:IOSXR_ROUTER#"}

【问题讨论】:

  • @mdaniel 我已经尝试过了,当命令执行失败时,我仍然需要编辑剧本以使用 output.msg 的内容而不是 output.stdout,然后我得到了我想要的。谢谢你:)

标签: ansible cisco


【解决方案1】:

正如@mdaniel 建议的那样,我尝试在任务中使用 ignore_errors: yes 然后我稍微修改剧本,以便在命令出现语法错误时捕获 output.msg 而不是 output.stdout。像这样:

- name: run show command
  iosxr_command:
    commands: 
      - invalid command
  register: output
  ignore_errors: yes

- name: Check previous task fail
  set_fact:
    captured_data: "{{output.msg}}"
  when: output.failed

- name: Check previous task succeed
  set_fact:
    captured_data: "{{output.stdout[0] | replace('\\n', '\n')}}"
  when: not output.failed

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-10
    • 2020-11-15
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-09
    相关资源
    最近更新 更多