Remove heavy code (nginx filename) using a filter

pull/35/head
Emilien Mantel 2017-10-26 15:33:00 +02:00
parent 608784ca55
commit 4b3b857733
3 changed files with 28 additions and 8 deletions

View File

@ -0,0 +1,20 @@
def nginx_site_filename(site):
if site.has_key('filename'):
return site['filename']
else:
return nginx_site_name(site)
def nginx_site_name(site):
if isinstance(site['name'], list):
return site['name'][0]
else:
return site['name']
class FilterModule(object):
''' Nginx module '''
def filters(self):
return {
'nginx_site_filename': nginx_site_filename,
'nginx_site_name': nginx_site_name
}

View File

@ -2,7 +2,7 @@
- name: FAIL | Check filenames - name: FAIL | Check filenames
fail: fail:
msg: "Forbidden keyword default on site {{ item.name if item.name is string else item.name.0 }}" msg: "Forbidden keyword default on site {{ item | nginx_site_name }}"
when: item.filename is defined and item.filename == 'default' when: item.filename is defined and item.filename == 'default'
with_items: "{{ nginx_sites }}" with_items: "{{ nginx_sites }}"
@ -21,7 +21,7 @@
- name: FILE | Create root public folders (foreach nginx_sites) - name: FILE | Create root public folders (foreach nginx_sites)
file: file:
path: "{{ nginx_root }}/{{ item.filename | default(item.name if item.name is string else item.name.0) }}/public" path: "{{ nginx_root }}/{{ item | nginx_site_filename }}/public"
state: directory state: directory
owner: "{{ item.owner | default(nginx_user) }}" owner: "{{ item.owner | default(nginx_user) }}"
group: "{{ item.group | default(nginx_user) }}" group: "{{ item.group | default(nginx_user) }}"
@ -36,14 +36,14 @@
- name: TEMPLATE | Create sites - name: TEMPLATE | Create sites
template: template:
src: "etc/nginx/sites-available/{{ item.template if item.redirect_to is not defined else '_redirect' }}.j2" src: "etc/nginx/sites-available/{{ item.template if item.redirect_to is not defined else '_redirect' }}.j2"
dest: "{{ nginx_etc_dir }}/sites-available/{{ item.filename | default(item.name if item.name is string else item.name.0) }}" dest: "{{ nginx_etc_dir }}/sites-available/{{ item | nginx_site_filename }}"
with_items: "{{ nginx_sites }}" with_items: "{{ nginx_sites }}"
notify: ['reload nginx', 'restart nginx freebsd'] notify: ['reload nginx', 'restart nginx freebsd']
when: item.state is not defined or item.state != 'absent' when: item.state is not defined or item.state != 'absent'
- name: FILE | Delete sites - name: FILE | Delete sites
file: file:
path: "{{ nginx_etc_dir }}/{{ item.1 }}/{{ item.0.filename | default(item.0.name if item.0.name is string else item.0.name.0) }}" path: "{{ nginx_etc_dir }}/{{ item.1 }}/{{ item.0 | nginx_site_filename }}"
state: absent state: absent
with_nested: with_nested:
- "{{ nginx_sites }}" - "{{ nginx_sites }}"
@ -53,8 +53,8 @@
- name: FILE | Enable sites - name: FILE | Enable sites
file: file:
src: "{{ nginx_etc_dir }}/sites-available/{{ item.filename | default(item.name if item.name is string else item.name.0) }}" src: "{{ nginx_etc_dir }}/sites-available/{{ item | nginx_site_filename }}"
dest: "{{ nginx_etc_dir }}/sites-enabled/{{ item.filename | default(item.name if item.name is string else item.name.0) }}" dest: "{{ nginx_etc_dir }}/sites-enabled/{{ item | nginx_site_filename }}"
state: link state: link
with_items: "{{ nginx_sites }}" with_items: "{{ nginx_sites }}"
notify: ['reload nginx', 'restart nginx freebsd'] notify: ['reload nginx', 'restart nginx freebsd']
@ -63,7 +63,7 @@
- name: FILE | Disable sites - name: FILE | Disable sites
file: file:
path: "{{ nginx_etc_dir}}/sites-enabled/{{ item.filename | default(item.name if item.name is string else item.name.0) }}" path: "{{ nginx_etc_dir}}/sites-enabled/{{ item | nginx_site_filename }}"
state: absent state: absent
with_items: "{{ nginx_sites }}" with_items: "{{ nginx_sites }}"
notify: ['reload nginx', 'restart nginx freebsd'] notify: ['reload nginx', 'restart nginx freebsd']

View File

@ -1,5 +1,5 @@
{% set __proto = item.proto | default(['http']) %} {% set __proto = item.proto | default(['http']) %}
{% set __main_name = item.filename | default(item.name if item.name is string else item.name[0]) %} {% set __main_name = item | nginx_site_filename %}
{% set __listen = item.listen | default([80]) %} {% set __listen = item.listen | default([80]) %}
{% set __listen_ssl = item.listen_ssl | default([443]) %} {% set __listen_ssl = item.listen_ssl | default([443]) %}
{% set __http_proxy_protocol_port = item.http_proxy_protocol_port | default([]) %} {% set __http_proxy_protocol_port = item.http_proxy_protocol_port | default([]) %}