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

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.

Did you ever find a solution to this?

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 :frowning:
I will try to stress my memory to blurt out the solution.

BR,
Ishan

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