Best practice for roles with per item configuration

92 views
Skip to first unread message

André

unread,
Oct 3, 2014, 10:40:58 AM10/3/14
to ansible...@googlegroups.com
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)

André

unread,
Oct 10, 2014, 12:17:24 PM10/10/14
to ansible...@googlegroups.com
Are there any examples for a configuration like this?

I can't find good examples of modular, reusable roles which can be used in conjunction with other roles.

Michael DeHaan

unread,
Oct 13, 2014, 4:54:37 PM10/13/14
to ansible...@googlegroups.com
Easiest way would be to have a role like "foo_site" and include it more than once:

- hosts: webservers:
  roles:
    - { name: foo_site, a: 1, b: 2 }
    - { name: bar_site, c: 2, d: 3 }

etc



On Fri, Oct 10, 2014 at 12:17 PM, André <an...@andre4all.de> wrote:
Are there any examples for a configuration like this?

I can't find good examples of modular, reusable roles which can be used in conjunction with other roles.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/62bb2e27-8209-47de-ac18-cfd216db8fbc%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages