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
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