diff --git a/docs/rolereference.rst b/docs/rolereference.rst index 6fb9da20f2964a33a342d6ae54d52b268029e5cb..b97e94c5798904a1f92822768431580540e240cd 100644 --- a/docs/rolereference.rst +++ b/docs/rolereference.rst @@ -1460,6 +1460,11 @@ Parameters be served with ``Strict-Transport-Security`` header with value of ``max-age=31536000; includeSubDomains``. +**environment_variables** (dict, optional, ``{}``) + Specify additional environment variables that should be set for running the + service. Environment variables will be set in both the systemd service and for + the application's administrator user (when logged in as one). + **fqdn** (string, mandatory) Fully-qualified domain name where the website is reachable. This value is used for calculating the user/group name for dedicated website user, as well as @@ -1536,6 +1541,8 @@ running a bare Django project): virtualenv_packages: - django wsgi_application: django_example_com.wsgi:application + environment_variables: + DJANGO_SETTINGS_MODULE: "django_example_com.settings.production" https_tls_key: "{{ lookup('file', inventory_dir + '/tls/wsgi.example.com_https.key') }}" https_tls_certificate: "{{ lookup('file', inventory_dir + '/tls/wsgi.example.com_https.pem') }}" futures_version: 3.0.5 diff --git a/docs/usage.rst b/docs/usage.rst index 61538833808e24e68334d3e4b71b6733320b701d..e6de992bc1a27046e637c2112e8735a6883b0ce9 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -1472,6 +1472,9 @@ on the safe side: will be automatically activated for you. * WSGI applications are executed via *Gunicorn*. The WSGI server listens on a Unix socket, making the socket accessible by *Nginx*. +* If you ever need to set some environment variables, this can easily be done + via the ``environment_variables`` role parameter. This particular example does + not set any, though. * Static content is served directly by *Nginx*. * Each web application gets distinct sub-directory under ``/var/www``, named after the FQDN. All sub-directories created under there are created with diff --git a/roles/wsgi_website/defaults/main.yml b/roles/wsgi_website/defaults/main.yml index 892275b9e5cb3d40ebed15954a0b8188604ebe25..741fb6bc4983c35763b9f779d545dee8e98c2a78 100644 --- a/roles/wsgi_website/defaults/main.yml +++ b/roles/wsgi_website/defaults/main.yml @@ -7,6 +7,7 @@ rewrites: [] static_locations: [] use_paste: False virtualenv_packages: [] +environment_variables: {} admin: "web-{{ fqdn | replace('.', '_') }}" https_tls_certificate: "{{ lookup('file', tls_certificate_dir + '/' + fqdn + '_https.pem') }}" https_tls_key: "{{ lookup('file', tls_private_key_dir + '/' + fqdn + '_https.key') }}" diff --git a/roles/wsgi_website/tasks/main.yml b/roles/wsgi_website/tasks/main.yml index 0badd1a7214353d1d702cd577495d72e39258322..8d562b1f7df93515df3073d0bb8fccb5b2efd7f3 100644 --- a/roles/wsgi_website/tasks/main.yml +++ b/roles/wsgi_website/tasks/main.yml @@ -20,6 +20,10 @@ copy: src="profile_virtualenv.sh" dest="{{ home }}/.profile.d/virtualenv.sh" owner="root" group="{{ user }}" mode="640" +- name: Deploy profile configuration file for setting environment variables + template: src="environment.sh.j2" dest="{{ home }}/.profile.d/environment.sh" + owner="root" group="{{ user }}" mode=640 + - name: Create WSGI website user user: name="{{ user }}" uid="{{ uid | default(omit) }}" group="{{ user }}" comment="umask=0007" system=yes createhome=no state=present diff --git a/roles/wsgi_website/templates/environment.sh.j2 b/roles/wsgi_website/templates/environment.sh.j2 new file mode 100644 index 0000000000000000000000000000000000000000..f5af0acd1fa8bef138717ff8bf924ca531707e88 --- /dev/null +++ b/roles/wsgi_website/templates/environment.sh.j2 @@ -0,0 +1,3 @@ +{% for var, val in environment_variables.iteritems() %} +export {{ var }}='{{ val }}' +{% endfor %} diff --git a/roles/wsgi_website/templates/systemd_wsgi_website.service.j2 b/roles/wsgi_website/templates/systemd_wsgi_website.service.j2 index fdd2f72148619d750e6f96285078a5366ad9b958..0169468a301f35b5b58a9e572f96992302c12116 100644 --- a/roles/wsgi_website/templates/systemd_wsgi_website.service.j2 +++ b/roles/wsgi_website/templates/systemd_wsgi_website.service.j2 @@ -9,10 +9,14 @@ Group={{ user }} WorkingDirectory={{ home }}/code ExecStart={{ home }}/virtualenv/bin/gunicorn --bind unix:/run/wsgi/{{ fqdn }}.sock {% if use_paste %}--paste {{home}}/code/{{ wsgi_application }}{% else %}{{ wsgi_application }}{% endif %} +{% for var, val in environment_variables.iteritems() %} +Environment="{{ var }}={{ val }}" +{% endfor %} + ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s TERM $MAINPID PrivateTmp=true UMask=0007 [Install] -WantedBy=multi-user.target \ No newline at end of file +WantedBy=multi-user.target diff --git a/testsite/playbooks/roles/wsgihello/files/hello.wsgi b/testsite/playbooks/roles/wsgihello/files/hello.wsgi index 307a0f246d6a171099bc08fee7c8cacdcd34692e..79b68a048e89f7588ae16d5e2ca932f66dd20f55 100644 --- a/testsite/playbooks/roles/wsgihello/files/hello.wsgi +++ b/testsite/playbooks/roles/wsgihello/files/hello.wsgi @@ -1,8 +1,10 @@ #!/usr/bin/env python +import os + def application(environ, start_response): status = '200 OK' - output = 'Hello, world one!' + output = 'Hello, world one! I am website %s' % os.environ.get("WEBSITE_NAME", "that nobody set a name for :(") response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] diff --git a/testsite/playbooks/roles/wsgihello/meta/main.yml b/testsite/playbooks/roles/wsgihello/meta/main.yml index 9e894e1c3a5e7625222113ea148e46687313d24f..e583920bb9a937c621a41e20acab96ab70d1f054 100644 --- a/testsite/playbooks/roles/wsgihello/meta/main.yml +++ b/testsite/playbooks/roles/wsgihello/meta/main.yml @@ -10,6 +10,8 @@ dependencies: - /static/ https_tls_key: "{{ lookup('file', inventory_dir + '/tls/wsgi.' + testsite_domain + '_https.key') }}" https_tls_certificate: "{{ lookup('file', inventory_dir + '/tls/wsgi.' + testsite_domain + '_https.pem') }}" + environment_variables: + WEBSITE_NAME: "Majic Ansible Roles Test Site" - role: database db_name: wsgi_{{ testsite_domain_underscores }} db_password: wsgi_{{ testsite_domain_underscores }} \ No newline at end of file