# Inventory to Variable Question

**URL:** <https://forum.ansible.com/t/inventory-to-variable-question/25483>\
**Category:** Ansible Project\
**Created:** [September 14, 2017, 3:08pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483 "2017-09-14T15:08:06Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sven\_Hans\_Knecht](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Sven\_Hans\_Knecht](https://forum.ansible.com/u/Sven_Hans_Knecht)\
**Post date:** [September 14, 2017, 3:08pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/1 "2017-09-14T15:08:06Z")

</div>

Is it possible to pull hosts from an inventory file as a variable to be used?

Example:  
######## Inventory File ###########

[group\_1]  
host1

####### Variable File ##############

hostname: {{ host1 }}

I want to reduce the number of places that hosts need to be listed and use the inventory file to add to a config file, using variables.

---

<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:** [September 14, 2017, 3:10pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/2 "2017-09-14T15:10:25Z")

</div>

I would need more info to give you a full answer, but  
`inventory_hostname` already has the entry from the inventory file ...  
not sure what you are trying to do.

---

<div class="post-metadata">

**Author:** ![Sven\_Hans\_Knecht](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Sven\_Hans\_Knecht](https://forum.ansible.com/u/Sven_Hans_Knecht)\
**Post date:** [September 14, 2017, 4:00pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/3 "2017-09-14T16:00:11Z")

</div>

I’m trying to install a cluster of Elasticsearch servers and writing an ansible script to manage that.

In my hosts file I have something like:

[elasticsearch-servers]  
elk-host-01  
elk-host-02  
elk-host-03

in side of my config file, I’d like it to do something like:

discovery.zen.ping.unicast.hosts: [“{{ elasticsearch-servers[0] }}”, “{{ elasticsearch-servers[1] }}”, “{{ elasticsearch-servers[2] }}”]

because I can put them in as separate variables, or use the hostnames directly, but I’d prefer to not have to change them in multiple places.

---

<div class="post-metadata">

**Author:** ![Sven\_Hans\_Knecht](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Sven\_Hans\_Knecht](https://forum.ansible.com/u/Sven_Hans_Knecht)\
**Post date:** [September 14, 2017, 4:08pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/4 "2017-09-14T16:08:43Z")

</div>

I found that I needed to be using:

```auto
[groups['groupname'][0]

```

Thanks!

---

<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:** [September 14, 2017, 4:42pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/5 "2017-09-14T16:42:57Z")

</div>

For this case you can use this

discovery.zen.ping.unicast.hosts: {{ groups['elasticsearch-servers'] | to\_json }}

---

<div class="post-metadata">

**Author:** ![Sven\_Hans\_Knecht](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Sven\_Hans\_Knecht](https://forum.ansible.com/u/Sven_Hans_Knecht)\
**Post date:** [September 14, 2017, 5:17pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/6 "2017-09-14T17:17:19Z")

</div>

Oh, that’s super handy. Because then it would expand dynamically.

Thanks!

---

<div class="post-metadata">

**Author:** ![Sven\_Hans\_Knecht](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Sven\_Hans\_Knecht](https://forum.ansible.com/u/Sven_Hans_Knecht)\
**Post date:** [September 14, 2017, 5:19pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/7 "2017-09-14T17:19:07Z")

</div>

Kai,

Is there a way to append information to each one as well?

say I want a variable to append a port to each host

Hans

---

<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:** [September 14, 2017, 5:47pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/8 "2017-09-14T17:47:08Z")

</div>

With template you can do almost anything.

- debug: msg="{% set foo = %}{% for i in groups['elasticsearch-servers'] %}{{ foo.append(i + ':8888')}}{% endfor %}{{ foo | to\_json }}"

For Jinja template documentation  
[http://jinja.pocoo.org/docs/dev/templates/](http://jinja.pocoo.org/docs/dev/templates/)

---

<div class="post-metadata">

**Author:** ![Sven\_Hans\_Knecht](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Sven\_Hans\_Knecht](https://forum.ansible.com/u/Sven_Hans_Knecht)\
**Post date:** [September 14, 2017, 5:49pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/9 "2017-09-14T17:49:30Z")

</div>

Awesome! Thanks. I’ll start looking into the templates and use those.

---

<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:** [September 14, 2017, 6:19pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/10 "2017-09-14T18:19:48Z")

</div>

Too complicated:

&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg="{{':8888, '.join(groups['elasticsearch-servers'])|to\_json}}"

---

<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:** [September 14, 2017, 6:23pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/11 "2017-09-14T18:23:17Z")

</div>

and i was missing a split:

- debug: msg="{{':8888, '.join(groups['all']).split(',')|to\_json}}"

or alternative

- debug: msg=" [{{':8888, '.join(groups['all'])}}]"

---

<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:** [September 14, 2017, 7:04pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/12 "2017-09-14T19:04:38Z")

</div>

> and i was missing a split:
> 
> - debug: msg="{{':8888, '.join(groups['all']).split(',')|to\_json}}"

Result  
["a1:8888", " a2:8888", " a3"]

> or alternative
> 
> - debug: msg=" [{{':8888, '.join(groups['all'])}}]"

Result  
[a1:8888, a2:8888, a3]

None of the solutions add the port number to the last record.

---

<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:** [September 14, 2017, 7:08pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/13 "2017-09-14T19:08:22Z")

</div>

need a trailiing :8888 i keep forgetting join does not do last element

- debug: msg="{{(':8888, '.join(groups['all']).split(',') + ':8888')|to\_json}}"

or alternative

- debug: msg=" [{{':8888, '.join(groups['all']) + ':8888'}}]"

---

<div class="post-metadata">

**Author:** ![Sven\_Hans\_Knecht](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Sven\_Hans\_Knecht](https://forum.ansible.com/u/Sven_Hans_Knecht)\
**Post date:** [September 15, 2017, 2:19pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/14 "2017-09-15T14:19:53Z")

</div>

So I’d like a variable port, not just a static one, so I’m using:

{{":{{ es\_port }}, ".join(groups[‘elasticsearch\_servers’]) + “:{{ es\_port }}”}}

I realize I probably have a quote or something wrong, but can you show me where?

Because I also tried:

{{“:” + {{ es\_port }}+ ", ".join(groups[‘elasticsearch\_servers’]) + “:” + {{ es\_port }}}}

And that didn’t fill in the es\_port variable.

---

<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:** [September 15, 2017, 2:28pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/15 "2017-09-15T14:28:57Z")

</div>

dont stack moustaches:

{{(":%s" % es\_port).join(groups['elasticsearch\_servers']) + ":" + es\_port}}

---

<div class="post-metadata">

**Author:** ![Sven\_Hans\_Knecht](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Sven\_Hans\_Knecht](https://forum.ansible.com/u/Sven_Hans_Knecht)\
**Post date:** [September 15, 2017, 2:36pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/16 "2017-09-15T14:36:36Z")

</div>

And if I want a comma in between each one, it would look like this?

{{(“:%s” % es\_port + “,”).join(groups[‘elasticsearch\_servers’]) + “:” + es\_port}}

right?

---

<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:** [September 15, 2017, 2:39pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/17 "2017-09-15T14:39:08Z")

</div>

yep

or ":%s," ....

---

<div class="post-metadata">

**Author:** ![Sven\_Hans\_Knecht](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@Sven\_Hans\_Knecht](https://forum.ansible.com/u/Sven_Hans_Knecht)\
**Post date:** [September 15, 2017, 2:42pm UTC](https://forum.ansible.com/t/inventory-to-variable-question/25483/18 "2017-09-15T14:42:34Z")

</div>

Awesome, thanks! that explained a lot. Appreciate the help.
