Refactored docker role include

This commit is contained in:
Kevin Veen-Birkenbach 2025-05-15 18:35:21 +02:00
parent 2aac8b5f80
commit 2478e4013f
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E
4 changed files with 100 additions and 218 deletions

View File

@ -1,17 +1,26 @@
ROLES_DIR=./roles
OUTPUT=./group_vars/all/11_applications.yml
SCRIPT=./cli/generate_defaults_applications.py
ROLES_DIR := ./roles
APPLICATIONS_OUT := ./group_vars/all/11_applications.yml
APPLICATIONS_SCRIPT := ./cli/generate_defaults_applications.py
INCLUDES_OUT := ./tasks/include-docker-roles.yml
INCLUDES_SCRIPT := ./cli/generate_role_includes.py
.PHONY: build install test
build:
@echo "🔧 Generating $(OUTPUT) from roles in $(ROLES_DIR)..."
@mkdir -p $(dir $(OUTPUT))
python3 $(SCRIPT) --roles-dir $(ROLES_DIR) --output-file $(OUTPUT)
@echo "✅ Output written to $(OUTPUT)"
@echo "🔧 Generating applications defaults → $(APPLICATIONS_OUT) from roles in $(ROLES_DIR)"
@mkdir -p $(dir $(APPLICATIONS_OUT))
python3 $(APPLICATIONS_SCRIPT) --roles-dir $(ROLES_DIR) --output-file $(APPLICATIONS_OUT)
@echo "✅ Applications defaults written to $(APPLICATIONS_OUT)\n"
@echo "🔧 Generating Docker role includes → $(INCLUDES_OUT)"
@mkdir -p $(dir $(INCLUDES_OUT))
python3 $(INCLUDES_SCRIPT) $(ROLES_DIR) -o $(INCLUDES_OUT) -p docker-
@echo "✅ Docker role includes written to $(INCLUDES_OUT)"
install: build
@echo "⚙️ Install complete."
test:
@echo "Executing Unit Tests:"
@echo "\n🧪 Running Unit Tests..."
python -m unittest discover -s tests/unit
@echo "Executing Integration Tests:"
@echo "\n🔬 Running Integration Tests..."
python -m unittest discover -s tests/integration

View File

@ -0,0 +1,79 @@
import os
import argparse
import yaml
def find_roles(roles_dir, prefix=None):
"""
Yield absolute paths of role directories under roles_dir.
Only include roles whose directory name starts with prefix (if given) and contain vars/main.yml.
"""
for entry in os.listdir(roles_dir):
if prefix and not entry.startswith(prefix):
continue
path = os.path.join(roles_dir, entry)
vars_file = os.path.join(path, 'vars', 'main.yml')
if os.path.isdir(path) and os.path.isfile(vars_file):
yield path, vars_file
def load_application_id(vars_file):
"""
Load the vars/main.yml and return the value of application_id key.
Returns None if not found.
"""
with open(vars_file, 'r') as f:
data = yaml.safe_load(f) or {}
return data.get('application_id')
def generate_playbook_entries(roles_dir, prefix=None):
entries = []
for role_path, vars_file in find_roles(roles_dir, prefix):
app_id = load_application_id(vars_file)
if not app_id:
continue
# Derive role name from directory name
role_name = os.path.basename(role_path)
# entry text
entry = (
f"- name: setup {app_id}\n"
f" when: (\"{app_id}\" in group_names)\n"
f" include_role:\n"
f" name: {role_name}\n"
)
entries.append(entry)
return entries
def main():
parser = argparse.ArgumentParser(
description='Generate an Ansible playbook include file from Docker roles and application_ids.'
)
parser.add_argument(
'roles_dir',
help='Path to directory containing role folders'
)
parser.add_argument(
'-p', '--prefix',
help='Only include roles whose names start with this prefix (e.g. docker-, client-)',
default=None
)
parser.add_argument(
'-o', '--output',
help='Output file path (default: stdout)',
default=None
)
args = parser.parse_args()
entries = generate_playbook_entries(args.roles_dir, args.prefix)
output = ''.join(entries)
if args.output:
with open(args.output, 'w') as f:
f.write(output)
print(f"Playbook entries written to {args.output}")
else:
print(output)
if __name__ == '__main__':
main()

1
tasks/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
include-docker-roles.yml

View File

