Search code examples
linuxansible

Is there some Ansible equivalent to "failed_when" for success


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.


Solution

  • I think maybe assert module is what you want.

    Examples:

    - name: A single condition can be supplied as string instead of list
      ansible.builtin.assert:
        that: "ansible_os_family != 'RedHat'"
    
    - name: Use yaml multiline strings to ease escaping
      ansible.builtin.assert:
        that:
          - "'foo' in some_command_result.stdout"
          - number_of_the_counting == 3
          - >
            "reject" not in some_command_result.stderr
    
    - name: After version 2.7 both 'msg' and 'fail_msg' can customize failing assertion message
      ansible.builtin.assert:
        that:
          - my_param <= 100
          - my_param >= 0
        fail_msg: "'my_param' must be between 0 and 100"
        success_msg: "'my_param' is between 0 and 100"
    
    - name: Please use 'msg' when ansible version is smaller than 2.7
      ansible.builtin.assert:
        that:
          - my_param <= 100
          - my_param >= 0
        msg: "'my_param' must be between 0 and 100"
    
    - name: Use quiet to avoid verbose output
      ansible.builtin.assert:
        that:
          - my_param <= 100
          - my_param >= 0
        quiet: true