Continuing with that example, how do you access the variable?
If I use
hostvars[inventory_hostname][‘{{ inventory_hostname }} http_port’]
the value I get is80 maxRequestsPerChild=808
and I’d have to do some further splitting of the string to get the values.
Otherwise I can use a new line, but the example in Ansible docs puts them on one line, so I was curious as to how to access them simply.
If, however I use an inventory structure such as
– inventories
I'm not sure where you got `hostvars[inventory_hostname]['{{
inventory_hostname }} http_port']` but that does not look like a valid
construct
In any case, using:
hostvars[inventory_hostname]['http_port']
hostvars[inventory_hostname]['maxRequestsPerChild']
you should be able to access the vars no matter if defined inline in
the inventory or in a host/group-vars file,
but just use `http_port` and `maxRequestsPerChild` directly, you
really only need hostvars when accessing other hosts or trying to
compose variable names.
Hi Brian,
I got that via trial and error
I’m using ansible 2.4.2.0
In my case I have an inventory setup like this
inventories/uat/hosts
inventories/uat/host_vars/stable-1
inventories/uat/host_vars/stable-2
inventories/uat/group_vars/stable
in inventories/uat/hosts
[stable]
stable-1
stable-2
Ah, thank-you Brian.
Now I have the variables next to the host definition (within the group definition) it works fine, as expected.
I’m trying to think why I missed this even though it was staring me in the face on the documentation page.
I guess I was thinking that all variables would have to be defined with [:vars]
I had actually tried
[stable-1:vars]
var=value
that is
[hostname:vars]
var=value
And if you can do
[groupname]
hostname var=value
Why not
[groupname]
groupvar=groupvalue
groupvar2=groupvalue2
hostname var1=value1 var2=value2
Anyway it is all working for me now so thanks again.