From 36cd4d3aec652a74871bc6b4edcb15ab2b7cdbde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Sch=C3=B6ldstr=C3=B6m?= Date: Sat, 25 Mar 2017 15:46:37 -0500 Subject: [PATCH] Allow users to extend config files through template inheritance --- defaults/main.yml | 3 +++ tasks/main.yml | 2 +- tasks/vhosts.yml | 2 +- templates/nginx.conf.j2 | 14 ++++++++++++++ templates/vhost.j2 | 15 +++++++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index eb0d3b5..68d60c1 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -12,6 +12,9 @@ nginx_ppa_version: stable # The name of the nginx apt/yum package to install. nginx_package_name: "nginx" +nginx_conf_template: "nginx.conf.j2" +nginx_vhost_template: "vhost.j2" + nginx_worker_processes: "{{ ansible_processor_vcpus | default(ansible_processor_count) }}" nginx_worker_connections: "1024" nginx_multi_accept: "off" diff --git a/tasks/main.yml b/tasks/main.yml index 6c420fe..74ca563 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -30,7 +30,7 @@ # Nginx setup. - name: Copy nginx configuration in place. template: - src: nginx.conf.j2 + src: "{{ nginx_conf_template }}" dest: "{{ nginx_conf_file_path }}" owner: root group: "{{ root_group }}" diff --git a/tasks/vhosts.yml b/tasks/vhosts.yml index 8448503..e6dd5f6 100644 --- a/tasks/vhosts.yml +++ b/tasks/vhosts.yml @@ -14,7 +14,7 @@ - name: Add managed vhost config files. template: - src: vhost.j2 + src: "{{ nginx_vhost_template }}" dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf" force: yes owner: root diff --git a/templates/nginx.conf.j2 b/templates/nginx.conf.j2 index 262e54a..4ec1de1 100644 --- a/templates/nginx.conf.j2 +++ b/templates/nginx.conf.j2 @@ -3,18 +3,25 @@ user {{ nginx_user }}; error_log {{ nginx_error_log }}; pid {{ nginx_pidfile }}; +{% block worker %} worker_processes {{ nginx_worker_processes }}; +{% endblock %} +{% block events %} events { worker_connections {{ nginx_worker_connections }}; multi_accept {{ nginx_multi_accept }}; } +{% endblock %} {% if nginx_extra_conf_options %} {{ nginx_extra_conf_options }} {% endif %} http { + {% block http_begin %}{% endblock %} + +{% block http_basic %} include {{ nginx_mime_file_path }}; default_type application/octet-stream; @@ -39,11 +46,13 @@ http { {% if nginx_proxy_cache_path %} proxy_cache_path {{ nginx_proxy_cache_path }}; {% endif %} +{% endblock %} {% if nginx_extra_http_options %} {{ nginx_extra_http_options|indent(4, False) }} {% endif %} +{% block http_upstream %} {% for upstream in nginx_upstreams %} upstream {{ upstream.name }} { {% if upstream.strategy is defined %} @@ -57,9 +66,14 @@ http { {% endif %} } {% endfor %} +{% endblock %} +{% block http_includes %} include {{ nginx_conf_path }}/*.conf; {% if nginx_conf_path != nginx_vhost_path %} include {{ nginx_vhost_path }}/*; {% endif %} +{% endblock %} + + {% block http_end %}{% endblock %} } diff --git a/templates/vhost.j2 b/templates/vhost.j2 index 35e3de1..40beced 100644 --- a/templates/vhost.j2 +++ b/templates/vhost.j2 @@ -1,4 +1,16 @@ +{% block server_redirect %} +{% if item.server_name_redirect is defined %} + listen {{ item.listen | default('80') }}; + server_name {{ item.server_name_redirect }}; + return 301 $scheme://{{ item.server_name.split(' ')[0] }}$request_uri; +} +{% endif %} +{% endblock %} + server { + {% block server_begin %}{% endblock %} + + {% block server_basic -%} listen {{ item.listen | default('80') }}; {% if item.server_name is defined %} @@ -24,6 +36,9 @@ server { {% if item.return is defined %} return {{ item.return }}; {% endif %} + {% endblock %} + + {% block server_end %}{% endblock %} {% if item.extra_parameters is defined %} {{ item.extra_parameters|indent(4) }}