mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2024-12-25 19:45:03 +01:00
Added role for automatic redirect from www.*domain.tld to *domain.tld
This commit is contained in:
parent
108c9f7c0f
commit
75c27f9a42
@ -32,6 +32,7 @@ path_docker_volumes: "{{path_administrator_home}}volume
|
||||
path_docker_compose_instances: "{{path_administrator_home}}docker-compose/"
|
||||
|
||||
# Runtime Variables
|
||||
activate_all_timers (bool): false # Activates all timers, independend if the handlers had been triggered
|
||||
nginx_matomo_tracking_active: false # Activates matomo tracking on all html pages
|
||||
execute_updates: true # Executes updates
|
||||
activate_all_timers: false # Activates all timers, independend if the handlers had been triggered
|
||||
nginx_matomo_tracking: false # Activates matomo tracking on all html pages
|
||||
nginx_www_redirect: true # Implements an redirect from all www. domains to the main domain
|
||||
execute_updates: true # Executes updates
|
@ -6,7 +6,7 @@
|
||||
tasks:
|
||||
roles:
|
||||
- role: update
|
||||
when: execute_updates is true
|
||||
when: execute_updates | default(false) | bool
|
||||
|
||||
- name: setup standard wireguard
|
||||
hosts: wireguard_server
|
||||
|
@ -12,22 +12,6 @@
|
||||
- cleanup-disc-space
|
||||
- health-btrfs
|
||||
|
||||
# Native Webserver Roles
|
||||
- name: setup nginx-homepages
|
||||
hosts: homepage
|
||||
become: true
|
||||
roles:
|
||||
- role: nginx-homepage
|
||||
vars:
|
||||
domain: "{{top_domain}}"
|
||||
- name: setup redirect hosts
|
||||
hosts: redirect
|
||||
become: true
|
||||
roles:
|
||||
- role: nginx-domain-redirect
|
||||
vars:
|
||||
domain_mappings: "{{redirect_domain_mappings}}"
|
||||
|
||||
# Docker Roles
|
||||
- name: setup nextcloud hosts
|
||||
hosts: nextcloud_server
|
||||
@ -219,4 +203,28 @@
|
||||
- role: docker-akaunting
|
||||
vars:
|
||||
domain: akaunting.{{top_domain}}
|
||||
http_port: 8080
|
||||
http_port: 8080
|
||||
|
||||
# Native Webserver Roles
|
||||
- name: setup nginx-homepages
|
||||
hosts: homepage
|
||||
become: true
|
||||
roles:
|
||||
- role: nginx-homepage
|
||||
vars:
|
||||
domain: "{{top_domain}}"
|
||||
|
||||
- name: setup redirect hosts
|
||||
hosts: redirect
|
||||
become: true
|
||||
roles:
|
||||
- role: nginx-domain-redirect
|
||||
vars:
|
||||
domain_mappings: "{{redirect_domain_mappings}}"
|
||||
|
||||
- name: setup www redirect
|
||||
hosts: all
|
||||
become: true
|
||||
roles:
|
||||
- role: nginx-www-redirect
|
||||
when: nginx_www_redirect | bool
|
||||
|
@ -8,7 +8,7 @@ server {
|
||||
|
||||
{% include 'roles/letsencrypt/templates/ssl_header.j2' %}
|
||||
|
||||
{% if nginx_matomo_tracking_active | default(False) %}
|
||||
{% if nginx_matomo_tracking | default(False) %}
|
||||
{% include 'roles/nginx-matomo-tracking/templates/matomo-tracking.conf.j2' %}
|
||||
{% endif %}
|
||||
|
||||
|
@ -4,7 +4,7 @@ server
|
||||
|
||||
{% include 'roles/letsencrypt/templates/ssl_header.j2' %}
|
||||
|
||||
{% if nginx_matomo_tracking_active | default(False) %}
|
||||
{% if nginx_matomo_tracking | default(False) %}
|
||||
{% include 'roles/nginx-matomo-tracking/templates/matomo-tracking.conf.j2' %}
|
||||
{% endif %}
|
||||
|
||||
|
@ -8,7 +8,7 @@ server {
|
||||
|
||||
{% include 'roles/letsencrypt/templates/ssl_header.j2' %}
|
||||
|
||||
{% if nginx_matomo_tracking_active | default(False) %}
|
||||
{% if nginx_matomo_tracking | default(False) %}
|
||||
{% include 'roles/nginx-matomo-tracking/templates/matomo-tracking.conf.j2' %}
|
||||
{% endif %}
|
||||
|
||||
|
@ -3,7 +3,7 @@ server
|
||||
server_name {{domain}};
|
||||
|
||||
# Include Matomo Tracking Code
|
||||
{% if nginx_matomo_tracking_active | default(False) %}
|
||||
{% if nginx_matomo_tracking | default(False) %}
|
||||
{% include 'roles/nginx-matomo-tracking/templates/matomo-tracking.conf.j2' %}
|
||||
{% endif %}
|
||||
|
||||
|
@ -5,7 +5,7 @@ server
|
||||
|
||||
{% include 'roles/letsencrypt/templates/ssl_header.j2' %}
|
||||
|
||||
{% if nginx_matomo_tracking_active | default(False) %}
|
||||
{% if nginx_matomo_tracking | default(False) %}
|
||||
{% include 'roles/nginx-matomo-tracking/templates/matomo-tracking.conf.j2' %}
|
||||
{% endif %}
|
||||
|
||||
|
37
roles/nginx-www-redirect/README.md
Normal file
37
roles/nginx-www-redirect/README.md
Normal file
@ -0,0 +1,37 @@
|
||||
# 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 `/etc/nginx/conf.d/` 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 `/etc/nginx/conf.d/` 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 `/etc/nginx/conf.d/` 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`.
|
||||
|
||||
4. **Include nginx-domain-redirect Role**: Applies the redirection configuration using the `nginx-domain-redirect` role with the dynamically generated domain mappings.
|
||||
|
||||
## Usage
|
||||
To use this role, include it in your playbook and ensure that the `nginx-domain-redirect` role is available in your Ansible environment. No additional configuration is required as the role is designed to dynamically identify and process the domain configurations.
|
||||
|
||||
Example playbook:
|
||||
```yaml
|
||||
- hosts: web-servers
|
||||
roles:
|
||||
- nginx-www-redirect
|
||||
```
|
||||
|
||||
## Requirements
|
||||
- Ansible environment set up and configured to run roles.
|
||||
- Access to the `/etc/nginx/conf.d/` directory on the target hosts.
|
||||
- The `nginx-domain-redirect` role must be present and properly configured to handle the redirection mappings.
|
||||
|
||||
## Notes
|
||||
- This role is designed to work in environments where domain and subdomain configurations follow the naming pattern `*domain.tld.conf`.
|
||||
- It automatically excludes any configurations that begin with `www.`, preventing duplicate redirects.
|
||||
|
||||
---
|
||||
|
||||
This `nginx-www-redirect` role was crafted with insights and guidance provided by ChatGPT, an advanced AI language model from OpenAI. The development process, including the discussions with ChatGPT that shaped this role, can be [here](https://chat.openai.com/share/a68e3574-f543-467d-aea7-0895f0e00bbb) explored in detail.
|
31
roles/nginx-www-redirect/tasks/main.yml
Normal file
31
roles/nginx-www-redirect/tasks/main.yml
Normal file
@ -0,0 +1,31 @@
|
||||
---
|
||||
- name: Find all .conf
|
||||
ansible.builtin.find:
|
||||
paths: "/etc/nginx/conf.d/"
|
||||
patterns: '*.*.conf'
|
||||
register: conf_files
|
||||
|
||||
- name: Print conf_files domains
|
||||
debug:
|
||||
var: conf_files
|
||||
|
||||
- name: Filter domain names and remove .conf extension and path
|
||||
set_fact:
|
||||
filtered_domains: "{{ conf_files.files | map(attribute='path') | map('regex_search', domain_regex) | select('string') | map('regex_replace', '^/etc/nginx/conf.d/', '') | map('regex_replace', '.conf$', '') | list }}"
|
||||
vars:
|
||||
domain_regex: '^/etc/nginx/conf.d/(?!www\.)[^/]+\.conf$'
|
||||
|
||||
|
||||
- name: Print filtered domains
|
||||
debug:
|
||||
var: filtered_domains
|
||||
|
||||
- name: Prepare redirect domain mappings
|
||||
set_fact:
|
||||
redirect_domain_mappings: "{{ filtered_domains | map('regex_replace', '^(.*)$', '{ source: \"www.\\1\", target: \"\\1\" }') | map('from_yaml') | list }}"
|
||||
|
||||
- name: Include nginx-domain-redirect role with dynamic domain mappings
|
||||
include_role:
|
||||
name: nginx-domain-redirect
|
||||
vars:
|
||||
domain_mappings: "{{ redirect_domain_mappings }}"
|
@ -1,4 +1,4 @@
|
||||
- name: Activate NGINX matomo tracking
|
||||
include_role:
|
||||
name: nginx-matomo-tracking
|
||||
when: nginx_matomo_tracking_active and domain is defined
|
||||
when: nginx_matomo_tracking and domain is defined
|
Loading…
Reference in New Issue
Block a user