# Best practice/way for variables to be used in template?

**URL:** <https://forum.ansible.com/t/best-practice-way-for-variables-to-be-used-in-template/25043>\
**Category:** Ansible Project\
**Created:** [July 21, 2017, 11:28am UTC](https://forum.ansible.com/t/best-practice-way-for-variables-to-be-used-in-template/25043 "2017-07-21T11:28:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike\_Eriksson](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Mike\_Eriksson](https://forum.ansible.com/u/Mike_Eriksson)\
**Post date:** [July 21, 2017, 11:28am UTC](https://forum.ansible.com/t/best-practice-way-for-variables-to-be-used-in-template/25043/1 "2017-07-21T11:28:05Z")

</div>

All,

I’m after some advice here on the best way of getting a template file populated with one or more network ranges based on the host the file is copied onto.

So, basically I’ll have 15+ hosts that will be running a program where one of the configuration files will contain one or more network ranges that varies with the host it is being installed on.

I’m trying to figure out where it is best to store these variables? Would it be in the inventory file or a local variable file for the role I’m creating?

The file looks something like this:

← Cut →

[10.0.0.0/8](http://10.0.0.0/8) Private IP space  
[172.16.0.0/12](http://172.16.0.0/12) Private IP space  
[192.168.0.0/16](http://192.168.0.0/16) Private IP space  
{{ ipranges }}

← Cut →

Where ‘ipranges’ is supposed to be one or more public ranges from the site in question. (I’m not even sure at this stage if I can have ‘multi-value’ variables like this? Ie, one site could have a range of say [1.1.1.1/20](http://1.1.1.1/20) and the other could have two ranges .[2.2.2.2/24](http://2.2.2.2/24) and [3.3.3.3/20](http://3.3.3.3/20) and I’d like them to be on one line each)

Have anyone else done something similar to this and have the time to give me a nudge in the correct direction? I’m sort of changing my mind about the best way of doing this as well as which way would be easiest to document/maintain in the long run?

Thanks in advance, Mike

---

<div class="post-metadata">

**Author:** ![azaghal](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/azaghal/32/4743_2.png) [@azaghal](https://forum.ansible.com/u/azaghal)\
**Post date:** [July 26, 2017, 7:59pm UTC](https://forum.ansible.com/t/best-practice-way-for-variables-to-be-used-in-template/25043/2 "2017-07-26T19:59:11Z")

</div>

Have you considered using host\_vars/group\_vars to define the ranges?  
Since this sounds like rather natural place to set these values at  
(allowing you to more easily reconfigure as needed without touching  
role internals).

Best regards

---

<div class="post-metadata">

**Author:** ![Mike\_Eriksson](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Mike\_Eriksson](https://forum.ansible.com/u/Mike_Eriksson)\
**Post date:** [July 26, 2017, 8:03pm UTC](https://forum.ansible.com/t/best-practice-way-for-variables-to-be-used-in-template/25043/3 "2017-07-26T20:03:02Z")

</div>

Hi Branko,

Yeah, I stumbled onto host\_vars during my trial and error approach so that’s where it all ended up.

Many thanks for getting back to me - confirming my conclusion/finding.

Cheers, Mike
