# checking if a dynamically selected variable is defined?

**URL:** <https://forum.ansible.com/t/checking-if-a-dynamically-selected-variable-is-defined/16074>\
**Category:** Ansible Project\
**Created:** [August 29, 2014, 4:30pm UTC](https://forum.ansible.com/t/checking-if-a-dynamically-selected-variable-is-defined/16074 "2014-08-29T16:30:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dylan\_Martin2](https://avatars.discourse-cdn.com/v4/letter/d/a87d85/32.png) [@Dylan\_Martin2](https://forum.ansible.com/u/Dylan_Martin2)\
**Post date:** [August 29, 2014, 4:30pm UTC](https://forum.ansible.com/t/checking-if-a-dynamically-selected-variable-is-defined/16074/1 "2014-08-29T16:30:48Z")

</div>

Hi all.

I often write plays that require extra vars. eg “ansible-playbook user -e user=bob”

In that play, I’ll start off with

- fail: msg=“You forgot to set the user variable”

when: user is not defined or user == “”

And I was thinking it might be nice to write an external, reusable play that would be called like this:

- include: check\_defined.yaml checkvar=user

and look something like this

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [August 30, 2014, 1:25pm UTC](https://forum.ansible.com/t/checking-if-a-dynamically-selected-variable-is-defined/16074/2 "2014-08-30T13:25:07Z")

</div>

Here’s a quicker one-liner:

- assert: { that: ‘foo is defined’ }

Or any other conditional, etc. ‘that’ can also take a list of conditionals.
