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:
- 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:
---
__group_packages:
- network-manager-openvpn
last but not least, my directory structure looks like this:
> 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
> "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