Explorar o código

Write vhost configurations to separate config files

Oskar Schöldström %!s(int64=8) %!d(string=hai) anos
pai
achega
f8a04dd544
Modificáronse 3 ficheiros con 43 adicións e 40 borrados
  1. 12 7
      tasks/vhosts.yml
  2. 31 0
      templates/vhost.j2
  3. 0 33
      templates/vhosts.j2

+ 12 - 7
tasks/vhosts.yml

@@ -12,17 +12,22 @@
     state: directory
   notify: reload nginx
 
-- name: Add managed vhost config file (if any vhosts are configured).
+- name: Add managed vhost config files.
   template:
-    src: vhosts.j2
-    dest: "{{ nginx_vhost_path }}/{{ nginx_vhosts_filename }}"
+    src: vhost.j2
+    dest: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf"
+    force: yes
+    owner: root
+    group: root
     mode: 0644
-  when: nginx_vhosts|length > 0
+  when: "{{ item.state|default('present') != 'absent' }}"
+  with_items: "{{ nginx_vhosts }}"
   notify: reload nginx
 
-- name: Remove managed vhost config file (if no vhosts are configured).
+- name: Remove managed vhost config files.
   file:
-    path: "{{ nginx_vhost_path }}/{{ nginx_vhosts_filename }}"
+    path: "{{ nginx_vhost_path }}/{{ item.server_name.split(' ')[0] }}.conf"
     state: absent
-  when: nginx_vhosts|length == 0
+  when: "{{ item.state|default('present') == 'absent' }}"
+  with_items: "{{ nginx_vhosts }}"
   notify: reload nginx

+ 31 - 0
templates/vhost.j2

@@ -0,0 +1,31 @@
+server {
+    listen {{ item.listen | default('80 default_server') }};
+
+{% if item.server_name is defined %}
+    server_name {{ item.server_name }};
+{% endif %}
+
+{% if item.root is defined %}
+    root {{ item.root }};
+{% endif %}
+
+    index {{ item.index | default('index.html index.htm') }};
+
+{% if item.error_page is defined %}
+    error_page {{ item.error_page }};
+{% endif %}
+{% if item.access_log is defined %}
+    access_log {{ item.access_log }};
+{% endif %}
+{% if item.error_log is defined %}
+    error_log {{ item.error_log }} error;
+{% endif %}
+
+{% if item.return is defined %}
+    return {{ item.return }};
+{% endif %}
+
+{% if item.extra_parameters is defined %}
+    {{ item.extra_parameters|indent(4) }}
+{% endif %}
+}

+ 0 - 33
templates/vhosts.j2

@@ -1,33 +0,0 @@
-{% 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 %}