# exit if shell variable is not set

**URL:** <https://forum.ansible.com/t/exit-if-shell-variable-is-not-set/19079>\
**Category:** Ansible Project\
**Created:** [August 21, 2015, 8:57am UTC](https://forum.ansible.com/t/exit-if-shell-variable-is-not-set/19079 "2015-08-21T08:57:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![brentgclarklist](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@brentgclarklist](https://forum.ansible.com/u/brentgclarklist)\
**Post date:** [August 21, 2015, 8:57am UTC](https://forum.ansible.com/t/exit-if-shell-variable-is-not-set/19079/1 "2015-08-21T08:57:27Z")

</div>

Good day Guys

Is there a way I can ansible to exit and print a custom error message if a bash / shell variable is not set.

If anyone can assist, it would be appreciated.

Thanks  
Brent

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [August 21, 2015, 2:00pm UTC](https://forum.ansible.com/t/exit-if-shell-variable-is-not-set/19079/2 "2015-08-21T14:00:27Z")

</div>

- hosts: localhost  
&nbsp;&nbsp;gather\_facts: false  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- fail: msg="lola is not set"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: lookup('env', 'lola') == ''

This fails the play  
#\>ansible-playbook play.yml

PLAY \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [fail msg=lola is not set] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [localhost]: FAILED! =\> {"changed": false, "failed": true,  
"msg": "lola is not set"}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=0 changed=0 unreachable=0 failed=1

And this continues play  
#\>lola=is ansible-playbook play.yml

PLAY \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [fail msg=lola is not set] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
skipping: [localhost]

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
localhost : ok=0 changed=0 unreachable=0 failed=0

note that the fail is 'per host'
