mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Fail safed more parts of the code
This commit is contained in:
40
filter_plugins/get_all_application_ids.py
Normal file
40
filter_plugins/get_all_application_ids.py
Normal file
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/env python3
|
||||
# filter_plugins/get_all_application_ids.py
|
||||
|
||||
import glob
|
||||
import os
|
||||
import yaml
|
||||
|
||||
|
||||
def get_all_application_ids(roles_dir='roles'):
|
||||
"""
|
||||
Ansible filter to retrieve all unique application_id values
|
||||
defined in roles/*/vars/main.yml files.
|
||||
|
||||
:param roles_dir: Base directory for Ansible roles (default: 'roles')
|
||||
:return: Sorted list of unique application_id strings
|
||||
"""
|
||||
pattern = os.path.join(roles_dir, '*', 'vars', 'main.yml')
|
||||
app_ids = []
|
||||
|
||||
for filepath in glob.glob(pattern):
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
data = yaml.safe_load(f)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if isinstance(data, dict) and 'application_id' in data:
|
||||
app_ids.append(data['application_id'])
|
||||
|
||||
return sorted(set(app_ids))
|
||||
|
||||
|
||||
class FilterModule(object):
|
||||
"""
|
||||
Ansible filter plugin for retrieving application IDs.
|
||||
"""
|
||||
def filters(self):
|
||||
return {
|
||||
'get_all_application_ids': get_all_application_ids
|
||||
}
|
51
filter_plugins/get_application_id.py
Normal file
51
filter_plugins/get_application_id.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# filter_plugins/get_application_id.py
|
||||
|
||||
import os
|
||||
import re
|
||||
import yaml
|
||||
from ansible.errors import AnsibleFilterError
|
||||
|
||||
|
||||
def get_application_id(role_name):
|
||||
"""
|
||||
Jinja2/Ansible filter: given a role name, load its vars/main.yml and return the application_id value.
|
||||
"""
|
||||
# Construct path: assumes current working directory is project root
|
||||
vars_file = os.path.join(os.getcwd(), 'roles', role_name, 'vars', 'main.yml')
|
||||
|
||||
if not os.path.isfile(vars_file):
|
||||
raise AnsibleFilterError(f"Vars file not found for role '{role_name}': {vars_file}")
|
||||
|
||||
try:
|
||||
# Read entire file content to avoid lazy stream issues
|
||||
with open(vars_file, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
data = yaml.safe_load(content)
|
||||
except Exception as e:
|
||||
raise AnsibleFilterError(f"Error reading YAML from {vars_file}: {e}")
|
||||
|
||||
# Ensure parsed data is a mapping
|
||||
if not isinstance(data, dict):
|
||||
raise AnsibleFilterError(
|
||||
f"Error reading YAML from {vars_file}: expected mapping, got {type(data).__name__}"
|
||||
)
|
||||
|
||||
# Detect malformed YAML: no valid identifier-like keys
|
||||
valid_key_pattern = re.compile(r'^[A-Za-z_][A-Za-z0-9_]*$')
|
||||
if data and not any(valid_key_pattern.match(k) for k in data.keys()):
|
||||
raise AnsibleFilterError(f"Error reading YAML from {vars_file}: invalid top-level keys")
|
||||
|
||||
if 'application_id' not in data:
|
||||
raise AnsibleFilterError(f"Key 'application_id' not found in {vars_file}")
|
||||
|
||||
return data['application_id']
|
||||
|
||||
|
||||
class FilterModule(object):
|
||||
"""
|
||||
Ansible filter plugin entry point.
|
||||
"""
|
||||
def filters(self):
|
||||
return {
|
||||
'get_application_id': get_application_id,
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
'''
|
||||
Ansible filter plugin: get_role_folder
|
||||
Ansible filter plugin: get_role
|
||||
|
||||
This filter inspects each role under the given roles directory, loads its vars/main.yml,
|
||||
and returns the role folder name whose application_id matches the provided value.
|
||||
@@ -10,7 +10,7 @@ import os
|
||||
import yaml
|
||||
|
||||
|
||||
def get_role_folder(application_id, roles_path='roles'):
|
||||
def get_role(application_id, roles_path='roles'):
|
||||
"""
|
||||
Find the role directory under `roles_path` whose vars/main.yml contains the given application_id.
|
||||
|
||||
@@ -40,9 +40,9 @@ def get_role_folder(application_id, roles_path='roles'):
|
||||
|
||||
class FilterModule(object):
|
||||
"""
|
||||
Register the get_role_folder filter
|
||||
Register the get_role filter
|
||||
"""
|
||||
def filters(self):
|
||||
return {
|
||||
'get_role_folder': get_role_folder,
|
||||
'get_role': get_role,
|
||||
}
|
Reference in New Issue
Block a user