Ansible-playbook 2.5 doesn’t find group vars when specifying an inventory file directly

ansible-playbook

I've been using ansible scripts for some one shot tasks, for example doing a firstsetup after provisioning a VM.

I have a script that creates a temporary inventory file, uses it to run an ansible playbook and deletes the temporary file afterwards:

INVENTORY=$(mktemp /tmp/inst-inventory.XXXXXX)
echo "[$FLAVOR]" > $INVENTORY
echo "$HOSTNAME user_password='$USER_PASSWORD' admin_password='$ADMIN_PASSWORD'" >> $INVENTORY
ansible-playbook -i $INVENTORY `dirname $0`/firstsetup_$FLAVOR.yml --become
rm $INVENTORY

This worked perfectly, but since upgrading ansible from 2.4 to 2.5 the playbooks can't find variables defined in the group_vars directory anymore.

$ playbooks/firstsetup.sh HOSTNAME ubuntu
Password for localadmin: PASSWORD

Running ansible-playbook firstsetup_ubuntu.yml on HOSTNAME

PLAY [ubuntu] *********************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************
fatal: [HOSTNAME]: FAILED! => {"msg": "The field 'become_pass' has an invalid value, which includes an undefined variable. The error was: 'ubuntu_inisc_initialpass' is undefined"}
        to retry, use: --limit @/home/geschnei/playbooks/firstsetup_ubuntu.retry

PLAY RECAP ************************************************************************************************************
HOSTNAME                    : ok=0    changed=0    unreachable=0    failed=1

This undefined variable is defined in ~/.ansible/group_vars/ubuntu.yml and is found when I run the same playbook against a host that is listed in the regular inventory file.

The Roadmap for Ansible 2.5 lists:

Add option to set playbook dir for adhoc, inventory and console to allow for ‘relative path loading’

Which sounds like it could be related, but I am unable to find more specific information about it. The 2.5 Porting Guide doesn't mention anything about that either.

What do I have to configure so ansible-playbook finds the group_vars files again?

Best Answer

After downgrading ansible to 2.4 I had to realize that it didn't work with that version either.

More reading revealed that it is not possible to change the location of host_vars or group_vars, they are always relative to the hostfile.

So after all I solved it by changing the location of the temporary inventory to the ~/.ansible dir, where the regular inventory also resides.

INVENTORY=$(mktemp ~/.ansible/inst-inventory.XXXXXX)

I don't know why it worked before. It works again now.