Variable interpolation in task name

Hi,

I have a role that updates DNS records. It contains a number of tasks such as:

  • name: Create A record
    route53: command=create zone=example.com record={{ domain }} type=A value={{ ip }}

Other roles (that represent server profiles) depend on this role like this:

  • role: dns
    domain: “{{ website_domain }}”
    ip: “{{ ansible_default_ipv4.address }}”

The problem is that the role execution does not give a lot of information because the name of the task is the same every time. Of course I could use a debug task, but I thought I would put the domain in the task name, like this:

  • name: Create A record for domain {{ domain }}

Unfortunately, the variable interpolation does not work well, it seems to go only one “level” deep. This is the output:

TASK: [dns | Create A record for {{website_domain}}] ******************************
ok: [web1.example.com]

My question: is this expected behavior? Otherwise I will try and isolate the problem and create an issue.

Regards,
Joost

Hi,

[...]

- name: Create A record for domain {{ domain }}

Unfortunately, the variable interpolation does not work well, it seems to go
only one "level" deep. This is the output:

TASK: [dns | Create A record for {{website_domain}}]
******************************
ok: [web1.example.com]

Have you tried with

- name: Create A record {{domain}}
  route53: command=create zone=example.com record={{ domain }} type=A
value={{ ip }}

?

Because based on output above you used website_domain variable which
isn't available inside role.

I guess I was not clear. The variable website_domain does exist, I was
including only what I thought was the interesting bits. Ansible would
have complained, by the way, if the variable had not existed.

The attached archive contains an isolated example. Run with
"ansible-playbook test.yml -i hosts". Notice how the debug tasks print
the contents of the variables foo and bar, while the task names both
contain "{{ foo }}".

Regards,
Joost

(attachments)

ansible-namevar.zip (1.47 KB)

You mean output like this during a play:

.......
TASK: [base-mongodb | install mongodb RPMs v {{mongo_version}}] ***************
ok: [mongo1] =>
(item=mongodb-org-2.6.0,mongodb-org-server-2.6.0,mongodb-org-shell-2.6.0)
.......

?

Personally I quite like the behaviour, I see it as useful to know what var
is controlling that part of a playbook run - though I know colleagues who
took the opposite view.

you can interpolate variables, just not from every scope, only ones that maintain value across hosts (extra, vars, vars_files, vars_prompts, etc) but not inventory vars.​

What Brian said. This is not a bug.

To explaloate (this has been answered several times before), imagine you are managing 500 hosts and an the inventory variable is different on each one? There’s no way to show a good task header in that case.

The best course of action is to name each task and don’t have variables in the task name, so it prints out a nice description.

To explaloate (this has been answered several times before), imagine you are
managing 500 hosts and an the inventory variable is different on each one?
There's no way to show a good task header in that case.

Of course, that's it. There is no single value.

The best course of action is to name each task and don't have variables in
the task name, so it prints out a nice description.

Well, that was my original problem. I have a generic role that creates
DNS entries, that is depended upon by various other roles, in order
to, well, create DNS entries. So there is not really a nice
description to print out.

It has tasks like:

- name: Create A record for {{ domain }}
  route53: command=create zone={{ dns_zone }}
           record={{ domain }} type=A ttl={{ ttl }} value={{
ansible_default_ipv4.address }}

I was trying to get Ansible to print out the domains that are created
without using the debug task, but I guess I will use debug after all.

By the way, I did search the list for variation on "name variable
interpolation" but did not find anything. Thanks for explaining yet
again!

Regards,
Joost

But, this should be possible to implement into Ansible? I would also like to use any variable which works in a task, to work in the task name. If you don’t have anything against this, I would like to create an issue on github, maybe someone takes it and implements it.

The problem with “any variable” is that hosts share the task name, that is the main reason inventory variables don’t work

Brian Coca

I see what you are saying. Since the tasks run in parallel for multiple servers and if servers have different values for the same variable in the inventory, or host_vars, Ansible wouldn’t know how to name that task. Makes sense.