# Is there a 1.2 equivilant of "when\_set: $var"?

**URL:** <https://forum.ansible.com/t/is-there-a-1-2-equivilant-of-when-set-var/12751>\
**Category:** Ansible Project\
**Created:** [May 2, 2013, 2:05am UTC](https://forum.ansible.com/t/is-there-a-1-2-equivilant-of-when-set-var/12751 "2013-05-02T02:05:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@John\_Jarvis](https://forum.ansible.com/u/John_Jarvis)\
**Post date:** [May 2, 2013, 2:05am UTC](https://forum.ansible.com/t/is-there-a-1-2-equivilant-of-when-set-var/12751/1 "2013-05-02T02:05:31Z")

</div>

I see this thread comes up in google: [https://github.com/ansible/ansible/issues/2699](https://github.com/ansible/ansible/issues/2699)  
I’m finding that I often want to test if a variable is set before I run a task. Specifically I’m running into situations where I load variables conditionally like:

var\_files:

- path/to/{{ environment}}/vars.yml

Where downstream tasks operate on lists defined in vars.yml that may or may not exist depending on the environment.

-John

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [May 2, 2013, 2:19am UTC](https://forum.ansible.com/t/is-there-a-1-2-equivilant-of-when-set-var/12751/2 "2013-05-02T02:19:10Z")

</div>

Hi John,

I use the following idiom all the time:

- name: maybe do this is foo is set

shell: echo ‘Hazzah!’

when: foo is defined

K

---

<div class="post-metadata">

**Author:** ![John\_Jarvis](https://avatars.discourse-cdn.com/v4/letter/j/e47774/32.png) [@John\_Jarvis](https://forum.ansible.com/u/John_Jarvis)\
**Post date:** [May 2, 2013, 3:44am UTC](https://forum.ansible.com/t/is-there-a-1-2-equivilant-of-when-set-var/12751/3 "2013-05-02T03:44:37Z")

</div>

That’s perfect, thanks! -John

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 2, 2013, 12:01pm UTC](https://forum.ansible.com/t/is-there-a-1-2-equivilant-of-when-set-var/12751/4 "2013-05-02T12:01:08Z")

</div>

We should put this in the docs under ‘when’ in 1.2.

Nice.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [May 2, 2013, 12:01pm UTC](https://forum.ansible.com/t/is-there-a-1-2-equivilant-of-when-set-var/12751/5 "2013-05-02T12:01:42Z")

</div>

BTW, it will work _here_ but you should be careful naming variables named ‘environment’, as environment is set to the values of ‘environment:’ later on.

It’s documented as reserved.

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [May 2, 2013, 11:02pm UTC](https://forum.ansible.com/t/is-there-a-1-2-equivilant-of-when-set-var/12751/6 "2013-05-02T23:02:54Z")

</div>

Done 🙂 Pull request submitted.

K
