# How do I use a variable in an inventory file in a template

**URL:** <https://forum.ansible.com/t/how-do-i-use-a-variable-in-an-inventory-file-in-a-template/24286>\
**Category:** Ansible Project\
**Created:** [April 21, 2017, 5:13pm UTC](https://forum.ansible.com/t/how-do-i-use-a-variable-in-an-inventory-file-in-a-template/24286 "2017-04-21T17:13:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [April 21, 2017, 5:13pm UTC](https://forum.ansible.com/t/how-do-i-use-a-variable-in-an-inventory-file-in-a-template/24286/1 "2017-04-21T17:13:36Z")

</div>

> |   
> up vote  
> down vote[favorite](http://stackoverflow.com/questions/43547313/in-ansible-how-do-i-use-a-variable-in-an-inventory-file-in-a-template#)  
>   
>   
> |   
>   
>   
> I have this inventory file  
>   
>   
>   
> inventory/hosts\_elk  
>   
>   
>   
> `<br>[elk-client-0]<br>10.11.22.22<br><br>`  
>   
>   
>   
> Now I have a template, and I want to do this  
>   
>   
>   
> elk\_server.yml.j2  
>   
>   
>   
> `<br>elk_server = {{ elk-client-0 }}<br><br>`  
>   
>   
>   
> But that doesn’t work, that is the value for {{ elk-client-0 }} does NOT get substituted.  
>   
>   
>   
> What is the correct syntax so when the template gets laid out on the server, the resulting file looks like  
>   
>   
>   
> `<br>elk_server = 10.11.22.22<br><br>`  
>   
>   
>   
>   
> |

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [April 21, 2017, 5:43pm UTC](https://forum.ansible.com/t/how-do-i-use-a-variable-in-an-inventory-file-in-a-template/24286/2 "2017-04-21T17:43:06Z")

</div>

Those aren’t variables.  
You defined a group called ‘elk-client-0’ with a nameless server in it.

This template will produce the output you want:

```auto
elk_server = {{ groups['elk-client-0'][0] }}

```

Though I’m a bit confused why you have an elk server in a group called ‘elk-client-0’.

---

<div class="post-metadata">

**Author:** ![ZillaYT](https://avatars.discourse-cdn.com/v4/letter/z/4da419/32.png) [@ZillaYT](https://forum.ansible.com/u/ZillaYT)\
**Post date:** [April 21, 2017, 5:46pm UTC](https://forum.ansible.com/t/how-do-i-use-a-variable-in-an-inventory-file-in-a-template/24286/3 "2017-04-21T17:46:30Z")

</div>

Thanks, that worked! It makes sense now.
