mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-05-16 01:37:19 +02:00
Optimized defaults_redirect_domain_mappings for dynamic creating by roles
This commit is contained in:
parent
d0844ce44f
commit
026855b197
@ -1,2 +0,0 @@
|
||||
# Todos
|
||||
- Refactor all 4 functions to one
|
37
filter_plugins/redirect_filters.py
Normal file
37
filter_plugins/redirect_filters.py
Normal file
@ -0,0 +1,37 @@
|
||||
# roles/<your-role>/filter_plugins/redirect_filters.py
|
||||
from ansible.errors import AnsibleFilterError
|
||||
|
||||
class FilterModule(object):
|
||||
"""
|
||||
Custom filters for redirect domain mappings
|
||||
"""
|
||||
|
||||
def filters(self):
|
||||
return {
|
||||
"add_redirect_if_group": self.add_redirect_if_group,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def add_redirect_if_group(redirect_list, group, source, target, group_names):
|
||||
"""
|
||||
Append {"source": source, "target": target} to *redirect_list*
|
||||
**only** if *group* is contained in *group_names*.
|
||||
|
||||
Usage in Jinja:
|
||||
{{ redirect_list
|
||||
| add_redirect_if_group('lam',
|
||||
'ldap.' ~ primary_domain,
|
||||
domains.lam,
|
||||
group_names) }}
|
||||
"""
|
||||
try:
|
||||
# Make a copy so we don’t mutate the original list in place
|
||||
redirects = list(redirect_list)
|
||||
|
||||
if group in group_names:
|
||||
redirects.append({"source": source, "target": target})
|
||||
|
||||
return redirects
|
||||
|
||||
except Exception as exc:
|
||||
raise AnsibleFilterError(f"add_redirect_if_group failed: {exc}")
|
@ -53,31 +53,31 @@ defaults_domains:
|
||||
- "blog.{{primary_domain}}"
|
||||
|
||||
## Domain Redirects
|
||||
defaults_redirect_domain_mappings:
|
||||
- { source: "akaunting.{{primary_domain}}", target: "{{domains.akaunting}}" }
|
||||
- { source: "bbb.{{primary_domain}}", target: "{{domains.bigbluebutton}}" }
|
||||
- { source: "discourse.{{primary_domain}}", target: "{{domains.discourse}}" }
|
||||
- { source: "crm.{{primary_domain}}", target: "{{domains.espocrm}}" }
|
||||
- { source: "funkwhale.{{primary_domain}}", target: "{{domains.funkwhale}}" }
|
||||
- { source: "gitea.{{primary_domain}}", target: "{{domains.gitea}}" }
|
||||
- { source: "keycloak.{{primary_domain}}", target: "{{domains.keycloak}}" }
|
||||
- {
|
||||
source: "{{ domains.ldap }}",
|
||||
target: "{% if 'lam' in group_names %}{{ domains.lam }}{% elif 'phpmyldapadmin' in group_names %}{{ domains.phpmyldap }}{% else %}{{ primary_domain }}{% endif %}"
|
||||
}
|
||||
- { source: "listmonk.{{primary_domain}}", target: "{{domains.listmonk}}" }
|
||||
- { source: "mailu.{{primary_domain}}", target: "{{domains.mailu}}" }
|
||||
- { source: "moodle.{{primary_domain}}", target: "{{domains.moodle}}" }
|
||||
- { source: "nextcloud.{{primary_domain}}", target: "{{domains.nextcloud}}" }
|
||||
- { source: "openproject.{{primary_domain}}", target: "{{domains.openproject}}" }
|
||||
- { source: "peertube.{{primary_domain}}", target: "{{domains.peertube}}" }
|
||||
- { source: "pictures.{{primary_domain}}", target: "{{domains.pixelfed}}" }
|
||||
- { source: "pixelfed.{{primary_domain}}", target: "{{domains.pixelfed}}" }
|
||||
- { source: "short.{{primary_domain}}", target: "{{domains.yourls}}" }
|
||||
- { source: "snipe-it.{{primary_domain}}", target: "{{domains.snipe_it}}" }
|
||||
- { source: "taiga.{{primary_domain}}", target: "{{domains.taiga}}" }
|
||||
- { source: "videos.{{primary_domain}}", target: "{{domains.peertube}}" }
|
||||
- { source: "wordpress.{{primary_domain}}", target: "{{domains.wordpress[0]}}" }
|
||||
defaults_redirect_domain_mappings: >-
|
||||
{{ []
|
||||
| add_redirect_if_group('akaunting', "akaunting." ~ primary_domain, domains.akaunting, group_names)
|
||||
| add_redirect_if_group('bigbluebutton', "bbb." ~ primary_domain, domains.bigbluebutton, group_names)
|
||||
| add_redirect_if_group('discourse', "discourse." ~ primary_domain, domains.discourse, group_names)
|
||||
| add_redirect_if_group('espocrm', "crm." ~ primary_domain, domains.espocrm, group_names)
|
||||
| add_redirect_if_group('funkwhale', "funkwhale." ~ primary_domain, domains.funkwhale, group_names)
|
||||
| add_redirect_if_group('gitea', "gitea." ~ primary_domain, domains.gitea, group_names)
|
||||
| add_redirect_if_group('keycloak', "keycloak." ~ primary_domain, domains.keycloak, group_names)
|
||||
| add_redirect_if_group('lam', domains.ldap, domains.lam, group_names)
|
||||
| add_redirect_if_group('phpmyldapadmin', domains.ldap, domains.phpmyldap, group_names)
|
||||
| add_redirect_if_group('listmonk', "listmonk." ~ primary_domain, domains.listmonk, group_names)
|
||||
| add_redirect_if_group('mailu', "mailu." ~ primary_domain, domains.mailu, group_names)
|
||||
| add_redirect_if_group('moodle', "moodle." ~ primary_domain, domains.moodle, group_names)
|
||||
| add_redirect_if_group('nextcloud', "nextcloud." ~ primary_domain, domains.nextcloud, group_names)
|
||||
| add_redirect_if_group('openproject', "openproject." ~ primary_domain, domains.openproject, group_names)
|
||||
| add_redirect_if_group('peertube', "peertube." ~ primary_domain, domains.peertube, group_names)
|
||||
| add_redirect_if_group('pixelfed', "pictures." ~ primary_domain, domains.pixelfed, group_names)
|
||||
| add_redirect_if_group('pixelfed', "pixelfed." ~ primary_domain, domains.pixelfed, group_names)
|
||||
| add_redirect_if_group('yourls', "short." ~ primary_domain, domains.yourls, group_names)
|
||||
| add_redirect_if_group('snipe-it', "snipe-it." ~ primary_domain, domains.snipe_it, group_names)
|
||||
| add_redirect_if_group('taiga', "taiga." ~ primary_domain, domains.taiga, group_names)
|
||||
| add_redirect_if_group('peertube', "videos." ~ primary_domain, domains.peertube, group_names)
|
||||
| add_redirect_if_group('wordpress', "wordpress." ~ primary_domain, domains.wordpress[0], group_names)
|
||||
}}
|
||||
|
||||
# Domains which are deprecated and should be cleaned up
|
||||
deprecated_domains: []
|
53
tests/unit/test_redirect_filters.py
Normal file
53
tests/unit/test_redirect_filters.py
Normal file
@ -0,0 +1,53 @@
|
||||
import os
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../.."))
|
||||
sys.path.insert(0, PROJECT_ROOT)
|
||||
|
||||
from filter_plugins.redirect_filters import FilterModule
|
||||
|
||||
|
||||
class TestAddRedirectIfGroup(unittest.TestCase):
|
||||
"""Unit-tests for the add_redirect_if_group filter."""
|
||||
|
||||
def setUp(self):
|
||||
# Obtain the callable once for reuse
|
||||
self.add_redirect = FilterModule().filters()["add_redirect_if_group"]
|
||||
|
||||
def test_appends_redirect_when_group_present(self):
|
||||
original = [{"source": "a", "target": "b"}]
|
||||
result = self.add_redirect(
|
||||
original,
|
||||
group="lam",
|
||||
source="ldap.example.com",
|
||||
target="lam.example.com",
|
||||
group_names=["lam", "other"],
|
||||
)
|
||||
|
||||
# Original list must stay unchanged
|
||||
self.assertEqual(len(original), 1)
|
||||
# Result list must contain the extra entry
|
||||
self.assertEqual(len(result), 2)
|
||||
self.assertIn(
|
||||
{"source": "ldap.example.com", "target": "lam.example.com"}, result
|
||||
)
|
||||
|
||||
def test_keeps_list_unchanged_when_group_absent(self):
|
||||
original = [{"source": "a", "target": "b"}]
|
||||
result = self.add_redirect(
|
||||
original,
|
||||
group="lam",
|
||||
source="ldap.example.com",
|
||||
target="lam.example.com",
|
||||
group_names=["unrelated"],
|
||||
)
|
||||
|
||||
# No new entries
|
||||
self.assertEqual(result, original)
|
||||
# But ensure a new list object was returned (no in-place mutation)
|
||||
self.assertIsNot(result, original)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user