Luis Henrique
Luis Henrique

Reputation: 771

Ignore failure when existing strings in the error message

How to ignore a failure when there are some specific strings inside the error message?

stdout_lines :

failed: [lab_ansible] => {"ansible_loop_var": "item", "changed": true, "cmd": "cmd-agent stop lz", "delta": "0:00:02.398303", "end": "2020-07-14 13:17:04.858006", "failed_when_result": true, "item": "lz", "msg": "non-zero return code", "rc": 2, "start": "2020-07-14 13:17:02.459703", "stderr": "", "stderr_lines": [], "stdout": "Processing. Please wait...\nStopping Monitoring Agent for Linux OS ...\nAgent is not running...", "stdout_lines": ["Processing. Please wait...", "Stopping Monitoring Agent for Linux OS ...", "Agent is not running..."]}

Tentative:

- 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...'] )

As mentioned above, the playbook is still closed due to failure

Upvotes: 0

Views: 76

Answers (1)

UnknownBeast
UnknownBeast

Reputation: 979

So as per the requirement, you are checking whether agent is stopped or not. If agent is still running then it should fail the playbook execution otherwise it must pass. I believe that below sample script will be able to help you fulfil your requirement.

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

Upvotes: 1

Related Questions