Abort execution of remaining task if certain condition is failed

前端 未结 3 1293
悲&欢浪女
悲&欢浪女 2020-12-23 20:04

I want to abort execution of remaining task if certain condition is failed. and display proper error message.

So instead of skipping remaining task I want to show er

相关标签:
3条回答
  • 2020-12-23 20:21

    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"
    
    0 讨论(0)
  • 2020-12-23 20:22

    You can use

    • assert https://docs.ansible.com/ansible/latest/collections/ansible/builtin/assert_module.html
    • or fail https://docs.ansible.com/ansible/latest/collections/ansible/builtin/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
    
    0 讨论(0)
  • 2020-12-23 20:38

    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

    0 讨论(0)
提交回复
热议问题