Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abort execution of remaining task if certain condition is failed

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 error message and stop execution of ansible playbook.

Lets say I am running below command

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

My playbook look like this:-

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. 

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.

like image 616
Roopendra Avatar asked Mar 31 '14 10:03

Roopendra


People also ask

What happens when one task is failed in playbook?

Handlers and failure If a task notifies a handler but another task fails later in the play, by default the handler does not run on that host, which may leave the host in an unexpected state. For example, a task could update a configuration file and notify a handler to restart some service.

How do you abort an ansible playbook?

You can use ctrl+c if you wish to advance a pause earlier than it is set to expire or if you need to abort a playbook run entirely. To continue early: press ctrl+c and then c . To abort a playbook: press ctrl+c and then a . Or just a straight - fail: if you will certainly not want to continue.

How can Error Handling be done in ansible?

Ansible normally has defaults that make sure to check the return codes of commands and modules and it fails fast – forcing an error to be dealt with unless you decide otherwise. Sometimes a command that returns different than 0 isn't an error.

What keyword makes ansible not exit on error and continue play execution?

If you use ignore_errors, ansible will continue attempting to run tasks against that host.


1 Answers

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 
like image 129
DomaNitro Avatar answered Sep 21 '22 07:09

DomaNitro