Variable interpolation in task name

937 views
Skip to first unread message

Joost Cassee

unread,
May 7, 2014, 4:28:22 PM5/7/14
to ansible...@googlegroups.com
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}}] ****************************** 

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

Regards,
Joost

Slava Semushin

unread,
May 8, 2014, 2:21:52 AM5/8/14
to ansible...@googlegroups.com
2014-05-08 0:28 GMT+04:00 Joost Cassee <jo...@cassee.net>:

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.

--
Slava Semushin

Joost Cassee

unread,
May 8, 2014, 5:43:26 PM5/8/14
to ansible...@googlegroups.com
2014-05-08 8:21 GMT+02:00 Slava Semushin <slava.s...@gmail.com>:
> 2014-05-08 0:28 GMT+04:00 Joost Cassee <jo...@cassee.net>:
>
>> - name: Create A record for domain {{ domain }}
>> [...]
>
> 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

--
Joost Cassee
http://joost.cassee.net
ansible-namevar.zip

Dick Davies

unread,
May 9, 2014, 12:00:23 PM5/9/14
to ansible list
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 received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAEQrH%2Bc-KMEGaCV%2B3LLo3qKOSvztb78AMn%2BhY8gghzhmJBbuyQ%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
May 9, 2014, 2:04:29 PM5/9/14
to ansible...@googlegroups.com
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.​

Michael DeHaan

unread,
May 9, 2014, 3:11:04 PM5/9/14
to ansible...@googlegroups.com
What Brian said.  This is not a bug.




On Fri, May 9, 2014 at 2:04 PM, Brian Coca <bria...@gmail.com> wrote:
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.​

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Michael DeHaan

unread,
May 9, 2014, 3:12:00 PM5/9/14
to ansible...@googlegroups.com
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.


Joost Cassee

unread,
May 10, 2014, 4:04:16 PM5/10/14
to ansible...@googlegroups.com
2014-05-09 21:12 GMT+02:00 Michael DeHaan <mic...@ansible.com>:
> 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!

Strahinja Kustudić

unread,
May 11, 2014, 6:26:11 AM5/11/14
to ansible...@googlegroups.com
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.

Brian Coca

unread,
May 11, 2014, 9:04:14 AM5/11/14
to ansible...@googlegroups.com

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

Brian Coca

Strahinja Kustudić

unread,
May 11, 2014, 12:38:39 PM5/11/14
to ansible...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages