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
roles:
- { role: nginx-supervisor,
app_name: app,
app_nginx_hostname: app.domain.net,
app_service_command: app_executable --port 9000 }
nsbase_app_name: app,
nsbase_app_nginx_hostname: app.domain.net,
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
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]
(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
--------------
There are a few crucial variables that you need to configure for your particular WSGI powered app. They are broken up
into sections and described below:
There are a few crucial variables that you need to configure for your app. They are broken up into sections and
described below:
Note that all the variables have a prefix `nsbase_` to avoid naming conflicts with other roles.
### App Settings
- app_name:
- The name of WSGI app to manage via this role.
- nsbase_app_name:
- The name of app to manage via this 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.
- Default: web_root_path/app_name_webapp
- Default: nsbase_web_root_path/nsbase_app_name_webapp
### NGINX
- app_nginx_hostname:
- nsbase_app_nginx_hostname:
- The DNS hostname that the application serves.
- Default: localhost
- app_nginx_app_static:
- nsbase_app_nginx_app_static:
- 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_service_command:
- nsbase_app_service_command:
- 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.
- 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,
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.
- 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.
- 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
----------------------
@ -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
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.
- Default: current user
- nginx_app_conf:
- nsbase_nginx_app_conf:
- The filename of the NGINX configuration for the app.
- Default: app_name_nginx.conf
- supervisor_app_config:
- Default: nsbase_app_name_nginx.conf
- nsbase_supervisor_app_config:
- The filename of the supervisor configuration for the app.
- Default: app_name_supervisor.conf
- app_service_name:
- The name of the service setup for the app according to supervisor.
- Default: app_name
- Default: nsbase_app_name_supervisor.conf
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_path: /srv/www
web_server_group: www-data
nsbase_web_root_path: /srv/www
nsbase_web_server_group: www-data
app_name: app
app_root_path: "{{ web_root_path }}/{{ app_name }}_webapp"
nsbase_app_name: app
nsbase_app_root_path: "{{ nsbase_web_root_path }}/{{ nsbase_app_name }}_webapp"
# NGINX
app_nginx_hostname: localhost
app_nginx_static_path: "{{ app_root_path }}/{{ app_name }}/static/"
nsbase_app_nginx_hostname: localhost
nsbase_app_nginx_static_path: "{{ nsbase_app_root_path }}/{{ nsbase_app_name }}/static/"

View File

@ -4,13 +4,13 @@
sudo: yes
- 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
- name: link to enable nginx configuration
file: state=link
src=/etc/nginx/sites-available/{{ nginx_app_config }}
path=/etc/nginx/sites-enabled/{{ nginx_app_config }}
src=/etc/nginx/sites-available/{{ nsbase_nginx_app_config }}
path=/etc/nginx/sites-enabled/{{ nsbase_nginx_app_config }}
sudo: yes
- name: disable default configuration
@ -20,7 +20,7 @@
- name: set nginx directories permissions
file: path={{ item }} state=directory
group={{ web_server_group }} mode=0755
group={{ nsbase_web_server_group }} mode=0755
with_items:
- /var/log/nginx
- /etc/nginx
@ -30,7 +30,7 @@
- name: set nginx logs permissions
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:
- access.log
- error.log

View File

@ -4,7 +4,7 @@
sudo: yes
- 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:
- /etc/supervisor
- /etc/supervisor/conf.d
@ -16,10 +16,10 @@
option={{ item.key }} value={{ item.value }}
with_dict:
chmod: "0770"
chown: root:{{ web_server_group }}
chown: root:{{ nsbase_web_server_group }}
sudo: yes
- 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
notify: start supervisord

View File

@ -1,12 +1,12 @@
---
- name: setup webapp deployment folder with the correct permissions
file: path={{ web_root_path }} state=directory
owner={{ web_server_group }} group={{ web_server_group }} mode=0774
file: path={{ nsbase_web_root_path }} state=directory
owner={{ nsbase_web_server_group }} group={{ nsbase_web_server_group }} mode=0774
sudo: yes
- name: setup webapp deployment configuration + log folders with the correct permissions
file: path={{ web_root_path }}/{{ item }} state=directory
owner={{ web_server_group }} group={{ web_server_group }} mode=0774
file: path={{ nsbase_web_root_path }}/{{ item }} state=directory
owner={{ nsbase_web_server_group }} group={{ nsbase_web_server_group }} mode=0774
with_items:
- config
- config/uwsgi
@ -16,9 +16,9 @@
- name: link remote folders with webapp home folder
file: state=link
path={{ web_root_path }}/{{ item.value }}
path={{ nsbase_web_root_path }}/{{ item.value }}
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:
/etc/nginx: config/nginx
/etc/supervisor: config/supervisor
@ -27,5 +27,5 @@
sudo: yes
- 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

View File

@ -1,17 +1,19 @@
server {
server_name {{ app_nginx_hostname }};
access_log {{ web_root_path }}/logs/nginx/{{ app_name }}-access.log;
error_log {{ web_root_path }}/logs/nginx/{{ app_name }}-error.log info;
server_name {{ nsbase_app_nginx_hostname }};
access_log {{ nsbase_web_root_path }}/logs/nginx/{{ nsbase_app_name }}-access.log;
error_log {{ nsbase_web_root_path }}/logs/nginx/{{ nsbase_app_name }}-error.log info;
location /static/ {
alias {{ app_nginx_static_path }};
alias {{ nsbase_app_nginx_static_path }};
}
location / {
try_files $uri @{{app_name}}_app;
try_files $uri @{{ nsbase_app_name }}_app;
}
location @{{app_name}}_app {
# TODO: Find way to stitch together templates.
location @{{ nsbase_app_name }}_app {
{% 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 }}
[program:{{ app_service_name }}]
user={{ web_server_group }}
command={{ app_service_command }}
stopsignal={{ app_service_stop_signal }}
stdout_logfile = {{ web_root_path }}/logs/supervisor/{{ app_name }}-application.log
; UWSGI for {{ nsbase_app_name }}
[program:{{ nsbase_app_service_name }}]
user={{ nsbase_web_server_group }}
command={{ nsbase_app_service_command }}
stopsignal={{ nsbase_app_service_stop_signal }}
stdout_logfile = {{ nsbase_web_root_path }}/logs/supervisor/{{ nsbase_app_name }}-application.log
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

View File

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