I cannot get this seemingly simple example to work in Ansible 1.8.3. The variable interpolation does not kick in the task name. All examples I have seen seem to suggest this should work. Given that the variable is defined in the vars section I expected the task name to print the value of the variable. Why doesn't this work?
Even the example from the Ansible documentation seems to not print the variable value.
---
- hosts: 127.0.0.1
gather_facts: no
vars:
vhost: "foo"
tasks:
- name: create a virtual host file for {{ vhost }}
debug: msg="{{ vhost }}"
This results in the following output:
PLAY [127.0.0.1]
**************************************************************
TASK: [create a virtual host file for {{ vhost }}]
****************************
ok: [127.0.0.1] => {
"msg": "foo"
}
PLAY RECAP
********************************************************************
127.0.0.1 : ok=1 changed=0 unreachable=0 failed=0
Update This works with 1.7.2 but does not work with 1.8.3. So either this is a bug or a feature.
Explanation
Whether the variable gets interpolated depends on where it has been declared.
Imagine You have two hosts:
A
andB
.foo
has only per-host values, when Ansible runs the play, it cannot decide which value to use.Source: https://github.com/ansible/ansible/issues/3103#issuecomment-18835432
Hands on playbook
ansible_user
is an inventory variablegreeting
is an invariant variableYou have to surround the string in quotation marks.
From ansible documentation:
I experienced the same problem today in one of my Ansible roles and I noticed something interesting.
When I use the set_fact module before I use the vars in the task name, they actually get translated to their correct values.
In this example I wanted to set the password for a remote user:
Notice that I use the vars
test_user
anduser_password
that I set as facts before.This gives me the following output:
So this solved my problem.
Variables are not resolved inside the
name
. Only inside the actual tasks/conditions etc. the placeholders will be resolved. I guess this is by design. Imagine you have awith_items
loop and use the{{ item }}
in thename
. The tasksname
will only be printed once, but the{{ item }}
would change in every iteration.I see the examples, even the one in the doc you linked to, use variables in the
name
. But that doesn't mean the result would be like you expected it. The docs are community managed. It might be someone just put that line there w/o testing it - or maybe it used to work like that in a previous version of Ansible and the docs have not been updated then. (I'm only using Ansible since about one year). But even though it doesn't work like we wish it would, I'm still using variables in myname
's, just to indicate that the task is based on dynamic parameters. Might be the examples have been written with the same intention.An interesting observation I recently made (Ansible 1.9.4) is, default values are written out in the task name.
When executed, Ansible would show the task title as:
This way you can avoid ugly task names in the output.