Restructured users

This commit is contained in:
Kevin Veen-Birkenbach 2025-07-09 02:26:50 +02:00
parent 22b4342300
commit ed0cd9b8c0
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E
19 changed files with 27 additions and 13 deletions

View File

@ -21,7 +21,7 @@ def load_run_after(meta_file):
def load_application_id(role_path): def load_application_id(role_path):
"""Load the application_id from the vars/main.yml of the role.""" """Load the application_id from the vars/main.yml of the role."""
vars_file = os.path.join(role_path, 'main', 'main.yml') vars_file = os.path.join(role_path, 'vars', 'main.yml')
if os.path.exists(vars_file): if os.path.exists(vars_file):
with open(vars_file, 'r') as f: with open(vars_file, 'r') as f:
data = yaml.safe_load(f) or {} data = yaml.safe_load(f) or {}
@ -113,14 +113,21 @@ def generate_playbook_entries(roles_dir, prefix=None):
entries = [] entries = []
for role_name in sorted_role_names: for role_name in sorted_role_names:
role = roles[role_name] role = roles[role_name]
# --- new validation block ---
if role.get('application_id') is None:
raise ValueError(f"Role '{role_name}' is missing an application_id")
# ----------------------------
app_id = role['application_id']
entries.append( entries.append(
f"- name: setup {role['application_id']}\n" f"- name: setup {app_id}\n"
f" when: ('{role['application_id']}' | application_allowed(group_names, allowed_applications))\n" f" when: ('{app_id}' | application_allowed(group_names, allowed_applications))\n"
f" include_role:\n" f" include_role:\n"
f" name: {role['role_name']}\n" f" name: {role['role_name']}\n"
) )
entries.append( entries.append(
f"- name: flush handlers after {role['application_id']}\n" f"- name: flush handlers after {app_id}\n"
f" meta: flush_handlers\n" f" meta: flush_handlers\n"
) )

View File

@ -110,7 +110,7 @@ def build_users(defs, primary_domain, start_id, become_pwd):
def load_user_defs(roles_directory): def load_user_defs(roles_directory):
""" """
Scan all roles/*/meta/users.yml files and merge any 'users:' sections. Scan all roles/*/users/main.yml files and merge any 'users:' sections.
Args: Args:
roles_directory (str): Path to the directory containing role subdirectories. roles_directory (str): Path to the directory containing role subdirectories.
@ -121,7 +121,7 @@ def load_user_defs(roles_directory):
Raises: Raises:
ValueError: On invalid format or conflicting override values. ValueError: On invalid format or conflicting override values.
""" """
pattern = os.path.join(roles_directory, '*/meta/users.yml') pattern = os.path.join(roles_directory, '*/users/main.yml')
files = sorted(glob.glob(pattern)) files = sorted(glob.glob(pattern))
merged = OrderedDict() merged = OrderedDict()
@ -165,11 +165,11 @@ def dictify(data):
def parse_args(): def parse_args():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description='Generate a users.yml by merging all roles/*/meta/users.yml definitions.' description='Generate a users.yml by merging all roles/*/users/main.yml definitions.'
) )
parser.add_argument( parser.add_argument(
'--roles-dir', '-r', required=True, '--roles-dir', '-r', required=True,
help='Directory containing roles (e.g., roles/*/meta/users.yml).' help='Directory containing roles (e.g., roles/*/users/main.yml).'
) )
parser.add_argument( parser.add_argument(
'--output', '-o', required=True, '--output', '-o', required=True,

View File

@ -0,0 +1,7 @@
# Add here the users which your application needs e.g:
users:
demo:
username: demo
email: "demo@{{ primary_domain }}"
roles: []
description: Demo User

View File

@ -114,18 +114,18 @@ class TestGenerateUsers(unittest.TestCase):
# create temp roles structure # create temp roles structure
tmp = tempfile.mkdtemp() tmp = tempfile.mkdtemp()
try: try:
os.makedirs(os.path.join(tmp, 'role1/meta')) os.makedirs(os.path.join(tmp, 'role1/users'))
os.makedirs(os.path.join(tmp, 'role2/meta')) os.makedirs(os.path.join(tmp, 'role2/users'))
# role1 defines user x # role1 defines user x
with open(os.path.join(tmp, 'role1/meta/users.yml'), 'w') as f: with open(os.path.join(tmp, 'role1/users/main.yml'), 'w') as f:
yaml.safe_dump({'users': {'x': {'email': 'x@a'}}}, f) yaml.safe_dump({'users': {'x': {'email': 'x@a'}}}, f)
# role2 defines same user x with same value # role2 defines same user x with same value
with open(os.path.join(tmp, 'role2/meta/users.yml'), 'w') as f: with open(os.path.join(tmp, 'role2/users/main.yml'), 'w') as f:
yaml.safe_dump({'users': {'x': {'email': 'x@a'}}}, f) yaml.safe_dump({'users': {'x': {'email': 'x@a'}}}, f)
defs = generate_users.load_user_defs(tmp) defs = generate_users.load_user_defs(tmp)
self.assertIn('x', defs) self.assertIn('x', defs)
# now conflict definition # now conflict definition
with open(os.path.join(tmp, 'role2/meta/users.yml'), 'w') as f: with open(os.path.join(tmp, 'role2/users/main.yml'), 'w') as f:
yaml.safe_dump({'users': {'x': {'email': 'x@b'}}}, f) yaml.safe_dump({'users': {'x': {'email': 'x@b'}}}, f)
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
generate_users.load_user_defs(tmp) generate_users.load_user_defs(tmp)