Nginx variable refactoring and general bug solving. Got interupted commit not tested

This commit is contained in:
2025-01-31 13:14:07 +01:00
parent b96d95f3e4
commit 11752f5e2e
23 changed files with 59 additions and 48 deletions

View File

@@ -1,13 +1,13 @@
# README.md for nginx-www-redirect Role
## Overview
The `nginx-www-redirect` role is designed to automate the process of setting up redirects from `www.domain.tld` to `domain.tld` for all domains and subdomains configured within the `{{nginx_servers_directory}}` directory. This role dynamically identifies configuration files following the pattern `*domain.tld.conf` and creates corresponding redirection rules.
The `nginx-www-redirect` role is designed to automate the process of setting up redirects from `www.domain.tld` to `domain.tld` for all domains and subdomains configured within the `{{nginx.directories.http.servers}}` directory. This role dynamically identifies configuration files following the pattern `*domain.tld.conf` and creates corresponding redirection rules.
## Role Description
This role performs several key tasks:
1. **Find Configuration Files**: Locates all `.conf` files in the `{{nginx_servers_directory}}` directory that match the `*.*.conf` pattern, ensuring that only domain and subdomain configurations are selected.
1. **Find Configuration Files**: Locates all `.conf` files in the `{{nginx.directories.http.servers}}` directory that match the `*.*.conf` pattern, ensuring that only domain and subdomain configurations are selected.
2. **Filter Domain Names**: Processes each configuration file, extracting the domain names and removing both the `.conf` extension and the `{{nginx_servers_directory}}` path.
2. **Filter Domain Names**: Processes each configuration file, extracting the domain names and removing both the `.conf` extension and the `{{nginx.directories.http.servers}}` path.
3. **Prepare Redirect Domain Mappings**: Transforms the filtered domain names into a source-target mapping format, where `source` is `www.domain.tld` and `target` is `domain.tld`.
@@ -25,7 +25,7 @@ Example playbook:
## Requirements
- Ansible environment set up and configured to run roles.
- Access to the `{{nginx_servers_directory}}` directory on the target hosts.
- Access to the `{{nginx.directories.http.servers}}` directory on the target hosts.
- The `nginx-domain-redirect` role must be present and properly configured to handle the redirection mappings.
## Notes

View File

@@ -1,7 +1,7 @@
---
- name: Find all .conf
ansible.builtin.find:
paths: "{{nginx_servers_directory}}"
paths: "{{nginx.directories.http.servers}}"
patterns: '*.*.conf'
register: conf_files
@@ -9,8 +9,8 @@
set_fact:
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 }}"
vars:
domain_regex: "^{{ nginx_servers_directory }}(?!www\\.)[^/]+\\.conf$"
path_regex: "^{{ nginx_servers_directory }}"
domain_regex: "^{{nginx.directories.http.servers}}(?!www\\.)[^/]+\\.conf$"
path_regex: "^{{nginx.directories.http.servers}}"
- name: The domains for which a www. redirect will be implemented
debug: