# Exit playbook and return an error

**URL:** <https://forum.ansible.com/t/exit-playbook-and-return-an-error/25712>\
**Category:** Ansible Project\
**Created:** [October 10, 2017, 1:35pm UTC](https://forum.ansible.com/t/exit-playbook-and-return-an-error/25712 "2017-10-10T13:35:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colm\_Mcnabola](https://avatars.discourse-cdn.com/v4/letter/c/41988e/32.png) [@Colm\_Mcnabola](https://forum.ansible.com/u/Colm_Mcnabola)\
**Post date:** [October 10, 2017, 1:35pm UTC](https://forum.ansible.com/t/exit-playbook-and-return-an-error/25712/1 "2017-10-10T13:35:24Z")

</div>

Hi,

I want to insert an failure on my playbook where if the hostgroup isnt in the inventory, it fails.

Inventory path is  
inventory/“environment\_name”/inventory

and looks similiar to this

[NodeGroup1]  
node1@node

[NodeGroup2]  
node2@node.  
node3@node.

When running a playbook using --extra vars and declaring the name of the host group like so  
ansible-playbook deploy\_rpm.yml -i environment/testEnv --extra-vars=‘{“hostgroup”: “NodeGroup1”, “var1”: “test-rpm”, “version”: “0.1”}’

It passes as NodeGroup1 is in the inventory

When the hostgroup is named wrong, ‘NodeGroup999’ for example.  
ansible-playbook deploy\_rpm.yml -i environment/testEnv --extra-vars=‘{“hostgroup”: “NodeGroup999”, “var1”: “test-rpm”, “version”: “0.1”}’

It just silently fails but doesnt return an error message or stop.  
I have added this in as a task but it doesnt seem to like it.

- name: “Grep inventory to check hostgroup exists.”  
fail: msg:=“Hostgroup ‘{{ hostgroup }}’ is not defined”  
when:
- inventory\_hostname in groups[‘hostgroup’]|default()

How can i improve this task to stop the playbook and return a failure? Is it best to put the path of the inventory files inside the ‘when’ part of the task?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 10, 2017, 4:34pm UTC](https://forum.ansible.com/t/exit-playbook-and-return-an-error/25712/2 "2017-10-10T16:34:12Z")

</div>

I think you only option is to add the following play on top of your playbook.

- hosts: localhost  
&nbsp;&nbsp;&nbsp;gather\_facts: no  
&nbsp;&nbsp;&nbsp;become: no  
&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- fail:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;msg: Hostgroup '{{ hostgroup }}' is not defined  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: hostgroup not in groups

---

<div class="post-metadata">

**Author:** ![Colm\_Mcnabola](https://avatars.discourse-cdn.com/v4/letter/c/41988e/32.png) [@Colm\_Mcnabola](https://forum.ansible.com/u/Colm_Mcnabola)\
**Post date:** [October 11, 2017, 9:16am UTC](https://forum.ansible.com/t/exit-playbook-and-return-an-error/25712/3 "2017-10-11T09:16:17Z")

</div>

Is this possible to do the script locally and then still be able to connect to hosts in a later stage of the same playbook?

for example

task:

- name: local run of script against inventory

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [October 11, 2017, 9:34am UTC](https://forum.ansible.com/t/exit-playbook-and-return-an-error/25712/4 "2017-10-11T09:34:36Z")

</div>

Yes, the play I posted has "- hosts: localhost", meaning it will run on the Ansible controller.  
If this play fails, it's will not run subsequent plays in the playbook.

The subsequent play can therefore have - hosts: "{{ hostgroup }}"
