Ansible 当错误消息中存在字符串时忽略失败

Ansible 当错误消息中存在字符串时忽略失败,ansible,try-catch,ignore,.when,Ansible,Try Catch,Ignore,.when,当错误消息中有一些特定字符串时,如何忽略失败 标准输出线: 暂定: 如上所述,playbook仍因故障而关闭,因此根据要求,您正在检查代理是否已停止。如果代理仍在运行,那么它应该使playbook执行失败,否则它必须通过。我相信下面的示例脚本将能够帮助您满足您的要求 --- - hosts: all name: "[ Playbook Example ]" gather_facts: false vars: - failure_message: &q

当错误消息中有一些特定字符串时,如何忽略失败

标准输出线:

暂定:


如上所述,playbook仍因故障而关闭,因此根据要求,您正在检查代理是否已停止。如果代理仍在运行,那么它应该使playbook执行失败,否则它必须通过。我相信下面的示例脚本将能够帮助您满足您的要求

---
- hosts: all
  name: "[ Playbook Example ]"
  gather_facts: false
  vars:
       - failure_message: "Agent is not running"
  tasks:
          - name: "[ Verify Failure Message ]"
            command: "echo The failure message is {{ failure_message }}"
            register: failure_reg
            failed_when:
               - "'Agent is not running' not in failure_reg.stdout"
               - "'Agent stopped' not in failure_reg.stdout"

          - debug:
                  msg: "{{ failure_reg.stdout }}"
- name: "Stopping agent"
  shell: cmd-agent stop lz
  register: stopped_lz
  failed_when: ( stopped_lz.stdout_lines not in ['Agent is not running...', 'Agent stopped...'] )
---
- hosts: all
  name: "[ Playbook Example ]"
  gather_facts: false
  vars:
       - failure_message: "Agent is not running"
  tasks:
          - name: "[ Verify Failure Message ]"
            command: "echo The failure message is {{ failure_message }}"
            register: failure_reg
            failed_when:
               - "'Agent is not running' not in failure_reg.stdout"
               - "'Agent stopped' not in failure_reg.stdout"

          - debug:
                  msg: "{{ failure_reg.stdout }}"