Basic stream template (#92)

* Add Stream template defaults
* Add stream default config template
* Added stream template variables
* Changed Ansible task to use new stream template
This commit is contained in:
Timothy Allen 2019-02-06 07:39:48 -05:00 committed by Grzegorz Dzien
parent f79e6d0210
commit 3a66e10a17
9 changed files with 231 additions and 32 deletions

View File

@ -8,6 +8,10 @@ matrix:
distribution: centos distribution: centos
version: 6 version: 6
playbook: basic playbook: basic
- env:
distribution: centos
version: 6
playbook: http-template
- env: - env:
distribution: centos distribution: centos
version: 6 version: 6
@ -23,7 +27,7 @@ matrix:
- env: - env:
distribution: centos distribution: centos
version: 6 version: 6
playbook: template playbook: stream-template
- env: - env:
distribution: centos distribution: centos
version: 6 version: 6
@ -32,6 +36,10 @@ matrix:
distribution: centos distribution: centos
version: 7 version: 7
playbook: basic playbook: basic
- env:
distribution: centos
version: 7
playbook: http-template
- env: - env:
distribution: centos distribution: centos
version: 7 version: 7
@ -43,11 +51,11 @@ matrix:
- env: - env:
distribution: centos distribution: centos
version: 7 version: 7
playbook: template playbook: stable
- env: - env:
distribution: centos distribution: centos
version: 7 version: 7
playbook: stable playbook: stream-template
- env: - env:
distribution: centos distribution: centos
version: 7 version: 7
@ -56,6 +64,10 @@ matrix:
distribution: debian distribution: debian
version: jessie version: jessie
playbook: basic playbook: basic
- env:
distribution: debian
version: jessie
playbook: http-template
- env: - env:
distribution: debian distribution: debian
version: jessie version: jessie
@ -67,11 +79,11 @@ matrix:
- env: - env:
distribution: debian distribution: debian
version: jessie version: jessie
playbook: template playbook: stable
- env: - env:
distribution: debian distribution: debian
version: jessie version: jessie
playbook: stable playbook: stream-template
- env: - env:
distribution: debian distribution: debian
version: jessie version: jessie
@ -80,6 +92,10 @@ matrix:
distribution: debian distribution: debian
version: stretch version: stretch
playbook: basic playbook: basic
- env:
distribution: debian
version: stretch
playbook: http-template
- env: - env:
distribution: debian distribution: debian
version: stretch version: stretch
@ -91,11 +107,11 @@ matrix:
- env: - env:
distribution: debian distribution: debian
version: stretch version: stretch
playbook: template playbook: stable
- env: - env:
distribution: debian distribution: debian
version: stretch version: stretch
playbook: stable playbook: stream-template
- env: - env:
distribution: debian distribution: debian
version: stretch version: stretch
@ -104,6 +120,10 @@ matrix:
distribution: ubuntu distribution: ubuntu
version: trusty version: trusty
playbook: basic playbook: basic
- env:
distribution: ubuntu
version: trusty
playbook: http-template
- env: - env:
distribution: ubuntu distribution: ubuntu
version: trusty version: trusty
@ -115,15 +135,19 @@ matrix:
- env: - env:
distribution: ubuntu distribution: ubuntu
version: trusty version: trusty
playbook: template playbook: stable
- env: - env:
distribution: ubuntu distribution: ubuntu
version: trusty version: trusty
playbook: stable playbook: stream-template
- env: - env:
distribution: ubuntu distribution: ubuntu
version: xenial version: xenial
playbook: basic playbook: basic
- env:
distribution: ubuntu
version: xenial
playbook: http-template
- env: - env:
distribution: ubuntu distribution: ubuntu
version: xenial version: xenial
@ -135,11 +159,11 @@ matrix:
- env: - env:
distribution: ubuntu distribution: ubuntu
version: xenial version: xenial
playbook: template playbook: stable
- env: - env:
distribution: ubuntu distribution: ubuntu
version: xenial version: xenial
playbook: stable playbook: stream-template
- env: - env:
distribution: ubuntu distribution: ubuntu
version: xenial version: xenial
@ -148,6 +172,10 @@ matrix:
distribution: ubuntu distribution: ubuntu
version: artful version: artful
playbook: basic playbook: basic
- env:
distribution: ubuntu
version: artful
playbook: http-template
- env: - env:
distribution: ubuntu distribution: ubuntu
version: artful version: artful
@ -163,7 +191,7 @@ matrix:
- env: - env:
distribution: ubuntu distribution: ubuntu
version: artful version: artful
playbook: template playbook: stream-template
- env: - env:
distribution: ubuntu distribution: ubuntu
version: artful version: artful
@ -172,6 +200,10 @@ matrix:
distribution: ubuntu distribution: ubuntu
version: bionic version: bionic
playbook: basic playbook: basic
- env:
distribution: ubuntu
version: bionic
playbook: http-template
- env: - env:
distribution: ubuntu distribution: ubuntu
version: bionic version: bionic
@ -187,7 +219,7 @@ matrix:
- env: - env:
distribution: ubuntu distribution: ubuntu
version: bionic version: bionic
playbook: template playbook: stream-template
- env: - env:
distribution: ubuntu distribution: ubuntu
version: bionic version: bionic

View File

@ -366,8 +366,37 @@ nginx_http_template:
health_check: max_fails=1 fail_timeout=10s health_check: max_fails=1 fail_timeout=10s
# Enable creating dynamic templated NGINX stream configuration files. # Enable creating dynamic templated NGINX stream configuration files.
# Defaults will not produce a valid configuration. Instead they are meant to showcase
# the options available for templating. Each key represents a new configuration file.
nginx_stream_template_enable: false nginx_stream_template_enable: false
nginx_stream_template_listen: 12345 nginx_stream_template:
default:
template_file: stream/default.conf.j2
conf_file_name: default.conf
conf_file_location: /etc/nginx/conf.d/stream/
network_streams:
default:
listen_address: localhost
listen_port: 80
udp_enable: false
proxy_pass: backend
proxy_timeout: 3s
proxy_connect_timeout: 1s
proxy_protocol: false
health_check_plus: false
upstreams:
upstream1:
name: backend
lb_method: least_conn
zone_name: backend
zone_size: 64k
sticky_cookie: false
servers:
server1:
address: localhost
port: 8080
weight: 1
health_check: max_fails=1 fail_timeout=10s
``` ```
Dependencies Dependencies
@ -405,11 +434,14 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a
- hosts: localhost - hosts: localhost
become: true become: true
roles: roles:
- ansible-role-nginx - role: nginxinc.nginx
vars: vars:
nginx_http_template_enable: true nginx_http_template_enable: true
nginx_http_template: nginx_http_template:
default: default:
template_file: http/default.conf.j2
conf_file_name: default.conf
conf_file_location: /etc/nginx/conf.d/
port: 80 port: 80
server_name: localhost server_name: localhost
error_page: /usr/share/nginx/html error_page: /usr/share/nginx/html
@ -420,6 +452,8 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a
location: / location: /
html_file_location: /usr/share/nginx/html html_file_location: /usr/share/nginx/html
html_file_name: index.html html_file_name: index.html
autoindex: false
http_demo_conf: false
``` ```
This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost and installing the open source version of NGINX as a reverse proxy. This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a localhost and installing the open source version of NGINX as a reverse proxy.
@ -429,11 +463,18 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a
- hosts: localhost - hosts: localhost
become: true become: true
roles: roles:
- nginxinc.nginx - role: nginxinc.nginx
vars: vars:
nginx_http_template_enable: true nginx_http_template_enable: true
nginx_http_template: nginx_http_template:
default: default:
template_file: http/default.conf.j2
conf_file_name: default.conf
conf_file_location: /etc/nginx/conf.d/
port: 80
server_name: localhost
error_page: /usr/share/nginx/html
autoindex: false
reverse_proxy: reverse_proxy:
locations: locations:
frontend: frontend:

View File

@ -197,5 +197,34 @@ nginx_http_template:
health_check: max_fails=1 fail_timeout=10s health_check: max_fails=1 fail_timeout=10s
# Enable creating dynamic templated NGINX stream configuration files. # Enable creating dynamic templated NGINX stream configuration files.
# Defaults will not produce a valid configuration. Instead they are meant to showcase
# the options available for templating. Each key represents a new configuration file.
nginx_stream_template_enable: false nginx_stream_template_enable: false
nginx_stream_template_listen: 12345 nginx_stream_template:
default:
template_file: stream/default.conf.j2
conf_file_name: default.conf
conf_file_location: /etc/nginx/conf.d/stream/
network_streams:
default:
listen_address: localhost
listen_port: 80
udp_enable: false
proxy_pass: backend
proxy_timeout: 3s
proxy_connect_timeout: 1s
proxy_protocol: false
health_check_plus: false
upstreams:
upstream1:
name: backend
lb_method: least_conn
zone_name: backend
zone_size: 64k
sticky_cookie: false
servers:
server1:
address: localhost
port: 8080
weight: 1
health_check: max_fails=1 fail_timeout=10s

View File

@ -39,16 +39,16 @@
- name: "(Setup: All NGINX) Ensure NGINX Stream Directory Exists" - name: "(Setup: All NGINX) Ensure NGINX Stream Directory Exists"
file: file:
path: /etc/nginx/conf.d/stream path: "{{ item.value.conf_file_location }}"
state: directory state: directory
with_dict: "{{ nginx_stream_template }}"
when: nginx_stream_template_enable when: nginx_stream_template_enable
- name: "(Setup: All NGINX) Dynamically Generate NGINX Stream Configuration Files" - name: "(Setup: All NGINX) Dynamically Generate NGINX Stream Configuration Files"
template: template:
src: "{{ item }}" src: "{{ item.value.template_file }}"
dest: /etc/nginx/conf.d/stream/{{ item | basename | regex_replace('\.j2','') }} dest: "{{ item.value.conf_file_location }}/{{ item.value.conf_file_name }}"
backup: yes backup: yes
with_fileglob: with_dict: "{{ nginx_stream_template }}"
- "../templates/stream/*.j2"
when: nginx_stream_template_enable when: nginx_stream_template_enable
notify: "(Handler: All OSs) Reload NGINX" notify: "(Handler: All OSs) Reload NGINX"

View File

@ -12,7 +12,6 @@ upstream {{ item.value.upstreams[upstream].name }} {
sticky cookie srv_id expires=1h path=/; sticky cookie srv_id expires=1h path=/;
{% endif %} {% endif %}
} }
{% endfor %} {% endfor %}
{% endif %} {% endif %}

