Looking for global level equivalent of 'inventory_dir' with Ansible 2.4 onwards

55 views
Skip to first unread message

ishan jain

unread,
Nov 21, 2017, 4:54:09 AM11/21/17
to Ansible Project
Hi all,

I have a bunch of playbooks which only define what needs to be done, and i had split my inventory in some logical groups, so each inventory file is a complete definition of this group and to run run any playbook, i would simply use the desired inventory file.

The hosts part of my playbook is defined by a variable and this variable is defined by a file which i will always find next to any of my inventory. So i would simply load this file in my playbooks like this:

vars_file
         
- "{{inventory_dir}}/host_definition.yaml"


It worked perfectly fine till now, but i see that this is going to break in Ansible 2.4 as inventory_dir is now a host var so i cannot use it to load host variables.
So my questions:

1) Any alternative to get path of inventory file at the global level ? 
2) I want to keep the target host list defined (and thus loaded with) in the inventory. I understand the whole situation where Ansible needs to know the host vars before loading the variables within, but i would like to load a global variable from within inventory files. Is this possible ?



Note: In the release notes of Ansible 2.4 ( https://github.com/ansible/ansible/blob/stable-2.4/CHANGELOG.md#24-dancing-days---2017-09-19 ) there is a mention about change in include_dir which i think should be inventory_dir

The include_dir var is not a global anymore, as we now allow multiple inventory sources, it is now host dependant. This means it cannot be used wherever host vars are not permitted, for example in task/handler names.

Anthony Truskinger

unread,
May 16, 2019, 1:38:15 AM5/16/19
to Ansible Project
Did you ever find a solution to this?

ishan jain

unread,
May 16, 2019, 3:34:29 AM5/16/19
to Ansible Project
Yes, we did, as this was the structure of every single playbook in our project. But to be honest, i am blanking out on the solution that we did. Since I moved to another company last year, i don't have access to the code base any more :(
I will try to stress my memory to blurt out the solution.

BR,
Ishan

Anthony Truskinger

unread,
May 16, 2019, 8:38:44 PM5/16/19
to Ansible Project
Thank you so much for the response - I appreciate it.

For other weary internet travellers, I went with the annoying, but simplest change, of explicitly defining localhost in every one of my inventories:

ungrouped:
hosts:
# defined explicitly so {{ inventory_dir }} will be defined
localhost:
vars:
ansible_connection: local
ansible_python_interpreter: "{{ansible_playbook_python}}"

And coupled that with this assertion where needed:
tasks:
- name: Check that inventory_dir is defined for localhost
assert:
fail_msg: "inventory_dir was none, make sure you have localhost defined in your inventory"
that:
- "inventory_dir is defined"

--Anthony
Reply all
Reply to author
Forward
0 new messages