mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Added get_domain function
This commit is contained in:
60
filter_plugins/get_domain_filter.py
Normal file
60
filter_plugins/get_domain_filter.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from ansible.errors import AnsibleFilterError
|
||||
|
||||
class FilterModule(object):
|
||||
'''Ansible filter plugin to retrieve the correct domain for a given application_id.'''
|
||||
|
||||
def filters(self):
|
||||
return {
|
||||
'get_domain': self.get_domain,
|
||||
}
|
||||
|
||||
def get_domain(self, domains, application_id):
|
||||
"""
|
||||
Return the domain for application_id from the domains mapping:
|
||||
- If value is a string, return it.
|
||||
- If value is a dict, return its first value.
|
||||
- If value is a list, return its first element.
|
||||
- Otherwise, raise an error.
|
||||
"""
|
||||
# Ensure domains is a mapping
|
||||
if not isinstance(domains, dict):
|
||||
raise AnsibleFilterError(f"'domains' must be a dict, got {type(domains).__name__}")
|
||||
|
||||
if application_id not in domains:
|
||||
raise AnsibleFilterError(f"application_id '{application_id}' not found in domains mapping")
|
||||
|
||||
val = domains[application_id]
|
||||
|
||||
# String case
|
||||
if isinstance(val, str):
|
||||
if not val:
|
||||
raise AnsibleFilterError(f"domains['{application_id}'] is an empty string")
|
||||
return val
|
||||
|
||||
# Dict case
|
||||
if isinstance(val, dict):
|
||||
try:
|
||||
first_val = next(iter(val.values()))
|
||||
except StopIteration:
|
||||
raise AnsibleFilterError(f"domains['{application_id}'] dict is empty")
|
||||
if not isinstance(first_val, str) or not first_val:
|
||||
raise AnsibleFilterError(
|
||||
f"first value of domains['{application_id}'] must be a non-empty string, got {first_val!r}"
|
||||
)
|
||||
return first_val
|
||||
|
||||
# List case
|
||||
if isinstance(val, list):
|
||||
if not val:
|
||||
raise AnsibleFilterError(f"domains['{application_id}'] list is empty")
|
||||
first = val[0]
|
||||
if not isinstance(first, str) or not first:
|
||||
raise AnsibleFilterError(
|
||||
f"first element of domains['{application_id}'] must be a non-empty string, got {first!r}"
|
||||
)
|
||||
return first
|
||||
|
||||
# Other types
|
||||
raise AnsibleFilterError(
|
||||
f"domains['{application_id}'] has unsupported type {type(val).__name__}, must be str, dict or list"
|
||||
)
|
@@ -21,7 +21,7 @@ class FilterModule(object):
|
||||
{{ redirect_list
|
||||
| add_redirect_if_group('lam',
|
||||
'ldap.' ~ primary_domain,
|
||||
domains.lam,
|
||||
domains | get_domain('lam'),
|
||||
group_names) }}
|
||||
"""
|
||||
try:
|
||||
|
Reference in New Issue
Block a user