host variables in hosts file versus host variables in files within host_vars inventory directory

1,675 views
Skip to first unread message

Jason S

unread,
Dec 11, 2017, 8:29:40 PM12/11/17
to Ansible Project
The inventory documentation describes setting some variables for a single host in a hosts file

[atlanta]
host1 http_port=80 maxRequestsPerChild=808
host2 http_port=303 maxRequestsPerChild=909
Continuing with that example, how do you access the variable?
If I use
   hostvars[inventory_hostname]['{{ inventory_hostname }} http_port']

the value I get is 
    80 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
   |-- group_vars
   |   |-- atlanta 
   |-- host_vars
   |   |-- host1
   |   |-- host2
   |-- hosts

And in the host1 file I have
http_port: 80
maxRequestsPerChild: 808

Then I can access the variable values with
hostvars[inventory_hostname]['http_port']
hostvars[inventory_hostname]['maxRequestsPerChild']

The questions are then
1. How do I access the variables as in the first example of host variables in the hosts file as described at http://docs.ansible.com/ansible/latest/intro_inventory.html#host-variables ?
2. Why are the variables not consistent whether defined in a host file or host_vars subdirectory? Is this intentional?


Brian Coca

unread,
Dec 11, 2017, 8:57:43 PM12/11/17
to Ansible Project
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

Jason S

unread,
Dec 11, 2017, 9:33:27 PM12/11/17
to Ansible Project
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'] )

Jason S

unread,
Dec 11, 2017, 9:37:19 PM12/11/17
to Ansible Project
I should have mentioned I have inventory = inventories/uat defined in my ansible.cfg or I'm running the playbook with -i inventories/uat  (or both)

Brian Coca

unread,
Dec 11, 2017, 9:40:27 PM12/11/17
to Ansible Project
[stable:vars]
stable-1 http_port=80 maxRequestsPerChild=808

^ this is defining a var named 'stable-1 http_port' , not http_port for stable-1
the format is:

[groupname:vars]
varname=value


then you can use simply with:

debug:
var: varname

or

debug:
var: hostvars[inventory_hostname]['varname']


If you want to define a variable PER host do it in the host definition
[groupname]
hostname varname=value

--
----------
Brian Coca

Jason S

unread,
Dec 11, 2017, 10:19:24 PM12/11/17
to Ansible Project
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.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages