Add in variable prefix to avoid issues with name collisions in other roles.

This commit is contained in:
Dorian 2016-05-14 10:56:30 -04:00
parent 26a93b9558
commit 5a039eb419
8 changed files with 73 additions and 68 deletions

View File

@ -25,14 +25,15 @@ app_name, app_nginx_hostname and app_service_command parameters especially.
sudo: yes sudo: yes
roles: roles:
- { role: nginx-supervisor, - { role: nginx-supervisor,
app_name: app, nsbase_app_name: app,
app_nginx_hostname: app.domain.net, nsbase_app_nginx_hostname: app.domain.net,
app_service_command: app_executable --port 9000 } nsbase_app_service_command: app_executable --port 9000 }
The role itself is very configurable. For exaple, if you prefer the location of the web application to refer to the The role itself is very configurable. For exaple, if you prefer the location of the web application to refer to the
domain name rather than the default root path, then simply override the app_root_path variable with something like: domain name rather than the default root path, then simply override the nsbase_app_root_path variable with something
like:
app_rooth_path: "{{ web_root_path }}/{{ app_nginx_hostname }}" nsbase_app_rooth_path: "{{ nsbase_web_root_path }}/{{ nsbase_app_nginx_hostname }}"
A comprehensive example can be found in the [Ansible rookeries role] A comprehensive example can be found in the [Ansible rookeries role]
(https://bitbucket.org/dorianpula/ansible-rookeries) that uses this role as a base to deploy a Flask-based webapp. (https://bitbucket.org/dorianpula/ansible-rookeries) that uses this role as a base to deploy a Flask-based webapp.
@ -40,32 +41,34 @@ A comprehensive example can be found in the [Ansible rookeries role]
Role Variables Role Variables
-------------- --------------
There are a few crucial variables that you need to configure for your particular WSGI powered app. They are broken up There are a few crucial variables that you need to configure for your app. They are broken up into sections and
into sections and described below: described below:
Note that all the variables have a prefix `nsbase_` to avoid naming conflicts with other roles.
### App Settings ### App Settings
- app_name: - nsbase_app_name:
- The name of WSGI app to manage via this role. - The name of app to manage via this role.
- Used as a prefix through out the role. - Used as a prefix through out the role.
- app_root_path: - nsbase_app_root_path:
- The path to the root folder of the app. - The path to the root folder of the app.
- Default: web_root_path/app_name_webapp - Default: nsbase_web_root_path/nsbase_app_name_webapp
### NGINX ### NGINX
- app_nginx_hostname: - nsbase_app_nginx_hostname:
- The DNS hostname that the application serves. - The DNS hostname that the application serves.
- Default: localhost - Default: localhost
- app_nginx_app_static: - nsbase_app_nginx_app_static:
- The path to the static elements of the site (templates, CSS, images, etc.) - The path to the static elements of the site (templates, CSS, images, etc.)
- Default: app_root_path/app_name/static/ - Default: nsbase_app_root_path/nsbase_app_name/static/
### App Specific ### App Specific
- app_service_command: - nsbase_app_service_command:
- The full command to execute to run the application - The full command to execute to run the application
- app_service_stop_signal: - nsbase_app_service_stop_signal:
- The signal used to gracefully stop the application. - The signal used to gracefully stop the application.
- Default: QUIT (as in SIGQUIT) - Default: QUIT (as in SIGQUIT)
@ -74,12 +77,15 @@ into sections and described below:
These variables are not as crucial to configure. They do give good defaults for configuring the system in a consistent, These variables are not as crucial to configure. They do give good defaults for configuring the system in a consistent,
POSIX/LSB-friendly and user-friendly manner. See the section on Default File Structure for more details. POSIX/LSB-friendly and user-friendly manner. See the section on Default File Structure for more details.
- web_root_path: - nsbase_web_root_path:
- The root of the entire web app structure include configuration and logging. - The root of the entire web app structure include configuration and logging.
- Default: /srv/www - Default: /srv/www
- web_server_group: - nsbase_web_server_group:
- The user group responsible for starting, stopping and managing the web and app servers on the target machine. - The user group responsible for starting, stopping and managing the web and app servers on the target machine.
- Default: www-data - Default: www-data
- nsbase_app_service_name:
- The name of the service setup for the app according to supervisor.
- Default: nsbase_app_name
Default File Structure Default File Structure
---------------------- ----------------------
@ -102,18 +108,15 @@ Internal Variables
The following variables are part of the internals of the role. However if you really want to, you can tweak them to The following variables are part of the internals of the role. However if you really want to, you can tweak them to
work with your setup: work with your setup:
- web_user: - nsbase_web_user:
- The non-root user who is allowed to control web + app servers on the target machine. - The non-root user who is allowed to control web + app servers on the target machine.
- Default: current user - Default: current user
- nginx_app_conf: - nsbase_nginx_app_conf:
- The filename of the NGINX configuration for the app. - The filename of the NGINX configuration for the app.
- Default: app_name_nginx.conf - Default: nsbase_app_name_nginx.conf
- supervisor_app_config: - nsbase_supervisor_app_config:
- The filename of the supervisor configuration for the app. - The filename of the supervisor configuration for the app.
- Default: app_name_supervisor.conf - Default: nsbase_app_name_supervisor.conf
- app_service_name:
- The name of the service setup for the app according to supervisor.
- Default: app_name
License License
------- -------

View File

@ -1,13 +1,13 @@
--- ---
# defaults file for ansible-nginx-uwsgi-supervisor # defaults file for ansible-nginx-supervisor
# Web root paths + app name + home # Web root paths + app name + home
web_root_path: /srv/www nsbase_web_root_path: /srv/www
web_server_group: www-data nsbase_web_server_group: www-data
app_name: app nsbase_app_name: app
app_root_path: "{{ web_root_path }}/{{ app_name }}_webapp" nsbase_app_root_path: "{{ nsbase_web_root_path }}/{{ nsbase_app_name }}_webapp"
# NGINX # NGINX
app_nginx_hostname: localhost nsbase_app_nginx_hostname: localhost
app_nginx_static_path: "{{ app_root_path }}/{{ app_name }}/static/" nsbase_app_nginx_static_path: "{{ nsbase_app_root_path }}/{{ nsbase_app_name }}/static/"

View File

@ -4,13 +4,13 @@
sudo: yes sudo: yes
- name: configure nginx - name: configure nginx
template: src=app_nginx.conf dest=/etc/nginx/sites-available/{{ nginx_app_config }} template: src=app_nginx.conf dest=/etc/nginx/sites-available/{{ nsbase_nginx_app_config }}
sudo: yes sudo: yes
- name: link to enable nginx configuration - name: link to enable nginx configuration
file: state=link file: state=link
src=/etc/nginx/sites-available/{{ nginx_app_config }} src=/etc/nginx/sites-available/{{ nsbase_nginx_app_config }}
path=/etc/nginx/sites-enabled/{{ nginx_app_config }} path=/etc/nginx/sites-enabled/{{ nsbase_nginx_app_config }}
sudo: yes sudo: yes
- name: disable default configuration - name: disable default configuration
@ -20,7 +20,7 @@
- name: set nginx directories permissions - name: set nginx directories permissions
file: path={{ item }} state=directory file: path={{ item }} state=directory
group={{ web_server_group }} mode=0755 group={{ nsbase_web_server_group }} mode=0755
with_items: with_items:
- /var/log/nginx - /var/log/nginx
- /etc/nginx - /etc/nginx
@ -30,7 +30,7 @@
- name: set nginx logs permissions - name: set nginx logs permissions
file: path=/var/log/nginx/{{ item }} state=file file: path=/var/log/nginx/{{ item }} state=file
owner={{ web_server_group }} group={{ web_server_group }} mode=0644 owner={{ nsbase_web_server_group }} group={{ nsbase_web_server_group }} mode=0644
with_items: with_items:
- access.log - access.log
- error.log - error.log

View File

@ -4,7 +4,7 @@
sudo: yes sudo: yes
- name: set permissions on supervisor config and log directories - name: set permissions on supervisor config and log directories
file: path={{ item }} group={{ web_server_group }} state=directory mode=0755 file: path={{ item }} group={{ nsbase_web_server_group }} state=directory mode=0755
with_items: with_items:
- /etc/supervisor - /etc/supervisor
- /etc/supervisor/conf.d - /etc/supervisor/conf.d
@ -16,10 +16,10 @@
option={{ item.key }} value={{ item.value }} option={{ item.key }} value={{ item.value }}
with_dict: with_dict:
chmod: "0770" chmod: "0770"
chown: root:{{ web_server_group }} chown: root:{{ nsbase_web_server_group }}
sudo: yes sudo: yes
- name: upload supervisor configuration to web server home - name: upload supervisor configuration to web server home
template: src=app_supervisor.conf dest=/etc/supervisor/conf.d/{{ supervisor_app_config }} template: src=app_supervisor.conf dest=/etc/supervisor/conf.d/{{ nsbase_supervisor_app_config }}
sudo: yes sudo: yes
notify: start supervisord notify: start supervisord

View File

@ -1,12 +1,12 @@
--- ---
- name: setup webapp deployment folder with the correct permissions - name: setup webapp deployment folder with the correct permissions
file: path={{ web_root_path }} state=directory file: path={{ nsbase_web_root_path }} state=directory
owner={{ web_server_group }} group={{ web_server_group }} mode=0774 owner={{ nsbase_web_server_group }} group={{ nsbase_web_server_group }} mode=0774
sudo: yes sudo: yes
- name: setup webapp deployment configuration + log folders with the correct permissions - name: setup webapp deployment configuration + log folders with the correct permissions
file: path={{ web_root_path }}/{{ item }} state=directory file: path={{ nsbase_web_root_path }}/{{ item }} state=directory
owner={{ web_server_group }} group={{ web_server_group }} mode=0774 owner={{ nsbase_web_server_group }} group={{ nsbase_web_server_group }} mode=0774
with_items: with_items:
- config - config
- config/uwsgi - config/uwsgi
@ -16,9 +16,9 @@
- name: link remote folders with webapp home folder - name: link remote folders with webapp home folder
file: state=link file: state=link
path={{ web_root_path }}/{{ item.value }} path={{ nsbase_web_root_path }}/{{ item.value }}
src={{ item.key }} src={{ item.key }}
owner={{ web_server_group }} group={{ web_server_group }} mode=0774 owner={{ nsbase_web_server_group }} group={{ nsbase_web_server_group }} mode=0774
with_dict: with_dict:
/etc/nginx: config/nginx /etc/nginx: config/nginx
/etc/supervisor: config/supervisor /etc/supervisor: config/supervisor
@ -27,5 +27,5 @@
sudo: yes sudo: yes
- name: add user to webserver group - name: add user to webserver group
user: name={{ web_user }} append=yes groups={{ web_server_group }} user: name={{ nsbase_web_user }} append=yes groups={{ nsbase_web_server_group }}
sudo: yes sudo: yes

View File

@ -1,17 +1,19 @@
server { server {
server_name {{ app_nginx_hostname }}; server_name {{ nsbase_app_nginx_hostname }};
access_log {{ web_root_path }}/logs/nginx/{{ app_name }}-access.log; access_log {{ nsbase_web_root_path }}/logs/nginx/{{ nsbase_app_name }}-access.log;
error_log {{ web_root_path }}/logs/nginx/{{ app_name }}-error.log info; error_log {{ nsbase_web_root_path }}/logs/nginx/{{ nsbase_app_name }}-error.log info;
location /static/ { location /static/ {
alias {{ app_nginx_static_path }}; alias {{ nsbase_app_nginx_static_path }};
} }
location / { location / {
try_files $uri @{{app_name}}_app; try_files $uri @{{ nsbase_app_name }}_app;
} }
location @{{app_name}}_app { location @{{ nsbase_app_name }}_app {
# TODO: Find way to stitch together templates. {% block app_service_setup %}
# Include your setup to connect to the webapp setup here.
{% endblock %}
} }
} }

View File

@ -1,9 +1,9 @@
; UWSGI for {{ app_name }} ; UWSGI for {{ nsbase_app_name }}
[program:{{ app_service_name }}] [program:{{ nsbase_app_service_name }}]
user={{ web_server_group }} user={{ nsbase_web_server_group }}
command={{ app_service_command }} command={{ nsbase_app_service_command }}
stopsignal={{ app_service_stop_signal }} stopsignal={{ nsbase_app_service_stop_signal }}
stdout_logfile = {{ web_root_path }}/logs/supervisor/{{ app_name }}-application.log stdout_logfile = {{ nsbase_web_root_path }}/logs/supervisor/{{ nsbase_app_name }}-application.log
stdout_logfile_backups = 5 stdout_logfile_backups = 5
stderr_logfile = {{ web_root_path }}/logs/supervisor/{{ app_name }}-error.log stderr_logfile = {{ nsbase_web_root_path }}/logs/supervisor/{{ nsbase_app_name }}-error.log
stderr_logfile_backups = 5 stderr_logfile_backups = 5

View File

@ -2,15 +2,15 @@
# vars file for ansible-nginx-uwsgi-supervisor # vars file for ansible-nginx-uwsgi-supervisor
# User # User
web_user: "{{ ansible_env.SUDO_USER }}" nsbase_web_user: "{{ ansible_env.SUDO_USER }}"
# NGINX # NGINX
nginx_app_config: "{{ app_name }}_nginx.conf" nsbase_nginx_app_config: "{{ nsbase_app_name }}_nginx.conf"
# Supervisor # Supervisor
supervisor_app_config: "{{ app_name }}_supervisor.conf" nsbase_supervisor_app_config: "{{ nsbase_app_name }}_supervisor.conf"
# App specific configuration # App specific configuration
app_service_name: "{{ app_name }}" nsbase_app_service_name: "{{ nsbase_app_name }}"
app_service_command: "echo {{ app_name }}" nsbase_app_service_command: "echo {{ nsbase_app_name }}"
app_service_stop_signal: QUIT nsbase_app_service_stop_signal: QUIT