Inventory to Variable Question

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.

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.

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.

I found that I needed to be using:

[groups['groupname'][0]

Thanks!

For this case you can use this

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

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

Thanks!

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

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/

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

Too complicated:

    - debug: msg="{{':8888, '.join(groups['elasticsearch-servers'])|to_json}}"

and i was missing a split:

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

or alternative

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

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.

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'}}]"

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.

dont stack moustaches:

{{(":%s" % es_port).join(groups['elasticsearch_servers']) + ":" + es_port}}

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?

yep

or ":%s," ....

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