如果某些条件失败,则中止剩余任务的执行 [英] Abort execution of remaining task if certain condition is failed

查看:33
本文介绍了如果某些条件失败,则中止剩余任务的执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某些条件失败,我想中止剩余任务的执行.并显示正确的错误信息.

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

因此,我想显示错误消息并停止执行 ansible playbook,而不是跳过剩余的任务.

So instead of skipping remaining task I want to show error message and stop execution of ansible playbook.

假设我在命令下运行

$ ansible-playbook playbook.yml -e "param1=value1 param2=value"

我的剧本是这样的:-

playbook.yml:-

---
    - hosts: local
      user: roop
      gather_facts: no

      vars: {param1: "", param2: ""}

      tasks:

        #check whether param1 defined
        - name: 'Check for valid param1'
          shell: echo {{ param1 }}
          register: isValidParam1
          when: param1 !=""

        #check if param1 is null or invalid  
        - name: 'check if param1 is null or invalid' 
          debug: msg="Please enter correct param1"
          when: param1 == ""

        #check whether param2 defined
        - name: 'Check for valid param2'
          shell: echo {{ param2 }}
          register: isValidParam2
          when: param2 != ""

        #check if param2 is null or invalid  
        - name: 'check if param2 is null or invalid' 
          debug: msg="Please enter correct param2"
          when: param2 == ""  


        #check params is valid and defined
        - name: 'Check for valid params'
          shell: echo "param1={{ param1 }} param2={{ param2 }}
          register: validParams
          when: isValidParam1 is defined and isValidParam2 is defined

        #check if params are invalid then abort below all tasks.  
        - name: 'check if validParams is null or invalid' 
          debug: msg="Please enter correct Params"
          when: validParams is not defined    


         # based on validParams, and different value of param1 more than 
          10 task executing.

正如我在上次任务评论中提到的.我正在根据 validParamsparam1 不同的值执行 10 多个任务.我在这里需要什么如果 validParams 未定义,则中止所有执行并显示错误消息.

As I have mentioned in my last task comment. I am executing more than 10 task based on validParams and param1 different value. What I need here if validParams is undefined then abort all execution and show error messages.

有什么有效的方法可以做到这一点.请建议我.

Is there any efficient way to do this . Please suggest me.

推荐答案

可以使用

它会伴随着这样的事情

        #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

这篇关于如果某些条件失败,则中止剩余任务的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