# The convenience of variables - and how can I achieve something similar in tasks

**URL:** <https://forum.ansible.com/t/the-convenience-of-variables-and-how-can-i-achieve-something-similar-in-tasks/12409>\
**Category:** Ansible Project\
**Created:** [February 23, 2013, 5:40pm UTC](https://forum.ansible.com/t/the-convenience-of-variables-and-how-can-i-achieve-something-similar-in-tasks/12409 "2013-02-23T17:40:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Asad\_Khan](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@Asad\_Khan](https://forum.ansible.com/u/Asad_Khan)\
**Post date:** [February 23, 2013, 5:40pm UTC](https://forum.ansible.com/t/the-convenience-of-variables-and-how-can-i-achieve-something-similar-in-tasks/12409/1 "2013-02-23T17:40:42Z")

</div>

In playbooks, one can achieve brevity of expression by assigning some complex expression to a variable, then using the variable in subsequent actions.

However, I can’t achieve that in a task - unless I pre-declare all variables the task will use in the calling playbook. With a proliferation of tasks and sub-tasks and multiple playbooks that include tasks at all include levels, this isn’t manageable/scalable.

I was wondering if there’s any way of memorializing (complex) expressions in a task?

My specific experience with this: Some of my tasks require hand-crafting actions via commands/scripts then inspecting their response to determine change - so as to achieve idempotency, and then to proceed with further actions.These boolean expressions can be complex, and often need to be reused through the task. For now I’m just copying/pasting the expression to all subsequent actions that need it, but it would be nice to have a local variable that memorialized the boolean expression.

Thoughts: I can definitely write my own module that encapsulates the command/script work and then everything would be clean and concise, with registered variables. However, coding up a module takes time, and existing Ansible modules (along with command/script) pretty much accomplish most of what’s needed.

---

<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:** [February 23, 2013, 6:01pm UTC](https://forum.ansible.com/t/the-convenience-of-variables-and-how-can-i-achieve-something-similar-in-tasks/12409/2 "2013-02-23T18:01:32Z")

</div>

> However, I can't achieve that in a task - unless I pre-declare all variables  
> the task will use in the calling playbook. With a proliferation of tasks and  
> sub-tasks and multiple playbooks that include tasks at all include levels,  
> this isn't manageable/scalable.

Define your variables in the playbook or inventory and they can be  
used just fine in the task file.

Task files are simple lists, we are not going to make them contain  
anything more than lists of tasks.

---

<div class="post-metadata">

**Author:** ![Asad\_Khan](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@Asad\_Khan](https://forum.ansible.com/u/Asad_Khan)\
**Post date:** [February 23, 2013, 6:52pm UTC](https://forum.ansible.com/t/the-convenience-of-variables-and-how-can-i-achieve-something-similar-in-tasks/12409/3 "2013-02-23T18:52:27Z")

</div>

I wasn’t quite asking for tasks to be changed, was more looking for methodology/best-practices/techniques ( I already have a working prototype as I stated in my post)

Beyond making it work , I need to ensure that multiple people are able to author and maintain tasks and playbooks, and that they’re easily readable and understandable. Task reuse is very important, so I need to ensure that as the DevOps team modifies/enhances tasks, all the different consumers of these tasks (other teams in-house, outsourced data-center personnel) will not break.

Based on your reply, I see 3 choices:

- keep doing what I’m doing, just copy/paste/repeat boolean expressions in a task
- develop custom modules that save us from having to parse stdout from command/script returns
- specify that every task have a dedicated section in group-vars/all, where it keeps it’s “local” variables. A task will be a 3-piece affair: this var section, the task itself, and the task handler (from our other conversation). I’m inclined towards this.

Sorry for being obtuse - my Ansible experience is 6 days old so there’s a lot I don’t know.  
So far I don’t see a blocker, there seem to be good techniques/workarounds for all my concerns.  
As always I have one more question that I will formulate in a bit - thanks for your response 🙂

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [February 25, 2013, 4:00pm UTC](https://forum.ansible.com/t/the-convenience-of-variables-and-how-can-i-achieve-something-similar-in-tasks/12409/4 "2013-02-25T16:00:11Z")

</div>

Are you looking for something like this? (taken from one of my playbooks):

vars:  
&nbsp;&nbsp;&nbsp;- is\_fbsd: $ansible\_system == 'FreeBSD'

tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: asdf  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action: echo hello world  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;only\_if: $is\_fbsd

---

<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:** [February 25, 2013, 4:39pm UTC](https://forum.ansible.com/t/the-convenience-of-variables-and-how-can-i-achieve-something-similar-in-tasks/12409/5 "2013-02-25T16:39:31Z")

</div>

You have to quote 'only\_if' statements around variables. (It's the  
older syntax).

If you change 'only\_if' to "when\_string" you should be able to use  
that verbatim.

---

<div class="post-metadata">

**Author:** ![Asad\_Khan](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@Asad\_Khan](https://forum.ansible.com/u/Asad_Khan)\
**Post date:** [February 25, 2013, 6:34pm UTC](https://forum.ansible.com/t/the-convenience-of-variables-and-how-can-i-achieve-something-similar-in-tasks/12409/6 "2013-02-25T18:34:00Z")

</div>

Yes I understand this, my question was a bit related.

Let’s say I have a task, within which there is an action (this is mostly an illustration, so please don’t focus on why I might want to do specifically this use-case)  
yum list $package  
The stdout is parsed to see if the package is installed on the server.  
i.e. “‘${yumlist\_contents.stdout}’.find(‘Installed Packages’) == -1”  
Based on this answer certain other subsequent actions are taken:

action1:  
only\_if " “‘${yumlist\_contents.stdout}’.find(‘Installed Packages’) == -1”

action2:  
when\_boolean “‘${yumlist\_contents.stdout}’.find(‘Installed Packages’) == -1’ and ‘${nonstandardappquery\_contents.stdout}’.find(‘No such process’) == -1’”  
…

It would be so much more readable (and maintainable) to write:

action1:  
only\_if: $package\_already\_installed

action2:  
when\_boolean: $package\_already\_installed and $service\_stopped

But I don’t see any way to do the above (yet), since this all is in a task.

---

<div class="post-metadata">

**Author:** ![Asad\_Khan](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@Asad\_Khan](https://forum.ansible.com/u/Asad_Khan)\
**Post date:** [February 25, 2013, 6:59pm UTC](https://forum.ansible.com/t/the-convenience-of-variables-and-how-can-i-achieve-something-similar-in-tasks/12409/7 "2013-02-25T18:59:20Z")

</div>

Please ignore my erroneous thinking here - this issue has nothing to do with tasks, it’s just as relevant in a playbook.  
How can one store the evaluated result of an expression for later reference, that’s the crux of it.
