# Variable in the hosts file?

**URL:** <https://forum.ansible.com/t/variable-in-the-hosts-file/27595>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [May 22, 2018, 4:58pm UTC](https://forum.ansible.com/t/variable-in-the-hosts-file/27595 "2018-05-22T16:58:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisjx](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chrisjx/32/6922_2.png) [@chrisjx](https://forum.ansible.com/u/chrisjx)\
**Post date:** [May 22, 2018, 4:58pm UTC](https://forum.ansible.com/t/variable-in-the-hosts-file/27595/1 "2018-05-22T16:58:02Z")

</div>

On any given day I might be accessing different aws systems with different sets of FQDNs.

I’m looking for a way to basically templatize (new word?) the hosts file.

Example:  
`  
this\_track=track4  
this\_subnet=239

[group1]

ax.$this\_track$.ecm ansible\_host=10.$this\_subnet$.1.5 ansible\_ssh\_private\_key\_file=/home/me/.ssh/me\_rsa  
ca.$this\_track$.ecm ansible\_host=10.$this\_subnet$.1.6 ansible\_ssh\_private\_key\_file=/home/me/.ssh/me\_rsa

[group2]

if.$this\_track$.ecm ansible\_host=10.$this\_subnet$.1.7 ansible\_ssh\_private\_key\_file=/home/me/.ssh/me\_rsa  
ui.$this\_track$.ecm ansible\_host=10.$this\_subnet$.1.8 ansible\_ssh\_private\_key\_file=/home/me/.ssh/me\_rsa

`

Ideally I would set the this\_track variable at the top of the playbook file, but I’d be OK with something like this.

Any tips appreciated,  
Chris.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 22, 2018, 5:45pm UTC](https://forum.ansible.com/t/variable-in-the-hosts-file/27595/2 "2018-05-22T17:45:44Z")

</div>

> On any given day I might be accessing different aws systems with different  
> sets of FQDNs.
> 
> I'm looking for a way to basically templatize (new word?) the hosts file.

It's called dynamic inventory

> Example:  
> this\_track=track4  
> this\_subnet=239
> 
> [group1]
> 
> ax.$this\_track$.ecm ansible\_host=10.$this\_subnet$.1.5  
> ansible\_ssh\_private\_key\_file=/home/me/.ssh/me\_rsa  
> ca.$this\_track$.ecm ansible\_host=10.$this\_subnet$.1.6  
> ansible\_ssh\_private\_key\_file=/home/me/.ssh/me\_rsa
> 
> [group2]
> 
> if.$this\_track$.ecm ansible\_host=10.$this\_subnet$.1.7  
> ansible\_ssh\_private\_key\_file=/home/me/.ssh/me\_rsa  
> ui.$this\_track$.ecm ansible\_host=10.$this\_subnet$.1.8  
> ansible\_ssh\_private\_key\_file=/home/me/.ssh/me\_rsa
> 
> Ideally I would set the this\_track variable at the top of the playbook  
> file, but I'd be OK with something like this.

With a custom script you can do all this and more.

[https://docs.ansible.com/ansible/latest/user\_guide/intro\_dynamic\_inventory.html](https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html)  
[https://docs.ansible.com/ansible/latest/dev\_guide/developing\_inventory.html](https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html)
