当使用iosxr_command模块向思科设备发送任意命令并保存输出时,如何在设备返回命令语法错误的情况下保留输出,而不是将任务视为失败?
攻略:
- 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#"}发布于 2021-06-23 23:24:47
正如@mdaniel建议的那样,我尝试在任务中使用ignore_errors: yes,然后我对playbook进行了一些修改,这样当命令出现语法错误时,它将捕获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.failedhttps://stackoverflow.com/questions/68086924
复制相似问题