Abort execution of remaining task if certain condition is failed

◇◆丶佛笑我妖孽 提交于 2019-12-02 18:50:31

You can use assert http://docs.ansible.com/assert_module.html or fail http://docs.ansible.com/fail_module.html

It will go along with something like this

        #check if params are invalid then abort below all tasks.  
        - name: 'check parm is null or invalid' 
          fail: msg="Please enter correct Params"
          when: "param1 is not defined or param2 is not defined " ## whatever condition you want

So in Ansible 2.2+ there is the meta module: http://docs.ansible.com/ansible/latest/meta_module.html

So

meta: end_play

stops the playbook with a non failing status

Ansible >= 2.0 has a block feature that allows you to logically group tasks. This allows you to apply a when to a group of tasks.

The main difference between this and the fail or assert modules is that the task isn't marked as failed; it's just skipped. Whether this is better depends on your use case. For example I have some tasks that write to a log that's later parsed for failures; it's easier to do this if only "real" failure conditions are logged.

Example code:

- block:

    # task 1

    # task 2

    # task 3

  when: "param1 is defined or param2 is defined"

# otherwise output a message
- block:

    debug: msg="Missing params"

  when: "param1 is not defined or param2 is not defined"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!