Allow users to extend config files through template inheritance

pull/92/head
Oskar Schöldström 7 years ago
parent c9046d9f3a
commit 36cd4d3aec
  1. 3
      defaults/main.yml
  2. 2
      tasks/main.yml
  3. 2
      tasks/vhosts.yml
  4. 14
      templates/nginx.conf.j2
  5. 15
      templates/vhost.j2

@ -12,6 +12,9 @@ nginx_ppa_version: stable
# The name of the nginx apt/yum package to install. # The name of the nginx apt/yum package to install.
nginx_package_name: "nginx" 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_processes: "{{ ansible_processor_vcpus | default(ansible_processor_count) }}"
nginx_worker_connections: "1024" nginx_worker_connections: "1024"
nginx_multi_accept: "off" nginx_multi_accept: "off"

@ -30,7 +30,7 @@
# Nginx setup. # Nginx setup.
- name: Copy nginx configuration in place. - name: Copy nginx configuration in place.
template: template:
src: nginx.conf.j2 src: "{{ nginx_conf_template }}"
dest: "{{ nginx_conf_file_path }}" dest: "{{ nginx_conf_file_path }}"
owner: root owner: root
group: "{{ root_group }}" group: "{{ root_group }}"

@ -14,7 +14,7 @@
- name: Add managed vhost config files. - name: Add managed vhost config files.
template: template:
src: vhost.j2 src: "{{ nginx_vhost_template }}"
dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf" dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf"
force: yes force: yes
owner: root owner: root

@ -3,18 +3,25 @@ user {{ nginx_user }};
error_log {{ nginx_error_log }}; error_log {{ nginx_error_log }};
pid {{ nginx_pidfile }}; pid {{ nginx_pidfile }};
{% block worker %}
worker_processes {{ nginx_worker_processes }}; worker_processes {{ nginx_worker_processes }};
{% endblock %}
{% block events %}
events { events {
worker_connections {{ nginx_worker_connections }}; worker_connections {{ nginx_worker_connections }};
multi_accept {{ nginx_multi_accept }}; multi_accept {{ nginx_multi_accept }};
} }
{% endblock %}
{% if nginx_extra_conf_options %} {% if nginx_extra_conf_options %}
{{ nginx_extra_conf_options }} {{ nginx_extra_conf_options }}
{% endif %} {% endif %}
http { http {
{% block http_begin %}{% endblock %}
{% block http_basic %}
include {{ nginx_mime_file_path }}; include {{ nginx_mime_file_path }};
default_type application/octet-stream; default_type application/octet-stream;
@ -39,11 +46,13 @@ http {
{% if nginx_proxy_cache_path %} {% if nginx_proxy_cache_path %}
proxy_cache_path {{ nginx_proxy_cache_path }}; proxy_cache_path {{ nginx_proxy_cache_path }};
{% endif %} {% endif %}
{% endblock %}
{% if nginx_extra_http_options %} {% if nginx_extra_http_options %}
{{ nginx_extra_http_options|indent(4, False) }} {{ nginx_extra_http_options|indent(4, False) }}
{% endif %} {% endif %}
{% block http_upstream %}
{% for upstream in nginx_upstreams %} {% for upstream in nginx_upstreams %}
upstream {{ upstream.name }} { upstream {{ upstream.name }} {
{% if upstream.strategy is defined %} {% if upstream.strategy is defined %}
@ -57,9 +66,14 @@ http {
{% endif %} {% endif %}
} }
{% endfor %} {% endfor %}
{% endblock %}
{% block http_includes %}
include {{ nginx_conf_path }}/*.conf; include {{ nginx_conf_path }}/*.conf;
{% if nginx_conf_path != nginx_vhost_path %} {% if nginx_conf_path != nginx_vhost_path %}
include {{ nginx_vhost_path }}/*; include {{ nginx_vhost_path }}/*;
{% endif %} {% endif %}
{% endblock %}
{% block http_end %}{% endblock %}
} }

@ -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 { server {
{% block server_begin %}{% endblock %}
{% block server_basic -%}
listen {{ item.listen | default('80') }}; listen {{ item.listen | default('80') }};
{% if item.server_name is defined %} {% if item.server_name is defined %}
@ -24,6 +36,9 @@ server {
{% if item.return is defined %} {% if item.return is defined %}
return {{ item.return }}; return {{ item.return }};
{% endif %} {% endif %}
{% endblock %}
{% block server_end %}{% endblock %}
{% if item.extra_parameters is defined %} {% if item.extra_parameters is defined %}
{{ item.extra_parameters|indent(4) }} {{ item.extra_parameters|indent(4) }}