# host variables in hosts file versus host variables in files within host\_vars inventory directory

**URL:** <https://forum.ansible.com/t/host-variables-in-hosts-file-versus-host-variables-in-files-within-host-vars-inventory-directory/26254>\
**Category:** Ansible Project\
**Created:** [December 12, 2017, 1:29am UTC](https://forum.ansible.com/t/host-variables-in-hosts-file-versus-host-variables-in-files-within-host-vars-inventory-directory/26254 "2017-12-12T01:29:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_S](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@Jason\_S](https://forum.ansible.com/u/Jason_S)\
**Post date:** [December 12, 2017, 1:29am UTC](https://forum.ansible.com/t/host-variables-in-hosts-file-versus-host-variables-in-files-within-host-vars-inventory-directory/26254/1 "2017-12-12T01:29:40Z")

</div>

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](http://docs.ansible.com/ansible/latest/intro_inventory.html#host-variables)

```auto
[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](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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 12, 2017, 1:57am UTC](https://forum.ansible.com/t/host-variables-in-hosts-file-versus-host-variables-in-files-within-host-vars-inventory-directory/26254/2 "2017-12-12T01:57:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jason\_S](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@Jason\_S](https://forum.ansible.com/u/Jason_S)\
**Post date:** [December 12, 2017, 2:33am UTC](https://forum.ansible.com/t/host-variables-in-hosts-file-versus-host-variables-in-files-within-host-vars-inventory-directory/26254/3 "2017-12-12T02:33:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jason\_S](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@Jason\_S](https://forum.ansible.com/u/Jason_S)\
**Post date:** [December 12, 2017, 2:37am UTC](https://forum.ansible.com/t/host-variables-in-hosts-file-versus-host-variables-in-files-within-host-vars-inventory-directory/26254/4 "2017-12-12T02:37:19Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [December 12, 2017, 2:40am UTC](https://forum.ansible.com/t/host-variables-in-hosts-file-versus-host-variables-in-files-within-host-vars-inventory-directory/26254/5 "2017-12-12T02:40:09Z")

</div>

[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:  
&nbsp;&nbsp;&nbsp;var: varname

or

debug:  
&nbsp;&nbsp;var: hostvars[inventory\_hostname]['varname']

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

---

<div class="post-metadata">

**Author:** ![Jason\_S](https://avatars.discourse-cdn.com/v4/letter/j/3ab097/32.png) [@Jason\_S](https://forum.ansible.com/u/Jason_S)\
**Post date:** [December 12, 2017, 3:19am UTC](https://forum.ansible.com/t/host-variables-in-hosts-file-versus-host-variables-in-files-within-host-vars-inventory-directory/26254/6 "2017-12-12T03:19:24Z")

</div>

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.
