I am trying to find the best way to write roles which have a per item configurations. By that I mean something like nginx or uwsgi where there are multiple sites or applications.
For example my current nginx role has a
nginx_sites variable which is a dictionary of multiple site configurations:
nginx_sites:
proj1:
- root /var/www
- listen 80
- location: /
options:
- include uwsgi_params
- uwsgi_pass unix:///var/run/uwsgi/app/proj1/socket
proj2:
- root /var/www
- listen 8080
- location: /
options:
- include uwsgi_params
- uwsgi_pass unix:///var/run/uwsgi/app/proj2/socket
Then my uwsgi role has a
uswgi_apps variable where the different applications are defined:
uwgis_apps:
proj1:
wsgi_file: /srv/django/proj1/src/project/wsgi.py
virtualenv: /srv/django/proj1/env
proj2:
wsgi_file: /srv/django/proj2/src/project/wsgi.py
virtualenv: /srv/django/proj2/env
My django role would than have a
django_projects variable with multiple projects:
django_projects:
proj1: <git repository>
proj2: <git repository>
My problem with this is, that I need to know all the paths and eventually other options for the "upper" roles. In my opinion the path to the uwsgi socket is something internal to the role (it could depend on the os) so I would like some kind of "fact" variable like
uwsgi_sockets["proj1"] to use in the above configuration. The same with the uwsgi configuration.
I thougt about splitting the roles in two parts a global role which would install and configure the nginx and uwsgi packages and a site/application role which would install the configuration for one site/application. These roles would depend on the global roles.
Then I could write a playbook like so
- hosts: django_host
roles:
- role: django_project
django_project_repo: <git repository of proj1>
- role: uwsgi_application
uwsgi_application_name: proj1
uwsgi_application_config:
wsgi_file: {{ django_project_wsgi_file }}
virtualenv: {{ django_project_virtualenv }}
- role: nginx_site
nginx_site_name: proj1
nginx_site_config:
- root /var/www
- listen 80
- location: /
options:
- include uwsgi_params
- uwsgi_pass unix://{{ uwsgi_application_socket }}
- role: django_project
django_project_repo: <git repository of proj2>
- role: uwsgi_application
uwsgi_application_name: proj2
uwsgi_application_config:
wsgi_file: {{ django_project_wsgi_file }}
virtualenv: {{ django_project_virtualenv }}
- role: nginx_site
nginx_site_name: proj2
nginx_site_config:
- root /var/www
- listen 8080
- location: /
options:
- include uwsgi_params
- uwsgi_pass unix://{{ uwsgi_application_socket }}
I don't know if this is even possible because the variables must be set after each invokation of the roles. I would have to specify the options in the playbook itself instead of in the group_vars files.
So long story short, is this a good aproach? Is it even possible? How do you solve such dependencies in the role configurations? (the roles themselves are not dependent)