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

The inventory documentation describes setting some variables for a single host in a hosts file
http://docs.ansible.com/ansible/latest/intro_inventory.html#host-variables

[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 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

– 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?

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

[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

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)

[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

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.