Ansible: past when simulator

Ansible provides a module failed_whenthat allows users to specify specific failure conditions for their tasks, for example. a specific string is found in stdout or stderr.

I am trying to do the opposite: I would like my tasks to not crash if any of the rowset is found in stdout or stderr. In other words, I would like to bring something closer to the functionality of the module passed_when.

  • I still want it to go fine when the return code is 0.
  • But if it fails (rc! = 0), then it must first check for a row.
  • those. if some string is found, it passes no matter what.

My reasoning is as follows:

There are many reasons why the task is a failure - but some of them, depending on the result, I do not consider as a failure in the current context.

Does anyone have a good idea how this can be achieved?

+4
source share
2 answers

Look at here:

Is there any Ansible equivalent of "failed_when" for success

- name: ping pong redis
  command: redis-cli ping
  register: command_result
  failed_when: 
    - "'PONG' not in command_result.stderr"
    - "command_result.rc != 0"
  • will not work if the return code is 0 and there is no "PONG" in stderr.
  • will not work if stderr has "PONG".

So, it passes if any of the list is False

+9
source

Your original question was formulated as follows (using logical logic to simplify it):

Executes a command if a rowset is found in stdout or stderr

Rephrase your logic:

, stdout stderr. , failed_when. :

---
- name: Test failed_when as succeed_if
  hosts: localhost
  connection: local
  gather_facts: no

  tasks:
    - name: "'succeed_if' set of strings in stdout"
      command: /bin/echo succeed1
      register: command_result
      failed_when: "command_result.stdout not in ['succeed1',]"

    - name: "'succeed_if' set of strings in stdout (multiple values)"
      command: /bin/echo succeed2
      register: command_result
      failed_when: "command_result.stdout not in ['succeed1', 'succeed2']"

    - name: "'succeed_if' set of strings in stderr (multiple values)"
      shell: ">&2 /bin/echo succeed2 "
      register: command_result
      failed_when: "command_result.stderr not in ['succeed1', 'succeed2']"

    - name: "'succeed_if' set of strings in stderr (multiple values) or rc != 0"
      shell: ">&2 /bin/echo succeed2; /bin/false"
      register: command_result
      failed_when: "command_result.stderr not in ['succeed1', 'succeed2'] and command_result.rc != 0"

# vim: set ts=2 sts=2 fenc=utf-8 expandtab list:

, , , , , Jinja2 Expressions

+2

Source: https://habr.com/ru/post/1570431/


All Articles