Mikhail T.
Mikhail T.

Reputation: 3967

Is path of the inventory file accessible to playbooks?

I have a hierarchy of inventories like this:

inventories
  foo
    foo1/hosts
    foo2/hosts
  bar
    bar1/hosts
    bar2/hosts

Normally, I invoke ansible with explicit full path:

ansible -i inventories/bar/bar1 ....

However, some of the playbooks can run on the combined inventories:

ansible -i inventories/bar ....

This joins the multiple hosts files together, just as I want. However, I do not see a way for the tasks and templates to discern, which particular sub-inventory(ies) the host belongs to.

Is there a way to know about this? Ideally, a host would belong to group(s) based on the inventory file(s) it is listed in...

Upvotes: 2

Views: 1636

Answers (1)

Mikhail T.
Mikhail T.

Reputation: 3967

Yes, it is accessible. Ansible 2.3+ has the following two new magic variables:

  • inventory_file
  • inventory_dir

Upvotes: 4

Related Questions