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

30 views
Skip to first unread message

ZillaYT

unread,
Apr 21, 2017, 1:13:36 PM4/21/17
to Ansible Project

I have this inventory file

inventory/hosts_elk

[elk-client-0]
10.11.22.22

Now I have a template, and I want to do this

elk_server.yml.j2

elk_server = {{ elk-client-0 }}

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

elk_server = 10.11.22.22

Dick Davies

unread,
Apr 21, 2017, 1:43:12 PM4/21/17
to ansible list
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:

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'.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/06b4ebff-9b5a-42c7-8618-214fb70f78b5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ZillaYT

unread,
Apr 21, 2017, 1:46:30 PM4/21/17
to Ansible Project, di...@hellooperator.net
Thanks, that worked! It makes sense now.


On Friday, April 21, 2017 at 1:43:12 PM UTC-4, Dick Davies wrote:
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:

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'.
On 21 April 2017 at 18:13, ZillaYT <zil...@gmail.com> wrote:

I have this inventory file

inventory/hosts_elk

[elk-client-0]
10.11.22.22

Now I have a template, and I want to do this

elk_server.yml.j2

elk_server = {{ elk-client-0 }}

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

elk_server = 10.11.22.22

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages