# environment variable and conditional execution

**URL:** <https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568>\
**Category:** Ansible Project\
**Created:** [March 24, 2013, 12:47am UTC](https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568 "2013-03-24T00:47:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [March 24, 2013, 12:47am UTC](https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568/1 "2013-03-24T00:47:31Z")

</div>

Is conditional execution supposed to work with the environment variable macro?

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: my test  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: /bin/true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when\_string: $ENV(dropdb) == 'yes'

This never works. I even tried to add:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dropdb: $ENV(dropdb)

and replace the comparison with ${dropdb}, but same thing, it doesn't work.

---

<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:** [March 24, 2013, 2:19am UTC](https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568/2 "2013-03-24T02:19:14Z")

</div>

Are you using Ansible 1.1?

Please show output.

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [March 24, 2013, 2:54am UTC](https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568/3 "2013-03-24T02:54:46Z")

</div>

ansible-playbook 1.1 (20130322 5485451ddb) last updated 2013/03/22 23:06:08 (GMT -600)

---

<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:** [March 24, 2013, 3:27pm UTC](https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568/4 "2013-03-24T15:27:00Z")

</div>

Playbook output also?

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [March 24, 2013, 4:09pm UTC](https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568/5 "2013-03-24T16:09:12Z")

</div>

$ ansible-playbook --version  
ansible-playbook 1.1 (devel 934029842e) last updated 2013/03/22 20:52:54 (GMT -600)

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [March 24, 2013, 4:42pm UTC](https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568/6 "2013-03-24T16:42:21Z")

</div>

This looks like it was working as intended.

In your first message, it prints that the string value says "yes"

In the second, it skips the task, according to the conditional.

Can you help me grok what you think the problem here is?

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [March 24, 2013, 5:20pm UTC](https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568/7 "2013-03-24T17:20:13Z")

</div>

The condition is:  
when\_string: $ENV(dropdb) == 'yes'

Which, from what I understand, means it should be executed if the varaible is 'yes'. But even when $ENV(dropdb) is yes, as shown by the debug msg, it still gets skipped.

Also, it does not matter what $dropdb is. I can do any of those before I run the playbook, the debug msg is always correct and what I'd expect to see, but the behaviour is always the same, the task (/bin/true) NEVER gets run:

export dropdb=yes  
export dropdb='yes'  
export dropdb=no  
unset dropdb

---

<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:** [March 24, 2013, 7:09pm UTC](https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568/8 "2013-03-24T19:09:52Z")

</div>

Try '$ENV(dropdb)' ?

---

<div class="post-metadata">

**Author:** ![Yves\_Dorfsman](https://avatars.discourse-cdn.com/v4/letter/y/9de0a6/32.png) [@Yves\_Dorfsman](https://forum.ansible.com/u/Yves_Dorfsman)\
**Post date:** [March 24, 2013, 9:25pm UTC](https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568/9 "2013-03-24T21:25:04Z")

</div>

ERROR: Syntax Error while loading YAML script, x.yaml  
Note: The error may actually appear before this position: line 13, column 35

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: /bin/true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when\_string: '$ENV(dropdb)' == 'yes'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [March 24, 2013, 11:56pm UTC](https://forum.ansible.com/t/environment-variable-and-conditional-execution/12568/10 "2013-03-24T23:56:03Z")

</div>

Yep, if you start a value with a quote, you have to quote the whole line.

It's unfortunate, but true.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when\_string: "'$ENV(dropdb)' == 'yes'"

Not saying this would actually work.

I'm willing to test this, if there's a ticket for it.