View File

@ -0,0 +1,50 @@
# {{ ansible_managed }}
{% if item.value.upstreams is defined %}
{% for upstream in item.value.upstreams %}
upstream {{ item.value.upstreams[upstream].name }} {
{% if item.value.upstreams[upstream].lb_method is defined %}
{{ item.value.upstreams[upstream].lb_method }};
{% endif %}
zone {{ item.value.upstreams[upstream].zone_name }} {{ item.value.upstreams[upstream].zone_size }};
{% for server in item.value.upstreams[upstream].servers %}
server {{ item.value.upstreams[upstream].servers[server].address }}:{{ item.value.upstreams[upstream].servers[server].port }} weight={{ item.value.upstreams[upstream].servers[server].weight|default("1") }} {{ item.value.upstreams[upstream].servers[server].health_check|default("") }};
{% endfor %}
{% if item.value.upstreams[upstream].sticky_cookie is defined %}
{% if item.value.upstreams[upstream].sticky_cookie %}
sticky cookie srv_id expires=1h path=/;
{% endif %}
{% endif %}
}
{% endfor %}
{% endif %}
{% if item.value.network_streams is defined %}
{% for stream in item.value.network_streams %}
server {
{% if item.value.network_streams[stream].listen_address is defined and item.value.network_streams[stream].listen_port is defined %}
{% if item.value.network_streams[stream].udp_enable %}
listen {{ item.value.network_streams[stream].listen_address }}:{{ item.value.network_streams[stream].listen_port }} udp;
{% else %}
listen {{ item.value.network_streams[stream].listen_address }}:{{ item.value.network_streams[stream].listen_port }};
{% endif %}
{% elif item.value.network_streams[stream].listen_port is defined %}
{% if item.value.network_streams[stream].udp_enable %}
listen {{ item.value.network_streams[stream].listen_port }} udp;
{% else %}
listen {{ item.value.network_streams[stream].listen_port }};
{% endif %}
{% endif %}
proxy_pass {{ item.value.network_streams[stream].proxy_pass }};
proxy_timeout {{ item.value.network_streams[stream].proxy_timeout }};
proxy_connect_timeout {{ item.value.network_streams[stream].proxy_connect_timeout }};
{% if item.value.network_streams[stream].proxy_protocol %}
proxy_protocol on;
{% else %}
proxy_protocol off;
{% endif %}
{% if item.value.network_streams[stream].health_check_plus %}
health_check;
{% endif %}
}
{% endfor %}
{% endif %}

