# Running ansible-playbook with an inventories directory does no utilize the host\_vars files

**URL:** <https://forum.ansible.com/t/running-ansible-playbook-with-an-inventories-directory-does-no-utilize-the-host-vars-files/33533>\
**Category:** Ansible Project\
**Created:** [July 31, 2020, 1:09am UTC](https://forum.ansible.com/t/running-ansible-playbook-with-an-inventories-directory-does-no-utilize-the-host-vars-files/33533 "2020-07-31T01:09:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin\_Clay](https://avatars.discourse-cdn.com/v4/letter/k/e8c25b/32.png) [@Kevin\_Clay](https://forum.ansible.com/u/Kevin_Clay)\
**Post date:** [July 31, 2020, 1:09am UTC](https://forum.ansible.com/t/running-ansible-playbook-with-an-inventories-directory-does-no-utilize-the-host-vars-files/33533/1 "2020-07-31T01:09:46Z")

</div>

Hello,

I have run into an issues with my ansible installation wherein, if I run `ansible-playbook -i inventories/userland site.yml`, the playbook picks up all of the group\_vars and host\_vars files and fills in all of the variables where necessary so that I can use them in my roles and playbooks. However, if I instead run `ansible-playbook -i inventories site.yml`, none of the vars files are picked up and I consistently get errors to the effect of “The task includes an option with an undefined variable.” Even though the variable is very much defined and is working just fine under the other command.

One of my tasks which has the error is listed as such:

```auto
- name: Devine group_packages
set_fact:
group_packages: "{{ __group_packages | list }}"
when: group_packages is not defined

```

group\_vars file, which defines that variable is `laptops.yml` and lists as such:

```auto
---
__group_packages:
- network-manager-openvpn

```

last but not least, my directory structure looks like this:

```auto
.
├── ansible.cfg
├── inventories
│ ├── production
│ │ ├── group_vars
│ │ ├── hosts.yml
│ │ └── host_vars
│ ├── staging
│ │ ├── group_vars
│ │ ├── hosts.yml
│ │ └── host_vars
│ └── userland
│ ├── group_vars
│ │ ├── all.yml
│ │ ├── desktops.yml
│ │ └── laptops.yml
│ ├── hosts.yml
│ └── host_vars
│ ├── base.yml
│ └── M142BK19I02569.yml
├── roles
│ ├── ansible
│ │ ├── defaults
│ │ │ └── main.yml
│ │ ├── files
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── meta
│ │ │ └── main.yml
│ │ ├── README.md
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ ├── tests
│ │ │ ├── inventory
│ │ │ └── test.yml
│ │ ├── .travis.yml
│ │ └── vars
│ │ └── main.yml
│ ├── common
│ │ ├── defaults
│ │ │ └── main.yml
│ │ ├── files
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── meta
│ │ │ └── main.yml
│ │ ├── README.md
│ │ ├── tasks
│ │ │ ├── main.yml
│ │ │ ├── setup-Archlinux.yml
│ │ │ └── setup-Debian.yml
│ │ ├── templates
│ │ ├── tests
│ │ │ ├── inventory
│ │ │ └── test.yml
│ │ ├── .travis.yml
│ │ └── vars
│ │ ├── Archlinux.yml
│ │ ├── Debian.yml
│ │ └── main.yml
│ ├── docker
│ │ ├── defaults
│ │ │ └── main.yml
│ │ ├── files
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── meta
│ │ │ └── main.yml
│ │ ├── README.md
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ ├── tests
│ │ │ ├── inventory
│ │ │ └── test.yml
│ │ ├── .travis.yml
│ │ └── vars
│ │ └── main.yml
│ ├── gitlab
│ │ ├── defaults
│ │ │ └── main.yml
│ │ ├── files
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── meta
│ │ │ └── main.yml
│ │ ├── README.md
│ │ ├── tasks
│ │ │ └── main.yml
│ │ ├── templates
│ │ ├── tests
│ │ │ ├── inventory
│ │ │ └── test.yml
│ │ ├── .travis.yml
│ │ └── vars
│ │ └── main.yml
│ ├── packageCache
│ │ ├── defaults
│ │ │ └── main.yml
│ │ ├── files
│ │ ├── handlers
│ │ │ └── main.yml
│ │ ├── meta
│ │ │ └── main.yml
│ │ ├── README.md
│ │ ├── tasks
│ │ │ ├── main.yml
│ │ │ ├── update-Archlinux.yml
│ │ │ └── update-Debian.yml
│ │ ├── templates
│ │ ├── tests
│ │ │ ├── inventory
│ │ │ └── test.yml
│ │ ├── .travis.yml
│ │ └── vars
│ │ └── main.yml
│ └── userApplications
│ ├── defaults
│ │ └── main.yml
│ ├── files
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── main.yml
│ ├── README.md
│ ├── tasks
│ │ ├── chromium-Archlinux.yml
│ │ ├── chromium-Debian.yml
│ │ ├── firefox-Archlinux.yml
│ │ ├── firefox-Debian.yml
│ │ └── main.yml
│ ├── templates
│ ├── tests
│ │ ├── inventory
│ │ └── test.yml
│ ├── .travis.yml
│ └── vars
│ └── main.yml
├── runTests.sh
├── site.yml
└── .yamllint

```

Thank you in advance for all the help.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 31, 2020, 5:25am UTC](https://forum.ansible.com/t/running-ansible-playbook-with-an-inventories-directory-does-no-utilize-the-host-vars-files/33533/2 "2020-07-31T05:25:51Z")

</div>

> ... `ansible-playbook -i inventories site.yml`, none of the  
> vars files are picked up
> 
> ├── inventories  
> │ ├── production  
> │ │ ├── group\_vars  
> │ │ ├── hosts.yml  
> │ │ └── host\_vars  
> │ ├── staging  
> │ │ ├── group\_vars  
> │ │ ├── hosts.yml  
> │ │ └── host\_vars  
> │ └── userland  
> │ ├── group\_vars  
> │ │ ├── all.yml  
> │ │ ├── desktops.yml  
> │ │ └── laptops.yml  
> │ ├── hosts.yml  
> │ └── host\_vars  
> │ ├── base.yml  
> │ └── M142BK19I02569.yml

IMHO, ansible-playbook should complain about the directory used in -i  
parameter and exit with error.

man ansible-playbook says

&nbsp;&nbsp;"-i, --inventory, --inventory-file  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;specify inventory host path or comma separated host list."

On the contrary, ansible-inventory shows that all hosts.yml will be  
included from the directory.

&nbsp;&nbsp;\> ansible-inventory -i inventories --list

But, the host\_vars and group\_vars will be ignored despite the fact  
that doc says they should be included. Quoting from "Organizing host  
and group variables"  
[https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html#organizing-host-and-group-variables](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables)

&nbsp;&nbsp;"Ansible loads host and group variable files by searching paths  
&nbsp;&nbsp;relative to the inventory file or the playbook file."

(Do you really want to combine production, staging, and userland  
inventories and variables?)

> - name: Devine group\_packages  
> &nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;group\_packages: "{{ \_\_group\_packages | list }}"  
> &nbsp;&nbsp;when: group\_packages is not defined

"\_\_group\_packages" is not valid name of a variable. Quoting from  
"Creating valid variable names"  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#creating-valid-variable-names](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#creating-valid-variable-names)

&nbsp;&nbsp;"Variable names should be letters, numbers, and underscores.  
&nbsp;&nbsp;Variables should always start with a letter."

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 1, 2020, 7:51am UTC](https://forum.ansible.com/t/running-ansible-playbook-with-an-inventories-directory-does-no-utilize-the-host-vars-files/33533/3 "2020-08-01T07:51:00Z")

</div>

(list in the loop)

> \> IMHO, ansible-playbook should complain about the directory used in -i  
> \> parameter and exit with error.  
> \>  
> \> man ansible-playbook says  
> \>  
> \> "-i, --inventory, --inventory-file  
> \> specify inventory host path or comma separated host list."  
> \>  
> \> On the contrary, ansible-inventory shows that all hosts.yml will be  
> \> included from the directory.  
> \>  
> \> \> ansible-inventory -i inventories --list  
> \>  
> \> But, the host\_vars and group\_vars will be ignored despite the fact  
> \> that doc says they should be included. Quoting from "Organizing host  
> \> and group variables"  
> \> [https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html#organizing-host-and-group-variables](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables)  
> \> "Ansible loads host and group variable files by searching paths  
> \> relative to the inventory file or the playbook file."

> Going from what you have said here, there is either a bug in  
> ansible-inventory, which does not take into account the way that  
> ansible-playbook actually works, or ansible-playbook has the bug and should  
> be updated to reflect that the host\_vars and group\_vars can actually be  
> used from the single inventories directory.

Short answer: The environment variable ANSIBLE\_INVENTORY is an easy  
and convenient workaround.

Details: Let's test it with the inventories and playbook below

&nbsp;&nbsp;\> cat inventories/userland/hosts  
&nbsp;&nbsp;test\_01  
&nbsp;&nbsp;test\_02  
&nbsp;&nbsp;\> cat inventories/userland/host\_vars/test\_01  
&nbsp;&nbsp;my\_var: test\_01  
&nbsp;&nbsp;\> cat inventories/userland/host\_vars/test\_02  
&nbsp;&nbsp;my\_var: test\_02

&nbsp;&nbsp;\> cat inventories/staging/hosts  
&nbsp;&nbsp;test\_03  
&nbsp;&nbsp;\> cat inventories/staging/host\_vars/test\_03.yml  
&nbsp;&nbsp;my\_var: test\_03

&nbsp;&nbsp;\> cat test.yml  
&nbsp;&nbsp;- hosts: all  
&nbsp;&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: my\_var

The environment ANSIBLE\_INVENTORY works as described in  
"DEFAULT\_HOST\_LIST"  
[https://docs.ansible.com/ansible/latest/reference\_appendices/config.html#default-host-list](https://docs.ansible.com/ansible/latest/reference_appendices/config.html#default-host-list)  
For example the command

&nbsp;&nbsp;\> ANSIBLE\_INVENTORY="inventories/userland/hosts,\  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inventories/staging/hosts" ansible-playbook test.yml

gives

&nbsp;&nbsp;ok: [test\_01] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;my\_var: test\_01  
&nbsp;&nbsp;ok: [test\_02] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;my\_var: test\_02  
&nbsp;&nbsp;ok: [test\_03] =\>  
&nbsp;&nbsp;&nbsp;&nbsp;my\_var: test\_03