@ -11,215 +11,8 @@
- health-btrfs
- system-btrfs-auto-balancer
#########################################################################
### Docker Roles ###
#########################################################################
- name: "setup matomo"
when: ("matomo" in group_names)
include_role:
name: docker-matomo
- name: setup ldap
when: ("ldap" in group_names)
include_role:
name: docker-ldap
- name: setup keycloak
when: ("keycloak" in group_names)
include_role:
name: docker-keycloak
- name: setup lam
when: ("lam" in group_names)
include_role:
name: docker-lam
- name: setup phpldapadmin
when: ("phpldapadmin" in group_names)
include_role:
name: docker-phpldapadmin
- name: setup nextcloud hosts
when: ("nextcloud" in group_names)
include_role:
name: docker-nextcloud
- name: setup gitea hosts
when: ("gitea" in group_names)
include_role:
name: docker-gitea
vars:
run_mode: prod
- name: setup wordpress hosts
when: ("wordpress" in group_names)
include_role:
name: docker-wordpress
- name: setup mediawiki hosts
when: ("mediawiki" in group_names)
include_role:
name: docker-mediawiki
- name: setup mybb hosts
when: ("mybb" in group_names)
include_role:
name: docker-mybb
vars:
mybb_domains: "{{domains.mybb}}"
- name: setup yourls hosts
when: ("yourls" in group_names)
include_role:
name: docker-yourls
- name: setup mailu hosts
when: ("mailu" in group_names)
include_role:
name: docker-mailu
- name: setup elk hosts
when: ("elk" in group_names)
include_role:
name: docker-elk
- name: setup mastodon hosts
when: ("mastodon" in group_names)
include_role:
name: docker-mastodon
- name: setup pixelfed hosts
when: ("pixelfed" in group_names)
include_role:
name: docker-pixelfed
- name: setup peertube hosts
when: ("peertube" in group_names)
include_role:
name: docker-peertube
- name: setup bigbluebutton hosts
when: ("bigbluebutton" in group_names)
include_role:
name: docker-bigbluebutton
vars:
domain: "{{domains.bigbluebutton}}"
- name: setup funkwhale hosts
when: ("funkwhale" in group_names)
include_role:
name: docker-funkwhale
- name: setup roulette-wheel hosts
when: ("roulette-wheel" in group_names)
include_role:
name: docker-roulette-wheel
- name: setup joomla hosts
when: ("joomla" in group_names)
include_role:
name: docker-joomla
- name: setup attendize
when: ("attendize" in group_names)
include_role:
name: docker-attendize
- name: setup baserow hosts
when: ("baserow" in group_names)
include_role:
name: docker-baserow
- name: setup listmonk
when: ("listmonk" in group_names)
include_role:
name: docker-listmonk
- name: setup discourse
when: ("discourse" in group_names)
include_role:
name: docker-discourse
- name: setup matrix with flavor 'ansible'
include_role:
name: docker-matrix-ansible
when: applications.matrix.role == 'ansible' and ("matrix" in group_names)
- name: setup matrix with flavor 'compose'
include_role:
name: docker-matrix
when: applications.matrix.role == 'compose' and ("matrix" in group_names)
- name: setup open project instances
when: ("openproject" in group_names)
include_role:
name: docker-openproject
- name: setup gitlab hosts
when: ("gitlab" in group_names)
include_role:
name: docker-gitlab
- name: setup akaunting hosts
when: ("akaunting" in group_names)
include_role:
name: docker-akaunting
- name: setup moodle instance
when: ("moodle" in group_names)
include_role:
name: docker-moodle
- name: setup taiga instance
when: ("taiga" in group_names)
include_role:
name: docker-taiga
- name: setup friendica hosts
when: ("friendica" in group_names)
include_role:
name: docker-friendica
- name: setup portfolio
when: ("portfolio" in group_names)
include_role:
name: docker-portfolio
- name: setup bluesky
when: ("bluesky" in group_names)
include_role:
name: docker-bluesky
- name: setup PHPMyAdmin
when: ("phpmyadmin" in group_names)
include_role:
name: docker-phpmyadmin
- name: setup SNIPE-IT
when: ("snipe_it" in group_names)
include_role:
name: docker-snipe_it
- name: setup sphinx
when: ("sphinx" in group_names)
include_role:
name: docker-sphinx
- name: setup pgadmin
when: ("pgadmin" in group_names)
include_role:
name: docker-pgadmin
- name: setup presentation
when: ("presentation" in group_names)
include_role:
name: docker-presentation
- name: setup espocrm hosts
when: ("espocrm" in group_names)
include_role:
name: docker-espocrm
- name: "Integrate Docker Role includes"
include_tasks: "include-docker-roles.yml"
# Native Webserver Roles
- name: setup nginx-serve-htmls