diff --git a/.travis.yml b/.travis.yml index 0dc5197..5df2ad7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,10 @@ matrix: distribution: centos version: 6 playbook: basic + - env: + distribution: centos + version: 6 + playbook: http-template - env: distribution: centos version: 6 @@ -23,7 +27,7 @@ matrix: - env: distribution: centos version: 6 - playbook: template + playbook: stream-template - env: distribution: centos version: 6 @@ -32,6 +36,10 @@ matrix: distribution: centos version: 7 playbook: basic + - env: + distribution: centos + version: 7 + playbook: http-template - env: distribution: centos version: 7 @@ -43,11 +51,11 @@ matrix: - env: distribution: centos version: 7 - playbook: template + playbook: stable - env: distribution: centos version: 7 - playbook: stable + playbook: stream-template - env: distribution: centos version: 7 @@ -56,6 +64,10 @@ matrix: distribution: debian version: jessie playbook: basic + - env: + distribution: debian + version: jessie + playbook: http-template - env: distribution: debian version: jessie @@ -67,11 +79,11 @@ matrix: - env: distribution: debian version: jessie - playbook: template + playbook: stable - env: distribution: debian version: jessie - playbook: stable + playbook: stream-template - env: distribution: debian version: jessie @@ -80,6 +92,10 @@ matrix: distribution: debian version: stretch playbook: basic + - env: + distribution: debian + version: stretch + playbook: http-template - env: distribution: debian version: stretch @@ -91,11 +107,11 @@ matrix: - env: distribution: debian version: stretch - playbook: template + playbook: stable - env: distribution: debian version: stretch - playbook: stable + playbook: stream-template - env: distribution: debian version: stretch @@ -104,6 +120,10 @@ matrix: distribution: ubuntu version: trusty playbook: basic + - env: + distribution: ubuntu + version: trusty + playbook: http-template - env: distribution: ubuntu version: trusty @@ -115,15 +135,19 @@ matrix: - env: distribution: ubuntu version: trusty - playbook: template + playbook: stable - env: distribution: ubuntu version: trusty - playbook: stable + playbook: stream-template - env: distribution: ubuntu version: xenial playbook: basic + - env: + distribution: ubuntu + version: xenial + playbook: http-template - env: distribution: ubuntu version: xenial @@ -135,11 +159,11 @@ matrix: - env: distribution: ubuntu version: xenial - playbook: template + playbook: stable - env: distribution: ubuntu version: xenial - playbook: stable + playbook: stream-template - env: distribution: ubuntu version: xenial @@ -148,6 +172,10 @@ matrix: distribution: ubuntu version: artful playbook: basic + - env: + distribution: ubuntu + version: artful + playbook: http-template - env: distribution: ubuntu version: artful @@ -163,7 +191,7 @@ matrix: - env: distribution: ubuntu version: artful - playbook: template + playbook: stream-template - env: distribution: ubuntu version: artful @@ -172,6 +200,10 @@ matrix: distribution: ubuntu version: bionic playbook: basic + - env: + distribution: ubuntu + version: bionic + playbook: http-template - env: distribution: ubuntu version: bionic @@ -187,7 +219,7 @@ matrix: - env: distribution: ubuntu version: bionic - playbook: template + playbook: stream-template - env: distribution: ubuntu version: bionic diff --git a/README.md b/README.md index f0c4d3a..4c60638 100644 --- a/README.md +++ b/README.md @@ -366,8 +366,37 @@ nginx_http_template: health_check: max_fails=1 fail_timeout=10s # 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_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 @@ -405,11 +434,14 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a - hosts: localhost become: true roles: - - ansible-role-nginx + - role: nginxinc.nginx vars: nginx_http_template_enable: true nginx_http_template: 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 @@ -420,6 +452,8 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a location: / html_file_location: /usr/share/nginx/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. @@ -429,11 +463,18 @@ This is a sample playbook file for deploying the Ansible Galaxy NGINX role in a - hosts: localhost become: true roles: - - nginxinc.nginx + - role: nginxinc.nginx vars: nginx_http_template_enable: true nginx_http_template: 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: locations: frontend: diff --git a/defaults/main.yml b/defaults/main.yml index 5659334..1fb4f04 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -197,5 +197,34 @@ nginx_http_template: health_check: max_fails=1 fail_timeout=10s # 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_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 diff --git a/tasks/conf/template-config.yml b/tasks/conf/template-config.yml index c3b8d80..496b809 100644 --- a/tasks/conf/template-config.yml +++ b/tasks/conf/template-config.yml @@ -39,16 +39,16 @@ - name: "(Setup: All NGINX) Ensure NGINX Stream Directory Exists" file: - path: /etc/nginx/conf.d/stream + path: "{{ item.value.conf_file_location }}" state: directory + with_dict: "{{ nginx_stream_template }}" when: nginx_stream_template_enable - name: "(Setup: All NGINX) Dynamically Generate NGINX Stream Configuration Files" template: - src: "{{ item }}" - dest: /etc/nginx/conf.d/stream/{{ item | basename | regex_replace('\.j2','') }} + src: "{{ item.value.template_file }}" + dest: "{{ item.value.conf_file_location }}/{{ item.value.conf_file_name }}" backup: yes - with_fileglob: - - "../templates/stream/*.j2" + with_dict: "{{ nginx_stream_template }}" when: nginx_stream_template_enable notify: "(Handler: All OSs) Reload NGINX" diff --git a/templates/http/default.conf.j2 b/templates/http/default.conf.j2 index 84cb4f5..9aa80c6 100644 --- a/templates/http/default.conf.j2 +++ b/templates/http/default.conf.j2 @@ -12,7 +12,6 @@ upstream {{ item.value.upstreams[upstream].name }} { sticky cookie srv_id expires=1h path=/; {% endif %} } - {% endfor %} {% endif %} diff --git a/templates/stream/default.conf.j2 b/templates/stream/default.conf.j2 new file mode 100644 index 0000000..442485d --- /dev/null +++ b/templates/stream/default.conf.j2 @@ -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 %} diff --git a/templates/stream/stream.conf.j2 b/templates/stream/stream.conf.j2 deleted file mode 100644 index cb16569..0000000 --- a/templates/stream/stream.conf.j2 +++ /dev/null @@ -1,5 +0,0 @@ -# {{ ansible_managed }} - -server { - listen {{ nginx_stream_template_listen }}; -} diff --git a/tests/playbooks/nginx-template.yml b/tests/playbooks/nginx-http-template.yml similarity index 96% rename from tests/playbooks/nginx-template.yml rename to tests/playbooks/nginx-http-template.yml index b995b22..fe9c5e9 100644 --- a/tests/playbooks/nginx-template.yml +++ b/tests/playbooks/nginx-http-template.yml @@ -23,7 +23,7 @@ location: /backend proxy_pass: http://backend_servers/ upstreams: - upstream_1: + frontend_upstream: name: frontend_servers lb_method: least_conn zone_name: frontend @@ -35,7 +35,7 @@ port: 8081 weight: 1 health_check: max_fails=3 fail_timeout=5s - upstream_2: + backend_upstream: name: backend_servers lb_method: least_conn zone_name: backend @@ -57,7 +57,7 @@ autoindex: false web_server: locations: - default: + frontend_site: location: / html_file_location: /usr/share/nginx/html html_file_name: frontend_index.html @@ -73,7 +73,7 @@ autoindex: false web_server: locations: - default: + backend_site: location: / html_file_location: /usr/share/nginx/html html_file_name: backend_index.html diff --git a/tests/playbooks/nginx-stream-template.yml b/tests/playbooks/nginx-stream-template.yml new file mode 100644 index 0000000..90fd94c --- /dev/null +++ b/tests/playbooks/nginx-stream-template.yml @@ -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