View File

@ -1,5 +0,0 @@
# {{ ansible_managed }}
server {
listen {{ nginx_stream_template_listen }};
}

View File

@ -23,7 +23,7 @@
location: /backend location: /backend
proxy_pass: http://backend_servers/ proxy_pass: http://backend_servers/
upstreams: upstreams:
upstream_1: frontend_upstream:
name: frontend_servers name: frontend_servers
lb_method: least_conn lb_method: least_conn
zone_name: frontend zone_name: frontend
@ -35,7 +35,7 @@
port: 8081 port: 8081
weight: 1 weight: 1
health_check: max_fails=3 fail_timeout=5s health_check: max_fails=3 fail_timeout=5s
upstream_2: backend_upstream:
name: backend_servers name: backend_servers
lb_method: least_conn lb_method: least_conn
zone_name: backend zone_name: backend
@ -57,7 +57,7 @@
autoindex: false autoindex: false
web_server: web_server:
locations: locations:
default: frontend_site:
location: / location: /
html_file_location: /usr/share/nginx/html html_file_location: /usr/share/nginx/html
html_file_name: frontend_index.html html_file_name: frontend_index.html
@ -73,7 +73,7 @@
autoindex: false autoindex: false
web_server: web_server:
locations: locations:
default: backend_site:
location: / location: /
html_file_location: /usr/share/nginx/html html_file_location: /usr/share/nginx/html
html_file_name: backend_index.html html_file_name: backend_index.html

View File

@ -0,0 +1,53 @@
---
- hosts: localhost
become: true
remote_user: root
roles:
- ansible-role-nginx
vars:
nginx_main_template_enable: true
nginx_main_template:
template_file: nginx.conf.j2
conf_file_name: nginx.conf
conf_file_location: /etc/nginx/
user: nginx
worker_processes: auto
error_level: warn
worker_connections: 1024
http_enable: false
http_settings:
keepalive_timeout: 65
cache: false
rate_limit: false
keyval: false
stream_enable: true
http_global_autoindex: false
nginx_stream_template_enable: true
nginx_stream_template:
default:
template_file: stream/default.conf.j2
conf_file_name: default.conf
conf_file_location: /etc/nginx/conf.d/stream
network_streams:
app:
listen_address: localhost
listen_port: 80
udp_enable: false
proxy_pass: backend
proxy_timeout: 3s
proxy_connect_timeout: 1s
proxy_protocol: false
health_check_plus: false
upstreams:
backend_upstream:
name: backend
lb_method: least_conn
zone_name: backend
zone_size: 64k
sticky_cookie: false
servers:
backend_server_1:
address: localhost
port: 8080
weight: 1
health_check: max_fails=1 fail_timeout=10s