How to fail ansible playbook?

How to fail ansible playbook?

In ansible, fail module is avialble to fail ansible playbook with custom message.

fail module is used to exit the progress when certain condition is met.

msg parameter is used to specify the custome message.

msg parameter is mandatory and default value is 'Failed as requested from task.'

Let's see example ansible playbook to skip fail task,

---
- name: Test command
  hosts: localhost
  tasks:
    - name: Set command details
      set_fact:
    	test_cmd: "hostname -f"

    - name: "run command on localmachine"
      command: "{{ test_cmd }}"
       register: out

    - name: fail message
      fail:
    	msg: "failed - invalid machine."
      when: "{{ not 'mymachine' in out.stdout }}"

    - name: Print the msg
      debug:
    	msg: "{{ out.stdout }}"


Output:

$ ansible-playbook test-fail.yml

PLAY [Test command] *********************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [localhost]

TASK [Set command details] **************************************************************************************************************************************************
ok: [localhost]

TASK [run command on localmachine] ******************************************************************************************************************************************
changed: [localhost]

TASK [fail message] *****************************************************************************************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ not 'undercloud' in out.stdout }}
skipping: [localhost]

TASK [Print the msg] ********************************************************************************************************************************************************
ok: [localhost] => {
	"msg": "mymachine-0.local"
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=4	changed=1	unreachable=0	failed=0	skipped=1	rescued=0	ignored=0  

fail with custom message

Let's see example ansible playbook to fail the progress with custom message,

---
- name: Test command
  hosts: localhost
  tasks:
    - name: Set command details
      set_fact:
    	test_cmd: "hostname -f"

    - name: "run command on localmachine"
      command: "{{ test_cmd }}"
      register: out

    - name: fail message
      fail:
    	msg: "failed - invalid machine."
      when: "{{ 'mymachine' in out.stdout }}"

    - name: Print the msg
      debug:
    	msg: "{{ out.stdout }}"


Output:

$ ansible-playbook test-fail.yml

PLAY [Test command] *********************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [localhost]

TASK [Set command details] **************************************************************************************************************************************************
ok: [localhost]

TASK [run command on localmachine] ******************************************************************************************************************************************
changed: [localhost]

TASK [fail message] *****************************************************************************************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ not 'mymachine' in out.stdout }}
fatal: [localhost]: FAILED! => {"changed": false, "msg": "failed - invalid machine."}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=3	changed=1	unreachable=0	failed=1	skipped=0	rescued=0	ignored=0

fail with default message

Let's see example ansible playbook to fail the progress and check the default message,

---
- name: Test command
  hosts: localhost
  tasks:
    - name: Set command details
      set_fact:
    	test_cmd: "hostname -f"

    - name: "run command on localmachine"
      command: "{{ test_cmd }}"
      register: out

    - name: fail message
      fail:
      when: "{{ 'mymachine' in out.stdout }}"

    - name: Print the msg
      debug:
    	msg: "{{ out.stdout }}"


checking whether default message is 'Failed as requested from task' for fail ansible module,

Output:

$ ansible-playbook test-fail.yml

PLAY [Test command] *********************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [localhost]

TASK [Set command details] **************************************************************************************************************************************************
ok: [localhost]

TASK [run command on localmachine] ******************************************************************************************************************************************
changed: [localhost]

TASK [fail message] *****************************************************************************************************************************************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ not 'mymachine' in out.stdout }}
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Failed as requested from task"}

PLAY RECAP ******************************************************************************************************************************************************************
localhost              	: ok=3	changed=1	unreachable=0	failed=1	skipped=0	rescued=0	ignored=0  



Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^