# Variable namespacing

**URL:** <https://forum.ansible.com/t/variable-namespacing/24806>\
**Category:** Ansible Project\
**Created:** [June 22, 2017, 11:01pm UTC](https://forum.ansible.com/t/variable-namespacing/24806 "2017-06-22T23:01:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Mildren](https://avatars.discourse-cdn.com/v4/letter/b/cdc98d/32.png) [@Ben\_Mildren](https://forum.ansible.com/u/Ben_Mildren)\
**Post date:** [June 22, 2017, 11:01pm UTC](https://forum.ansible.com/t/variable-namespacing/24806/1 "2017-06-22T23:01:42Z")

</div>

When wanting to namespace variables in a role, is it typical to use a hash, or a prefix? Is there a best practice for this?

e.g.

role\_name\_var\_one: 1  
role\_name\_var\_two: 2  
role\_name\_var\_three: 3

or

role\_name:  
var\_one: 1  
var\_two: 2  
var\_three: 3

---

<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:** [June 27, 2017, 6:32pm UTC](https://forum.ansible.com/t/variable-namespacing/24806/2 "2017-06-27T18:32:27Z")

</div>

> When wanting to namespace variables in a role, is it typical to use a  
> hash, or a prefix? Is there a best practice for this?

There are pros and cons either way. Off the top of my head, using a hash  
lets you iterate over it easily, and IMHO looks nicer; but you can't (as  
far as I know) add elements to it in different places in your inventory.  
If you have a hash of elements that you want defined on all hosts, and you  
then want to add another element to be defined only on a particular group  
of hosts, I don't think there's a way to do that. That's easy with  
prefixes; but then it's harder to loop through the prefix-ed variables.

&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.
