# access inventory file name

**URL:** <https://forum.ansible.com/t/access-inventory-file-name/15165>\
**Category:** Ansible Project\
**Created:** [May 5, 2014, 9:26pm UTC](https://forum.ansible.com/t/access-inventory-file-name/15165 "2014-05-05T21:26:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Drew\_Gulino](https://avatars.discourse-cdn.com/v4/letter/d/df705f/32.png) [@Drew\_Gulino](https://forum.ansible.com/u/Drew_Gulino)\
**Post date:** [May 5, 2014, 9:26pm UTC](https://forum.ansible.com/t/access-inventory-file-name/15165/1 "2014-05-05T21:26:57Z")

</div>

As suggested in the best practices doc, have two inventory files: qa and production

I call these using the -i parameter:

ansible-playbook -i qa …

Is there a way I can determine in my playbook which inventory I am using? The use case is I want to notify one chat room if using qa inventory, another if I use production.

I’ve tried just putting a variable in the root of the inventory file qa:

inventory=qa  
[webservers]  
host1

but that doesn’t work: is not displayed as a variable with: “ansible host1 -m setup -i qa”

I can define a variable for each host like:

[webservers]  
host1 inventory=qa

but that is terribly redundant.

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [May 6, 2014, 12:45am UTC](https://forum.ansible.com/t/access-inventory-file-name/15165/2 "2014-05-06T00:45:57Z")

</div>

The name of the current inventory file in use is stored in the “inventory\_file” variable name, so you could check that. Beyond that, there are two other options:

1. Use --extra-vars to specify the target environment, for example: --extra-vars=“inventory=qa”
2. The group\_vars location is based on the location of the inventory file, so you could do something like this:

qa/group\_vars/all:  
inventory=qa

prod/group\_vars/all:  
inventory=production

The qa/hosts and prod/hosts would contain your different inventories, so you’ll get output like this:

$ cat play.yml

- hosts: all  
gather\_facts: no

tasks:

- name: show the variable  
debug: var=inventory

$ ansible-playbook play.yml -i prod/hosts

PLAY [all] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK: [show the variable] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [you] =\> {  
“inventory”: “production”  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
you : ok=1 changed=0 unreachable=0 failed=0

$ ansible-playbook play.yml -i qa/hosts

PLAY [all] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK: [show the variable] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [me] =\> {  
“inventory”: “qa”  
}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
me : ok=1 changed=0 unreachable=0 failed=0

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Mridusmita\_Talukdar](https://avatars.discourse-cdn.com/v4/letter/m/6a8cbe/32.png) [@Mridusmita\_Talukdar](https://forum.ansible.com/u/Mridusmita_Talukdar)\
**Post date:** [May 12, 2014, 7:27pm UTC](https://forum.ansible.com/t/access-inventory-file-name/15165/3 "2014-05-12T19:27:30Z")

</div>

Hi,  
We have multiple clusters. Each of the clusters need to have a different var file (cluster specific variables)  
Currently the variable file name is same as the cluster name, so that when the extra-variable says cluster=ABC, my playbook picks up the variable file ABC.yml which I refer to in the playbook by {{cluster}}.yml.  
This has a risk because the user while running the playbook might give the inventory file name of cluster XYZ and give cluster=ABC.  
In order to avoid this risk, I want to pick up my variable file as {{inventory\_file}}.yml  
But looks like the variable file is referred to, before the variable inventory\_file is loaded?  
Because even though {{inventory\_file}}.yml is picked up correctly under tasks(I printed it our using debug:) the variable file is not being picked up.  
Am I missing something here?  
Thanks and regards,  
Mridu

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 13, 2014, 3:38pm UTC](https://forum.ansible.com/t/access-inventory-file-name/15165/4 "2014-05-13T15:38:06Z")

</div>

“But looks like the variable file is referred to, before the variable inventory\_file is loaded?”

I’m not sure what this means, can you elaborate?

Thanks!
