2023-12-11 16:54:07 +01:00
|
|
|
---
|
|
|
|
- name: Find all .conf
|
|
|
|
ansible.builtin.find:
|
2025-01-31 13:14:07 +01:00
|
|
|
paths: "{{nginx.directories.http.servers}}"
|
2023-12-11 16:54:07 +01:00
|
|
|
patterns: '*.*.conf'
|
|
|
|
register: conf_files
|
|
|
|
|
2025-02-03 18:10:07 +01:00
|
|
|
# Filter all domains
|
|
|
|
|
2023-12-11 16:54:07 +01:00
|
|
|
- name: Filter domain names and remove .conf extension and path
|
|
|
|
set_fact:
|
2023-12-12 15:50:57 +01:00
|
|
|
filtered_domains: "{{ conf_files.files | map(attribute='path') | map('regex_search', domain_regex) | select('string') | map('regex_replace', path_regex, '') | map('regex_replace', '.conf$', '') | list }}"
|
2023-12-11 16:54:07 +01:00
|
|
|
vars:
|
2025-01-31 13:14:07 +01:00
|
|
|
domain_regex: "^{{nginx.directories.http.servers}}(?!www\\.)[^/]+\\.conf$"
|
|
|
|
path_regex: "^{{nginx.directories.http.servers}}"
|
2023-12-11 16:54:07 +01:00
|
|
|
|
2023-12-11 19:45:42 +01:00
|
|
|
- name: The domains for which a www. redirect will be implemented
|
2023-12-11 16:54:07 +01:00
|
|
|
debug:
|
|
|
|
var: filtered_domains
|
2025-02-03 18:10:07 +01:00
|
|
|
when: mode_debug | bool
|
|
|
|
|
|
|
|
# Routine for domains with primary domain included
|
|
|
|
|
|
|
|
- name: Set filtered_domains_with_primary_domain
|
|
|
|
set_fact:
|
|
|
|
filtered_domains_with_primary_domain: "{{ filtered_domains | select('search', primary_domain + '$') | list }}"
|
|
|
|
|
|
|
|
- name: Debug with primary domain
|
|
|
|
debug:
|
|
|
|
var: filtered_domains_with_primary_domain
|
|
|
|
when: mode_debug | bool
|
|
|
|
|
|
|
|
- name: Include nginx-domain-redirect role with dynamic domain mappings for domains with {{primary_domain}} included
|
|
|
|
include_role:
|
|
|
|
name: nginx-domain-redirect
|
|
|
|
vars:
|
|
|
|
domain_mappings: "{{ filtered_domains_with_primary_domain | map('regex_replace', '^(.*)$', '{ source: \"www.\\1\", target: \"\\1\" }') | map('from_yaml') | list }}"
|
|
|
|
when: not enable_wildcard_certificate | bool
|
2023-12-11 16:54:07 +01:00
|
|
|
|
2025-02-03 18:10:07 +01:00
|
|
|
- name: Include wildcard www. redirect for domains with {{primary_domain}} included
|
|
|
|
vars:
|
|
|
|
domain: "{{primary_domain}}"
|
|
|
|
template:
|
|
|
|
src: www.wildcard.conf.j2
|
2025-02-03 22:08:16 +01:00
|
|
|
dest: "{{nginx_www_wildcard_configuration}}"
|
2025-02-03 18:10:07 +01:00
|
|
|
notify: restart nginx
|
|
|
|
when: enable_wildcard_certificate | bool
|
|
|
|
|
|
|
|
# Routine for domains without the primary domain included
|
|
|
|
|
|
|
|
- name: Set filtered_domains_without_primary_domain
|
|
|
|
set_fact:
|
|
|
|
filtered_domains_without_primary_domain: "{{ filtered_domains | reject('search', primary_domain + '$') | list }}"
|
|
|
|
|
|
|
|
- name: Debug domains without primary domain
|
|
|
|
debug:
|
|
|
|
var: filtered_domains_without_primary_domain
|
|
|
|
when: mode_debug | bool
|
|
|
|
|
|
|
|
- name: Include nginx-domain-redirect role with dynamic domain mappings for domains without primary domain
|
2023-12-11 16:54:07 +01:00
|
|
|
include_role:
|
|
|
|
name: nginx-domain-redirect
|
|
|
|
vars:
|
2025-02-03 18:10:07 +01:00
|
|
|
domain_mappings: "{{ filtered_domains_without_primary_domain | map('regex_replace', '^(.*)$', '{ source: \"www.\\1\", target: \"\\1\" }') | map('from_yaml') | list }}"
|
|
|
|
|
|
|
|
|
|
|
|
# Cleanup
|
2025-02-03 22:08:16 +01:00
|
|
|
# Deactivated due to complexity
|
|
|
|
#- name: Cleanup dedicated nginx configurations for www redirect configuration
|
|
|
|
# file:
|
|
|
|
# path: "{{ nginx.directories.http.servers }}{{ item.source }}.conf"
|
|
|
|
# state: absent
|
|
|
|
# # Filter: Only first-level subdomains of primary_domain
|
|
|
|
# # Exclude the primary domain itself
|
|
|
|
# # Transform for www redirection
|
|
|
|
# loop: "{{ filtered_domains_with_primary_domain
|
|
|
|
# | select('regex_search', '^[^.]+\\.' ~ primary_domain ~ '$')
|
|
|
|
# | reject('equalto', primary_domain)
|
|
|
|
# | map('regex_replace', '^(.*)$', '{ source: \"www.\\1\", target: \"\\1\" }')
|
|
|
|
# | map('from_yaml')
|
|
|
|
# | list }}"
|
|
|
|
# notify: restart nginx
|
|
|
|
# when:
|
|
|
|
# - enable_wildcard_certificate | bool # Wildcard certificate must be enabled
|
|
|
|
# - mode_cleanup | bool # Cleanup mode must be enabled
|
2025-02-03 18:10:07 +01:00
|
|
|
|
2025-02-03 22:08:16 +01:00
|
|
|
- name: Cleanup {{nginx_www_wildcard_configuration}}
|
2025-02-03 18:10:07 +01:00
|
|
|
file:
|
2025-02-03 22:08:16 +01:00
|
|
|
path: "{{nginx_www_wildcard_configuration}}"
|
2025-02-03 18:10:07 +01:00
|
|
|
state: absent
|
|
|
|
notify: restart nginx
|
|
|
|
when:
|
|
|
|
- not enable_wildcard_certificate | bool
|
2025-02-03 22:08:16 +01:00
|
|
|
- mode_cleanup | bool
|