Is there some Ansible equivalent to “failed_when”

2019-06-15 16:22发布

问题:

Looking at the documentation about error handling Ansible error handling

I only see a way to fail the provisioning fail_when, i am wondering if there is any way to do the oposite.

something that looks like this:

- name: ping pong redis
  command: redis-cli ping
  register: command_result
  success_when: "'PONG' in command_result.stderr"

Thanks.

回答1:

I think maybe assert module is what you want.

New in version 1.5

Examples:

- assert: { that: "ansible_os_family != 'RedHat'" }


回答2:

There seem to be no such feature, at least my suggestion on the mailing list remained unresponded:

https://groups.google.com/forum/#!topic/ansible-project/cIaQTmY3ZLE

What might help is to know that failed_when behaves differently to its semantics:

- 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 fail if return code is 0 and there is no 'PONG' in stderr. So it passes if any of the list is False



标签: linux ansible