# silly trick: you can use variables in the "- name:" part of a playbook

**URL:** <https://forum.ansible.com/t/silly-trick-you-can-use-variables-in-the-name-part-of-a-playbook/22257>\
**Category:** Ansible Project\
**Created:** [August 10, 2016, 5:02am UTC](https://forum.ansible.com/t/silly-trick-you-can-use-variables-in-the-name-part-of-a-playbook/22257 "2016-08-10T05:02:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixel\_fairy](https://avatars.discourse-cdn.com/v4/letter/p/6f9a4e/32.png) [@pixel\_fairy](https://forum.ansible.com/u/pixel_fairy)\
**Post date:** [August 10, 2016, 5:02am UTC](https://forum.ansible.com/t/silly-trick-you-can-use-variables-in-the-name-part-of-a-playbook/22257/1 "2016-08-10T05:02:16Z")

</div>

i realize itfits how yaml and jinja2 works so probably obvious to many of you. never thought about it until after doing this and seeing the result. could make for more readable output.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 10, 2016, 2:19pm UTC](https://forum.ansible.com/t/silly-trick-you-can-use-variables-in-the-name-part-of-a-playbook/22257/2 "2016-08-10T14:19:02Z")

</div>

example please, didn’t get it. Thanks

---

<div class="post-metadata">

**Author:** ![Josh\_Smift1](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@Josh\_Smift1](https://forum.ansible.com/u/Josh_Smift1)\
**Post date:** [August 10, 2016, 2:44pm UTC](https://forum.ansible.com/t/silly-trick-you-can-use-variables-in-the-name-part-of-a-playbook/22257/3 "2016-08-10T14:44:17Z")

</div>

> i realize itfits how yaml and jinja2 works so probably obvious to many of  
> you. never thought about it until after doing this and seeing the result.  
> could make for more readable output.

> example please, didn't get it. Thanks

Something like this:

&nbsp;&nbsp;- name: "{{ action }} the hosts"  
&nbsp;&nbsp;&nbsp;&nbsp;hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: "{{ action }} a host"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug: var=action

Then you can run the playbook with '--extra-vars action=smite' or  
whatever, and you'll get output like

&nbsp;&nbsp;PLAY [smite the hosts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

&nbsp;&nbsp;TASK [setup] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;ok: [host-01]

&nbsp;&nbsp;TASK [smite a host] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;ok: [host-01] =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"action": "smite"  
&nbsp;&nbsp;}

As in that example, this also works in tasks, although not with things  
like with\_items, e.g.

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- name: "{{ item }} a host"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;debug: var=item  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: [bump, set, spike]

says

&nbsp;&nbsp;TASK [{{ item }} a host] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
&nbsp;&nbsp;ok: [dom-use-util-01] =\> (item=bump) =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"item": "bump"  
&nbsp;&nbsp;}  
&nbsp;&nbsp;ok: [dom-use-util-01] =\> (item=set) =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"item": "set"  
&nbsp;&nbsp;}  
&nbsp;&nbsp;ok: [dom-use-util-01] =\> (item=spike) =\> {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"item": "spike"  
&nbsp;&nbsp;}

because the three items all get processed within one task.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-Josh (jbs@care.com)

(apologies for the automatic corporate disclaimer that follows)

This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.
