The problem with doing _var is that you can no longer have generic configuration. Here is what I currently have:
roles/nginx/tasks/main.yml
- name: setup the nginx conf
sudo: true
action: template src=nginx.jinja2 dest=/etc/nginx/sites-enabled/{{ role }}.conf
notify: restart nginx
when: deploy_env is not defined
tags:
- nginx
roles/nginx/templates/nginx.jinja2
{% set application = role %}
upstream {{ application }}_pool {
server 127.0.0.1:{{ services[role].port }};
}
server {
listen {{ services[role].nginx_port }} default;
server_name_in_redirect off;
port_in_redirect off;
access_log /var/log/sm/{{ application }}.access.log sm;
location / {
proxy_set_header host $host;
real_ip_header X-True-Origin;
proxy_pass http://{{ application }}_pool;
}
}
roles/app1/tasks/main.yml
- include: …/…/…/roles/nginx/tasks/main.yml
roles/app2/tasks/main.yml
- include: …/…/…/roles/nginx/tasks/main.yml
and so lets say I want to deploy app1 and app2 each to 3 of their own nodes and 1 shared node that they are both on. I could make this happen by creates roles//vars/main.yml that look like this:
roles/app1/vars/main.yml
nginx_port: 6005
port: 8500
roles/app2/vars/main.yml
nginx_port: 6014
port: 8765
but now when I want to configure my load balancers I have no access to those vars to be able to get what nginx_port I need to proxy to for each role. So exposing the variables globally for the load balancer works:
group_vars/all
services:
app1:
port: 8500
nginx_port: 6005
app2:
port: 8765
nginx_port: 6014
but now the app roles don’t have access to it because they don’t know what role they are. So then if you mix both approaches, keep that previous group_vars/all and change the app vars to look like this:
roles/app1/vars/main.yml
role: app1
roles/app2/vars/main.yml
role: app2
Now everything will just work, unless you create “app3” and forget to set the role: app3 in it, because you wont be alerted that you forgot to set the var, it’ll just happily take whatever the previous app that was configured
said it was and use it.
That is where I am at today, I had this all working and then added app3 and the load balancer was sending all traffic to app3 and there wasn’t a clear reason why the app3 loadbalancer pool thought it should route to app2.
So this is what I’m trying to solve.