From d82765b396bff2c6c2b4ae4f931fc7ac7f3b2988 Mon Sep 17 00:00:00 2001 From: David Lundgren Date: Thu, 27 Apr 2017 17:32:57 -0500 Subject: [PATCH 1/2] Allow combined vhosts file --- defaults/main.yml | 4 ++++ tasks/vhosts-combined.yml | 20 ++++++++++++++++++++ tasks/vhosts-individual.yml | 26 ++++++++++++++++++++++++++ tasks/vhosts.yml | 26 +------------------------- templates/vhosts.j2 | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 25 deletions(-) create mode 100644 tasks/vhosts-combined.yml create mode 100644 tasks/vhosts-individual.yml create mode 100644 templates/vhosts.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 5161e82..2e96fd0 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -15,6 +15,10 @@ nginx_package_name: "nginx" nginx_conf_template: "nginx.conf.j2" nginx_vhost_template: "vhost.j2" +# Whether not to use a combined or individual vhost files (individual | combined) +nginx_vhost_style: "individual" +__nginx_vhosts_filename: "vhosts.conf" + nginx_worker_processes: "{{ ansible_processor_vcpus | default(ansible_processor_count) }}" nginx_worker_connections: "1024" nginx_multi_accept: "off" diff --git a/tasks/vhosts-combined.yml b/tasks/vhosts-combined.yml new file mode 100644 index 0000000..6932095 --- /dev/null +++ b/tasks/vhosts-combined.yml @@ -0,0 +1,20 @@ +--- +- name: Detect combined vhost filename + set_fact: + nginx_vhosts_filename: "{{ __nginx_vhosts_filename }}" + when: nginx_vhosts_filename is not defined + +- name: Add managed vhost config file (if any vhosts are configured). + template: + src: vhosts.j2 + dest: "{{ nginx_vhost_path }}/{{ nginx_vhosts_filename }}" + mode: 0644 + when: nginx_vhosts|length > 0 + notify: reload nginx + +- name: Remove managed vhost config file (if no vhosts are configured). + file: + path: "{{ nginx_vhost_path }}/{{ nginx_vhosts_filename }}" + state: absent + when: nginx_vhosts|length == 0 + notify: reload nginx \ No newline at end of file diff --git a/tasks/vhosts-individual.yml b/tasks/vhosts-individual.yml new file mode 100644 index 0000000..0136ad9 --- /dev/null +++ b/tasks/vhosts-individual.yml @@ -0,0 +1,26 @@ +--- +- name: Add managed vhost config files. + template: + src: "{{ item.template|default(nginx_vhost_template) }}" + dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf" + force: yes + owner: root + group: root + mode: 0644 + when: item.state|default('present') != 'absent' + with_items: "{{ nginx_vhosts }}" + notify: reload nginx + +- name: Remove managed vhost config files. + file: + path: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf" + state: absent + when: item.state|default('present') == 'absent' + with_items: "{{ nginx_vhosts }}" + notify: reload nginx + +- name: Remove legacy vhosts.conf file. + file: + path: "{{ nginx_vhost_path }}/vhosts.conf" + state: absent + notify: reload nginx \ No newline at end of file diff --git a/tasks/vhosts.yml b/tasks/vhosts.yml index 03c32d9..64e70f1 100644 --- a/tasks/vhosts.yml +++ b/tasks/vhosts.yml @@ -12,28 +12,4 @@ state: directory notify: reload nginx -- name: Add managed vhost config files. - template: - src: "{{ item.template|default(nginx_vhost_template) }}" - dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf" - force: yes - owner: root - group: root - mode: 0644 - when: item.state|default('present') != 'absent' - with_items: "{{ nginx_vhosts }}" - notify: reload nginx - -- name: Remove managed vhost config files. - file: - path: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf" - state: absent - when: item.state|default('present') == 'absent' - with_items: "{{ nginx_vhosts }}" - notify: reload nginx - -- name: Remove legacy vhosts.conf file. - file: - path: "{{ nginx_vhost_path }}/vhosts.conf" - state: absent - notify: reload nginx +- include: vhosts-{{ nginx_vhost_style }}.yml diff --git a/templates/vhosts.j2 b/templates/vhosts.j2 new file mode 100644 index 0000000..4b9ed32 --- /dev/null +++ b/templates/vhosts.j2 @@ -0,0 +1,33 @@ +{% for vhost in nginx_vhosts %} +server { + listen {{ vhost.listen | default('80 default_server') }}; + +{% if vhost.server_name is defined %} + server_name {{ vhost.server_name }}; +{% endif %} + +{% if vhost.root is defined %} + root {{ vhost.root }}; +{% endif %} + + index {{ vhost.index | default('index.html index.htm') }}; + +{% if vhost.error_page is defined %} + error_page {{ vhost.error_page }}; +{% endif %} +{% if vhost.access_log is defined %} + access_log {{ vhost.access_log }}; +{% endif %} +{% if vhost.error_log is defined %} + error_log {{ vhost.error_log }} error; +{% endif %} + +{% if vhost.return is defined %} + return {{ vhost.return }}; +{% endif %} + +{% if vhost.extra_parameters is defined %} + {{ vhost.extra_parameters|indent(4) }} +{% endif %} +} +{% endfor %} \ No newline at end of file From 41979991e5b5fbc181e9f038f9f2710a1f85e614 Mon Sep 17 00:00:00 2001 From: David Lundgren Date: Thu, 27 Apr 2017 17:41:45 -0500 Subject: [PATCH 2/2] Quote the variable on the include --- tasks/vhosts-combined.yml | 2 +- tasks/vhosts-individual.yml | 2 +- tasks/vhosts.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tasks/vhosts-combined.yml b/tasks/vhosts-combined.yml index 6932095..adf0bf2 100644 --- a/tasks/vhosts-combined.yml +++ b/tasks/vhosts-combined.yml @@ -17,4 +17,4 @@ path: "{{ nginx_vhost_path }}/{{ nginx_vhosts_filename }}" state: absent when: nginx_vhosts|length == 0 - notify: reload nginx \ No newline at end of file + notify: reload nginx diff --git a/tasks/vhosts-individual.yml b/tasks/vhosts-individual.yml index 0136ad9..90d5f18 100644 --- a/tasks/vhosts-individual.yml +++ b/tasks/vhosts-individual.yml @@ -23,4 +23,4 @@ file: path: "{{ nginx_vhost_path }}/vhosts.conf" state: absent - notify: reload nginx \ No newline at end of file + notify: reload nginx diff --git a/tasks/vhosts.yml b/tasks/vhosts.yml index 64e70f1..520c3d9 100644 --- a/tasks/vhosts.yml +++ b/tasks/vhosts.yml @@ -12,4 +12,4 @@ state: directory notify: reload nginx -- include: vhosts-{{ nginx_vhost_style }}.yml +- include: "vhosts-{{ nginx_vhost_style }}.yml"