# How to handle environment specific role vars?

**URL:** <https://forum.ansible.com/t/how-to-handle-environment-specific-role-vars/25980>\
**Category:** Ansible Project\
**Created:** [November 9, 2017, 10:49pm UTC](https://forum.ansible.com/t/how-to-handle-environment-specific-role-vars/25980 "2017-11-09T22:49:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bo\_Vial](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Bo\_Vial](https://forum.ansible.com/u/Bo_Vial)\
**Post date:** [November 9, 2017, 10:49pm UTC](https://forum.ansible.com/t/how-to-handle-environment-specific-role-vars/25980/1 "2017-11-09T22:49:03Z")

</div>

I am trying to work out the best way to handle multiple environments (dev/stage/prod) in Ansible?

I have read the best practices outlined in the Ansible docs here: [http://docs.ansible.com/ansible/latest/playbooks\_best\_practices.html](http://docs.ansible.com/ansible/latest/playbooks_best_practices.html)

Having an entirely different inventory for dev/stage/prod makes sense but I am unsure how to achieve a similar separation for role vars.

For example, I have split the inventory per environment as follows:

inventories/  
production/  
hosts/  
groups\_vars/  
group1  
group2  
host\_vars/  
hostname1  
hostname2  
staging/  
hosts/  
group\_vars/  
group1  
host\_vars/  
stagehost1

The above works great for group vars and host vars, but I can’t figure out a nice way to do something similar for role vars.

Currently, I have two possible candidate solutions

Option 1.  
Set an environment variable ‘ENVIRONMENT’ to either ‘production’ or ‘staging’. The first task in a role then includes a different vars file based on this environment variable. The directory structure would be:

roles/  
myrole/  
tasks/  
main.yml  
vars/  
main.yml ← common role vars  
production.yml ← production sepcific role vars  
staging.yml ← staging specific role vars

The first task in roles/myrole/tasks/main.yml would be
