# Having variables in task names

**URL:** <https://forum.ansible.com/t/having-variables-in-task-names/16237>\
**Category:** Ansible Project\
**Created:** [September 16, 2014, 2:32am UTC](https://forum.ansible.com/t/having-variables-in-task-names/16237 "2014-09-16T02:32:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stephen\_Aylward](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@Stephen\_Aylward](https://forum.ansible.com/u/Stephen_Aylward)\
**Post date:** [September 16, 2014, 2:32am UTC](https://forum.ansible.com/t/having-variables-in-task-names/16237/1 "2014-09-16T02:32:51Z")

</div>

When running my playbook, on tasks that I have tried to name with variables, the variable name is there rather than the value.  
Eg:  
TASK: [weblogic | Create {{ tmp\_dir }}] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

What do I need to change to get the tmp dir value output?  
Thanks

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [September 16, 2014, 12:32pm UTC](https://forum.ansible.com/t/having-variables-in-task-names/16237/2 "2014-09-16T12:32:24Z")

</div>

In order for this to work the variable cannot be a host var or by extension not a group\_var. It must be a variable that is defined globally such as from:

1. vars
2. vars\_files
3. –extra-vars/-e
4. role invocation ( - { role: foo, some\_var: bar } )

There are others, but the important thing is that vars from inventory, host\_vars and group\_vars will not work. The reason for this is that the var could be different for each host and it would make sense, as well as name being printed before the hosts are inspected.

---

<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:** [September 16, 2014, 12:47pm UTC](https://forum.ansible.com/t/having-variables-in-task-names/16237/3 "2014-09-16T12:47:55Z")

</div>

To expand on Matt’s answer, it’s because they aren’t really in scope yet.

If you can imagine having 500 different hosts, each with a different value of “x”, what would be printed for:

“TASK: {{ x }}”

in the header?

Well, we don’t know what the value of x is yet at this point in time, but we do in the tasks below it. But we can’t show 500 values in the header.

So that’s why it is the way is there.

---

<div class="post-metadata">

**Author:** ![cselden](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/cselden/32/4405_2.png) [@cselden](https://forum.ansible.com/u/cselden)\
**Post date:** [December 15, 2014, 5:02pm UTC](https://forum.ansible.com/t/having-variables-in-task-names/16237/4 "2014-12-15T17:02:06Z")

</div>

To expand on this, I have a task with 3 variable references in its name.

- name: Send messages, count {{msgcount}}, size {{msgsize}}, port {{msgport}}  
shell: some\_command

I’ve noticed a couple behaviors with evaluating these:

1. The variable is not evaluated if only defined in the role’s “defaults/main.yml” variables file

2. None of the variables are evaluated if one of them wouldn’t be evaluated

Are these behaviors purposeful or just the result of how and when variables are put in scope? None of these variables are related to hosts (i.e. they are global).  
I know that there is a hierarchy of variables and that they are looked for in a certain order, but where does Ansible look for variables before deciding how to display task names?

Thanks,  
Chip

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [December 15, 2014, 5:44pm UTC](https://forum.ansible.com/t/having-variables-in-task-names/16237/5 "2014-12-15T17:44:23Z")

</div>

short answer: yes, this is expected behaviour

long one:  
#1 happens because of main/defaults.yml has the lowest precedence  
possible and vars there don't get merged in until task execution,  
name: gets set during task definition, this is because host vars are  
allowed to override them. You can use vars/main.yml for 'non  
overridable' higher precedence vars.

#2 happens because jinja2 is given the full name: string to evaluate,  
and it throws a 'variable undefined error' (with default settings)  
which we capture, ignore and return the original string, this is done  
in some cases (like name:) as it should not play/task execution.

Using vars, extra vars or role vars (but not defaults) should give you  
what you need for task names.

---

<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:** [December 15, 2014, 6:14pm UTC](https://forum.ansible.com/t/having-variables-in-task-names/16237/6 "2014-12-15T18:14:13Z")

</div>

Basically think of it as each variable (for a large infrastructure) basically having 500 different values

Ansible can’t tell the variable has only _one_ value, since the variable is “inventory scope” as such, it can’t plug it in, because the task header (that preceeds the next 500 task results) would not apply equally to all hosts.

So it’s a cosmetic thing, but done for a very good (if not slightly confusing!) reason.

---

<div class="post-metadata">

**Author:** ![Rick\_Kasten](https://avatars.discourse-cdn.com/v4/letter/r/43a26b/32.png) [@Rick\_Kasten](https://forum.ansible.com/u/Rick_Kasten)\
**Post date:** [March 12, 2015, 5:20pm UTC](https://forum.ansible.com/t/having-variables-in-task-names/16237/7 "2015-03-12T17:20:48Z")

</div>

These answers don’t make sense to me. Here’s what I’ve got:

# inventory/dse/group\_vars/dse/

package\_versions.yml  
package\_versions:  
app:  
oraclejre\_version: jdk1.7.0\_75

# roles/oraclejre/tasks/main.yml

- name: OracleJRE | Make OracleJRE {{ version }} home directory  
file: path=/usr/local/java-{{ version }} state=directory  
sudo: yes

# roles/oraclejre/vars/main.yml

oraclejre\_versions:  
jdk1.7.0\_75:  
download\_filename: jdk-7u75-linux-x64.tar.gz  
download\_url: “[http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-x64.tar.gz](http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-x64.tar.gz)”

# roles/app/meta/main.yml

dependencies:

- { role: oraclejre, version: “{{ package\_versions.app.oraclejre\_version }}” }

and my output looks like this:

TASK: [oraclejre | OracleJRE | Make OracleJRE {{ package\_versions.app.oraclejre\_version }} home directory] \*\*\*  
changed: [dse02] =\> {“changed”: true, “gid”: 0, “group”: “root”, “mode”: “0755”, “owner”: “root”, “path”: “/usr/local/java-jdk1.7.0\_75”, “secontext”: “unconfined\_u:object\_r:usr\_t:s0”, “size”: 4096, “state”: “directory”, “uid”: 0}

Now it would seem to me that if everyone’s explanations were correct that my task should have failed, because “{{ package\_versions.app.oraclejre\_version }}” should not resolve properly AT ALL for the reasons that everyone has stated. But you can see that it IS resolving properly : “path”: “/usr/local/java- **jdk1.7.0\_75** ”. So why would it resolve properly in the file: field but not the name: field?

---

<div class="post-metadata">

**Author:** ![Krishna1](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@Krishna1](https://forum.ansible.com/u/Krishna1)\
**Post date:** [March 12, 2015, 7:41pm UTC](https://forum.ansible.com/t/having-variables-in-task-names/16237/8 "2015-03-12T19:41:31Z")

</div>

Except this fails to work even with global vars on 1.8.4 (but used to print the value with 1.7.x).

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [March 13, 2015, 5:00am UTC](https://forum.ansible.com/t/having-variables-in-task-names/16237/9 "2015-03-13T05:00:01Z")

</div>

This is a bug: [https://github.com/ansible/ansible/issues/10347](https://github.com/ansible/ansible/issues/10347)

The variable handling changed and the callback isn't getting the  
variables it needs anymore. Defered to v2 because one of the goals of  
v2 is to simplify variable handling which should make this easier to  
fix. (and any fix that was to hit v1 wouldn't be applicable to v2).

-Toshio

---

<div class="post-metadata">

**Author:** ![Toshio\_Kuratomi](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@Toshio\_Kuratomi](https://forum.ansible.com/u/Toshio_Kuratomi)\
**Post date:** [March 13, 2015, 5:08am UTC](https://forum.ansible.com/t/having-variables-in-task-names/16237/10 "2015-03-13T05:08:14Z")

</div>

I'm not sure I see the reason you have for thinking it won't resolve at all...

In general the difference between the name field of a task resolving  
and the same variable within a task is that the name field is  
templated once per task whereas the parts that make up what is  
executed in the task are templated once per host. So something that  
is at the host/group level can't be known at the time that we template  
and print out the name field. But it will be known by the time we  
loop over the task body.

You could think of it sort of like this pseudo code:

global\_vars = resolve\_globals()  
print (template(name\_field, global\_vars))  
for host in hosts:  
&nbsp;&nbsp;&nbsp;&nbsp;host\_and\_group\_vars = resolve\_host\_and\_group(host)  
&nbsp;&nbsp;&nbsp;&nbsp;execute(template(task, global\_vars, host\_and\_group\_vars))

( Any of these specific cases could be because of the bug I linked to  
later in the thread: [https://github.com/ansible/ansible/issues/10347](https://github.com/ansible/ansible/issues/10347)  
but I can't guarantee it... there are certain variables that simply  
aren't available at the time the name field is printed but are  
available once we start looping over the task itself.)

-Toshio
