commit inicial
This commit is contained in:
commit
e97e40e318
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
*~
|
||||
\#*
|
||||
.#*
|
||||
*.pyc
|
||||
*.bak
|
||||
__pycache__
|
96
README.md
Normal file
96
README.md
Normal file
@ -0,0 +1,96 @@
|
||||
# Rol nginx-docker
|
||||
|
||||
Este rol configura un container `nginx` en un host con Docker.
|
||||
Genera la siguiente estructura de directorio para configurar nginx:
|
||||
|
||||
* `{{ nginx_config }}`: en este directorio se puede sobreescribir la
|
||||
configuración de nginx (sección `http`) mediante archivos `.conf`.
|
||||
|
||||
* `00_ssl.conf` (no editar): configura los certificados para HTTPS.
|
||||
|
||||
* `10_server.conf` (no editar): configura los servicios HTTP y HTTPS.
|
||||
|
||||
* `common`: directorio con directivas comunes (locations, logs,
|
||||
headers, etc) a incluir en las secciones server de HTTP y HTTPS.
|
||||
|
||||
* `locations`: directorio con locations (o cualquier directiva
|
||||
válida) a incluir en la sección `server` para HTTPS.
|
||||
|
||||
* `locations-http`: directorio con locations (o cualquier directiva
|
||||
válida) a incluir en la sección `server` para HTTP.
|
||||
|
||||
## Variables
|
||||
|
||||
* `nginx_image`: nombre de la imagen a bajar. Por defecto: `nginx`.
|
||||
|
||||
* `nginx_ssl_certificate`: path (en el host) al certificado SSL a
|
||||
usar para el servicio HTTPS.
|
||||
Valor por defecto: `/etc/ssl/certs/host-rectorado.pem`.
|
||||
|
||||
* `nginx_ssl_private_key`: path (en el host) a la clave privada
|
||||
del certificado SSL a usar para el servicio HTTPS.
|
||||
Valor por defecto: `/etc/ssl/private/host-rectorado.key`.
|
||||
|
||||
* `nginx_config_volume`: nombre (path) del volumen de configuración.
|
||||
Valor por defecto: `nginx_conf`, lo que significa que el volumen se
|
||||
montará en `/var/lib/docker/volumes/nginx_conf/_data`.
|
||||
|
||||
* `nginx_webroot_volume`: nombre (path) del volumen raíz (directiva
|
||||
root de nginx).
|
||||
Valor por defecto: `nginx_webroot`, lo que significa que el volumen
|
||||
se montará en `/var/lib/docker/volumes/nginx_webroot/_data`.
|
||||
|
||||
* `nginx_publish_ports`: puertos a publicar (sintaxis Docker).
|
||||
Valor por defecto: `[ "80:80", "443:443" ]`
|
||||
|
||||
* `nginx_redirect_https`: indica si forzar redireccion a https.
|
||||
Valor por defecto: `yes`.
|
||||
|
||||
## Variables exportadas
|
||||
|
||||
* `nginx_config_mountpoint`: punto de montaje de la configuración de nginx.
|
||||
Valor por defecto: `/var/lib/docker/volumes/nginx_conf/_data`.
|
||||
|
||||
* `nginx_webroot_mountpoint`: punto de montaje del webroot de nginx.
|
||||
Valor por defecto: `/var/lib/docker/volumes/nginx_webroot/_data`.
|
||||
|
||||
## Handlers
|
||||
|
||||
El rol define el siguiente handler, que puede ser usado por los roles
|
||||
subsiguientes:
|
||||
|
||||
* `restart nginx container`: reinicia el container. Útil para recargar
|
||||
la configuración.
|
||||
|
||||
## Ejemplo de uso
|
||||
|
||||
En el repositorio sobre el que estamos trabajando, agregar este rol
|
||||
como submódulo:
|
||||
|
||||
```bash
|
||||
git submodule add https://servicios.unl.edu.ar/gitlab/administradores/ansible-role-nginx-docker.git roles/nginx-docker
|
||||
```
|
||||
|
||||
En el playbook, incluir el rol:
|
||||
|
||||
```yaml
|
||||
# ...
|
||||
roles:
|
||||
# .. roles basicos ..
|
||||
- nginx-docker
|
||||
# .. roles de la aplicacion ..
|
||||
```
|
||||
|
||||
En los roles de la aplicacion, definir una tarea que configura una
|
||||
`location` para ser publicada mediante nginx:
|
||||
|
||||
```yaml
|
||||
- copy:
|
||||
content: |
|
||||
location / {
|
||||
proxy_pass http://NOMBRE-DE-CONTAINER:PUERTO-DE-CONTAINER;
|
||||
}
|
||||
dest: "{{ nginx_config_mountpoint }}/locations/MI-APLICACION.conf"
|
||||
notify: "restart nginx container"
|
||||
|
||||
```
|
21
defaults/main.yml
Normal file
21
defaults/main.yml
Normal file
@ -0,0 +1,21 @@
|
||||
---
|
||||
# nombre de la imagen a bajar
|
||||
nginx_image: nginx
|
||||
|
||||
# certificado y clave privadas para HTTPS
|
||||
nginx_ssl_certificate: /etc/ssl/certs/host-rectorado.pem
|
||||
nginx_ssl_private_key: /etc/ssl/private/host-rectorado.key
|
||||
|
||||
# nombre del volumen de configuracion
|
||||
nginx_config_volume: nginx_conf
|
||||
|
||||
# nombre del volumen web
|
||||
nginx_webroot_volume: nginx_webroot
|
||||
|
||||
# puertos a publicar
|
||||
nginx_publish_ports:
|
||||
- "80:80"
|
||||
- "443:443"
|
||||
|
||||
# forzar redireccion a https?
|
||||
nginx_redirect_https: yes
|
6
handlers/main.yml
Normal file
6
handlers/main.yml
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
- name: restart nginx container
|
||||
docker_container:
|
||||
name: nginx
|
||||
state: started
|
||||
restart: yes
|
74
tasks/main.yml
Normal file
74
tasks/main.yml
Normal file
@ -0,0 +1,74 @@
|
||||
---
|
||||
- name: "Activar container nginx"
|
||||
docker_container:
|
||||
name: "nginx"
|
||||
state: "started"
|
||||
restart_policy: "unless-stopped"
|
||||
image: "{{ nginx_image }}"
|
||||
volumes:
|
||||
- "{{ nginx_ssl_certificate }}:/etc/nginx/server.crt"
|
||||
- "{{ nginx_ssl_private_key }}:/etc/nginx/server.key"
|
||||
- "{{ nginx_config_volume }}:/etc/nginx/conf.d/"
|
||||
- "{{ nginx_webroot_volume }}:/usr/share/nginx/html/"
|
||||
networks:
|
||||
- name: "{{ docker_network_name }}"
|
||||
ports: "{{ nginx_publish_ports }}"
|
||||
env:
|
||||
register: "container"
|
||||
|
||||
- name: "Configurar volumen {{ nginx_config_volume }}"
|
||||
docker_volume:
|
||||
name: "{{ nginx_config_volume }}"
|
||||
state: "present"
|
||||
register: "st_c_volume"
|
||||
|
||||
- name: "Configurar volumen {{ nginx_webroot_volume }}"
|
||||
docker_volume:
|
||||
name: "{{ nginx_webroot_volume }}"
|
||||
state: "present"
|
||||
register: "st_w_volume"
|
||||
|
||||
# exportar punto de montaje del volumen
|
||||
- set_fact:
|
||||
nginx_config_mountpoint: "{{ st_c_volume.ansible_facts.docker_volume.Mountpoint }}"
|
||||
nginx_webroot_mountpoint: "{{ st_w_volume.ansible_facts.docker_volume.Mountpoint }}"
|
||||
|
||||
# TODO: creo que estas tareas se deberian hacer desde dentro de un container
|
||||
- copy:
|
||||
content: |
|
||||
ssl_certificate /etc/nginx/server.crt;
|
||||
ssl_certificate_key /etc/nginx/server.key;
|
||||
dest: "{{ nginx_config_mountpoint }}/00_ssl.conf"
|
||||
notify: "restart nginx container"
|
||||
|
||||
- copy:
|
||||
content: |
|
||||
server {
|
||||
listen 80;
|
||||
server_name _;
|
||||
root /usr/share/nginx/html;
|
||||
include /etc/nginx/conf.d/common/*.conf;
|
||||
{% if nginx_redirect_https %}
|
||||
location / {
|
||||
rewrite ^ https://$http_host$request_uri permanent;
|
||||
}
|
||||
{% endif %}
|
||||
include /etc/nginx/conf.d/locations-http/*.conf;
|
||||
}
|
||||
server {
|
||||
listen 443 ssl;
|
||||
server_name _;
|
||||
root /usr/share/nginx/html;
|
||||
include /etc/nginx/conf.d/common/*.conf;
|
||||
include /etc/nginx/conf.d/locations/*.conf;
|
||||
}
|
||||
dest: "{{ nginx_config_mountpoint }}/10_server.conf"
|
||||
notify: "restart nginx container"
|
||||
|
||||
- file:
|
||||
name: "{{ nginx_config_mountpoint }}/{{ item }}"
|
||||
state: "directory"
|
||||
loop:
|
||||
- common
|
||||
- locations
|
||||
- locations-http
|
Loading…
x
Reference in New Issue
Block a user