I am having trouble wrapping my head around how i should setup my variable structure. I have two locations with multiple environments at each. I can have different variables depending on the location and production env.
For example, I have a dev web server at location A and a prod web server at location B. I want them to each have a separate virtual host. What is the best way to accomplish this?
I tried putting variables in the group_vars directory with a separate directory for each location, then subdirectories for each environment, but then I each location is going to pull in every variable from all environments.
I tried just using group_vars (all, webserver, etc) but I couldn’t get this to work either unless I have a group_var file for every group at every location for each environment.
The only other thing that I can think of to do to accomplish this is to put the defaults for all groups in group_vars. In my inventory file for each location or environment I can assign a group_var (i.e. env_type=dev). Then in my playbook I can do:
`
var_files:
- /var/{{ location }}/{{ env_type }}/group_name.yml
`
I want to avoid duplicating variables that are the same for each env and make the roles as dynamic as possible. I’ve read through the docs and can’t seem to make sense of it. I am sure that Ansible or someone else has figured out. I just cant find it. If someone can point me in the right direction I would really appreciate it.
Thanks.
I was trying to avoid a group_var file for everything but it looks like that is the route that I am going to have to go. I have set the parent-child relationship of the groups in my inventory files. So in my group_vars directory I have location_a, location_b, dev, prod, qa, location_a_dev, location_b_dev, location_a_web, location_b_web, etc. This accomplishes what I want but I feel like it is going to get out of control very quickly. If there is a better way to accomplish this please let me know.
So I figured out a setup that works for what I am trying to do. I was trying to set a hiera structure so that I could set global variables at the top and have more specific groups overwrite these variables. I ran into a lot of issues trying to set this up for multiple environments that span multiple datacenters.
I have an inv file for each environment (dev, test, prod). In my ansible home directory I have a dir called ‘env’. Inside there I have a dir for each environment. In each environment I have a playbooks/group_vars directory. So for each environment I have:
/etc/ansible/dev ← Inventory
/etc/ansible/group_vars <-global group_vars
/etc/ansible/env/dev/playbooks ← Environment specific playbooks
/etc/ansible/env/dev/playbooks/group_vars ← Environment specific variables since group vars in the playbooks directory overwrite the inv group_vars
When I run ansible I run:
ansible-playbook -i dev env/dev/playbooks/web-server.yml
I still have to specify each location. It works out though. I create a main group for example [web-server] then i set its children to web-server-loca and web-server-locb.
#Location
[loca:children]
web-server-loca
[locb:children]
web-server-locb
[web-server:children]
web-server-loca
web-server-locb
This way site specific vars overwrite role vars, and role-location vars overwrite location specific vars.
I have hash merging enabled as well. This setup allows me to set something in /etc/ansible/group_vars and have the more specific environment variables (/etc/ansible/env/dev/playbooks/group_vars) overwrite the global vars, or if needed merged via hash.
I hope this helps someone else.