# when\_string and only\_if don't seem to be expanding variables

**URL:** <https://forum.ansible.com/t/when-string-and-only-if-dont-seem-to-be-expanding-variables/13793>\
**Category:** Ansible Project\
**Tags:** rhel\
**Created:** [October 28, 2013, 10:25pm UTC](https://forum.ansible.com/t/when-string-and-only-if-dont-seem-to-be-expanding-variables/13793 "2013-10-28T22:25:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Martin](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@Andrew\_Martin](https://forum.ansible.com/u/Andrew_Martin)\
**Post date:** [October 28, 2013, 10:25pm UTC](https://forum.ansible.com/t/when-string-and-only-if-dont-seem-to-be-expanding-variables/13793/1 "2013-10-28T22:25:39Z")

</div>

Hello,

I’m working on a playbook to install several packages and would like to add logic to handle installing to both Debian-based and RHEL-based distros. To this end, I’ve attempted to use “when\_string” and “only\_if” to check facts gathered from the servers to determine if apt or yum should be used. However, whenever I add when\_string or only\_if (separately, not at the same time) to the tasks below, ansible skips these tasks. I’ve tried running ansible-playbook with -vvvv to get more verbose output, but the only output I am able to see is that it skips the task:

TASK: [Install required packages (apt)] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

skipping: [ubuntuserver]

Here is the playbook:

---

<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:** [October 30, 2013, 1:48am UTC](https://forum.ansible.com/t/when-string-and-only-if-dont-seem-to-be-expanding-variables/13793/2 "2013-10-30T01:48:30Z")

</div>

So, when\_string and only\_if are hopelessly old and deprecated.

The answer is to use “when:” everywhere, and that’s why it’s been the only thing documented in the docs for a couple of releases.

---

<div class="post-metadata">

**Author:** ![Andrew\_Martin](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@Andrew\_Martin](https://forum.ansible.com/u/Andrew_Martin)\
**Post date:** [October 30, 2013, 11:12pm UTC](https://forum.ansible.com/t/when-string-and-only-if-dont-seem-to-be-expanding-variables/13793/3 "2013-10-30T23:12:45Z")

</div>

Michael,

Thanks, I was able to get it working with “when:”. On a related note, are all variables within “when:” referred to without a preceding $ ?

Thanks,

Andrew

---

<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:** [October 31, 2013, 1:48am UTC](https://forum.ansible.com/t/when-string-and-only-if-dont-seem-to-be-expanding-variables/13793/4 "2013-10-31T01:48:30Z")

</div>

“On a related note, are all variables within “when:” referred to without a preceding $ ?”

Yeah, the template stuff is unnecessary because we actually evaluate the whole “when” line using the template engine internally.

The same goes true for “with\_items” and the other “with\_” statements.

---

<div class="post-metadata">

**Author:** ![Andrew\_Martin](https://avatars.discourse-cdn.com/v4/letter/a/77aa72/32.png) [@Andrew\_Martin](https://forum.ansible.com/u/Andrew_Martin)\
**Post date:** [October 31, 2013, 2:07am UTC](https://forum.ansible.com/t/when-string-and-only-if-dont-seem-to-be-expanding-variables/13793/5 "2013-10-31T02:07:14Z")

</div>

Thanks for the clarification.
