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
[stable:vars]
stable-1 http_port=80 maxRequestsPerChild=808
stable-2 http_port=303 maxRequestsPerChild=909
If I run a simply play-book like this
---
- hosts: stable-1
connection: ssh
gather_facts: no
tasks:
- name: Debug http_port
debug:
var: hostvars[inventory_hostname]['http_port']
- name: Debug http_port
debug:
var: hostvars[inventory_hostname][inventory_hostname + ' http_port']
- name: Debug maxRequestsPerChild
debug:
var: hostvars[inventory_hostname]['maxRequestsPerChild']
I get
stable-1 | SUCCESS => {
"changed": false,
"hostvars[inventory_hostname]['http_port']": "VARIABLE IS NOT DEFINED!"
}
stable-1 | SUCCESS => {
"changed": false,
"hostvars[inventory_hostname][inventory_hostname + ' http_port']": "80
maxRequestsPerChild=808"
}
stable-1 | SUCCESS => {
"changed": false,
"hostvars[inventory_hostname]['maxRequestsPerChild']": "VARIABLE IS NOT
DEFINED!"
}
If I define the host variables in inventories/uat/host_vars/stable-1 then
it works as expected (i.e variable values available via
hostvars[inventory_hostname]['http_port']
)
On Tuesday, 12 December 2017 12:57:43 UTC+11, Brian Coca wrote:
>
> 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.
>
> --
> ----------
> Brian Coca
>
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/ab5e9bb2-3926-4a15-a163-acc19c860675%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.