# Choose inventory hosts.ini file based on condition

**URL:** <https://forum.ansible.com/t/choose-inventory-hosts-ini-file-based-on-condition/30439>\
**Category:** Ansible Project\
**Created:** [May 28, 2019, 3:31pm UTC](https://forum.ansible.com/t/choose-inventory-hosts-ini-file-based-on-condition/30439 "2019-05-28T15:31:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saranya\_N](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Saranya\_N](https://forum.ansible.com/u/Saranya_N)\
**Post date:** [May 28, 2019, 3:31pm UTC](https://forum.ansible.com/t/choose-inventory-hosts-ini-file-based-on-condition/30439/1 "2019-05-28T15:31:53Z")

</div>

I have different staging environments under inventory.

Inventory/  
Qa/  
&nbsp;&nbsp;&nbsp;Group\_vars  
&nbsp;&nbsp;&nbsp;hosts.ini  
Uat/  
&nbsp;&nbsp;&nbsp;group\_vars  
&nbsp;&nbsp;&nbsp;hosts.ini  
Prod/  
&nbsp;&nbsp;&nbsp;group\_vars  
&nbsp;&nbsp;&nbsp;hosts.ini

I want to choose inventory file based on environment.

Within my playbooks I also use env variable.

When I run my ansible playbook like this it doesn't work. How can I do this?

ansible-playbook -i inventory/{{ env }}/ -e "env=prod"

---

<div class="post-metadata">

**Author:** ![vinod93pop](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@vinod93pop](https://forum.ansible.com/u/vinod93pop)\
**Post date:** [May 31, 2019, 5:20am UTC](https://forum.ansible.com/t/choose-inventory-hosts-ini-file-based-on-condition/30439/2 "2019-05-31T05:20:41Z")

</div>

Are you using right format? I can see Prod in inventory but in variable you used prod . Check the spelling

---

<div class="post-metadata">

**Author:** ![Wawrzek\_Niewodniczan](https://avatars.discourse-cdn.com/v4/letter/w/bbe5ce/32.png) [@Wawrzek\_Niewodniczan](https://forum.ansible.com/u/Wawrzek_Niewodniczan)\
**Post date:** [June 3, 2019, 12:08pm UTC](https://forum.ansible.com/t/choose-inventory-hosts-ini-file-based-on-condition/30439/3 "2019-06-03T12:08:12Z")

</div>

> I have different staging environments under inventory.

[...]

> Prod/  
> &nbsp;&nbsp;&nbsp;group\_vars  
> &nbsp;&nbsp;&nbsp;hosts.ini

[...]

> When I run my ansible playbook like this it doesn't work. How can I do this?
> 
> ansible-playbook -i inventory/{{ env }}/ -e "env=prod"

Is it 'prod' or 'Prod'?

I'm not sure if that (having {{ env }} in path) can work. AFAIR you  
need a path to the hosts files. Have you tried ansible-playbook -i  
inventory/Prod/hosts.ini?

Wawrzek

---

<div class="post-metadata">

**Author:** ![Saranya\_N](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@Saranya\_N](https://forum.ansible.com/u/Saranya_N)\
**Post date:** [June 4, 2019, 1:36am UTC](https://forum.ansible.com/t/choose-inventory-hosts-ini-file-based-on-condition/30439/4 "2019-06-04T01:36:54Z")

</div>

Sorry that prod / Prod is a typo.  
I’m basically using same at both places.

---

<div class="post-metadata">

**Author:** ![Wawrzek\_Niewodniczan](https://avatars.discourse-cdn.com/v4/letter/w/bbe5ce/32.png) [@Wawrzek\_Niewodniczan](https://forum.ansible.com/u/Wawrzek_Niewodniczan)\
**Post date:** [June 4, 2019, 6:58am UTC](https://forum.ansible.com/t/choose-inventory-hosts-ini-file-based-on-condition/30439/5 "2019-06-04T06:58:46Z")

</div>

I think 'hosts.ini' is missing from your command.

Wawrzek
