mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 23:08:06 +02:00
Adapted roles to new architecture
This commit is contained in:
113
cli/build/inventory/full.py
Normal file
113
cli/build/inventory/full.py
Normal file
@@ -0,0 +1,113 @@
|
||||
#!/usr/bin/env python3
|
||||
# cli/build/inventory/full.py
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import os
|
||||
|
||||
try:
|
||||
from filter_plugins.get_all_invokable_apps import get_all_invokable_apps
|
||||
except ImportError:
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')))
|
||||
from filter_plugins.get_all_invokable_apps import get_all_invokable_apps
|
||||
|
||||
import yaml
|
||||
import json
|
||||
|
||||
def build_group_inventory(apps, host):
|
||||
"""
|
||||
Builds a group-based Ansible inventory: each app is a group containing the host.
|
||||
"""
|
||||
groups = {app: {"hosts": [host]} for app in apps}
|
||||
inventory = {
|
||||
"all": {
|
||||
"hosts": [host],
|
||||
"children": {app: {} for app in apps},
|
||||
},
|
||||
**groups
|
||||
}
|
||||
return inventory
|
||||
|
||||
def build_hostvar_inventory(apps, host):
|
||||
"""
|
||||
Alternative: Builds an inventory where all invokables are set as hostvars (as a list).
|
||||
"""
|
||||
return {
|
||||
"all": {
|
||||
"hosts": [host],
|
||||
},
|
||||
"_meta": {
|
||||
"hostvars": {
|
||||
host: {
|
||||
"invokable_applications": apps
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description='Build a dynamic Ansible inventory for a given host with all invokable applications.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--host',
|
||||
required=True,
|
||||
help='Hostname to assign to all invokable application groups'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-f', '--format',
|
||||
choices=['json', 'yaml'],
|
||||
default='yaml',
|
||||
help='Output format (yaml [default], json)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'--inventory-style',
|
||||
choices=['group', 'hostvars'],
|
||||
default='group',
|
||||
help='Inventory style: group (default, one group per app) or hostvars (list as hostvar)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-c', '--categories-file',
|
||||
default=os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'roles', 'categories.yml')),
|
||||
help='Path to roles/categories.yml (default: roles/categories.yml at project root)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-r', '--roles-dir',
|
||||
default=os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'roles')),
|
||||
help='Path to roles/ directory (default: roles/ at project root)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-o', '--output',
|
||||
help='Write output to file instead of stdout'
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
apps = get_all_invokable_apps(
|
||||
categories_file=args.categories_file,
|
||||
roles_dir=args.roles_dir
|
||||
)
|
||||
except Exception as e:
|
||||
sys.stderr.write(f"Error: {e}\n")
|
||||
sys.exit(1)
|
||||
|
||||
# Select inventory style
|
||||
if args.inventory_style == 'group':
|
||||
inventory = build_group_inventory(apps, args.host)
|
||||
else:
|
||||
inventory = build_hostvar_inventory(apps, args.host)
|
||||
|
||||
# Output in chosen format
|
||||
if args.format == 'json':
|
||||
output = json.dumps(inventory, indent=2)
|
||||
else:
|
||||
output = yaml.safe_dump(inventory, default_flow_style=False)
|
||||
|
||||
if args.output:
|
||||
with open(args.output, 'w') as f:
|
||||
f.write(output)
|
||||
else:
|
||||
print(output)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
49
cli/meta/applications/invokable.py
Normal file
49
cli/meta/applications/invokable.py
Normal file
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3
|
||||
# cli/meta/applications/invokable.py
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Import filter plugin for get_all_invokable_apps
|
||||
try:
|
||||
from filter_plugins.get_all_invokable_apps import get_all_invokable_apps
|
||||
except ImportError:
|
||||
# Try to adjust sys.path if running outside Ansible
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')))
|
||||
try:
|
||||
from filter_plugins.get_all_invokable_apps import get_all_invokable_apps
|
||||
except ImportError:
|
||||
sys.stderr.write("Could not import filter_plugins.get_all_invokable_apps. Check your PYTHONPATH.\n")
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description='List all invokable applications (application_ids) based on invokable paths from categories.yml and available roles.'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-c', '--categories-file',
|
||||
default=os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'roles', 'categories.yml')),
|
||||
help='Path to roles/categories.yml (default: roles/categories.yml at project root)'
|
||||
)
|
||||
parser.add_argument(
|
||||
'-r', '--roles-dir',
|
||||
default=os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..', 'roles')),
|
||||
help='Path to roles/ directory (default: roles/ at project root)'
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
result = get_all_invokable_apps(
|
||||
categories_file=args.categories_file,
|
||||
roles_dir=args.roles_dir
|
||||
)
|
||||
except Exception as e:
|
||||
sys.stderr.write(f"Error: {e}\n")
|
||||
sys.exit(1)
|
||||
|
||||
for app_id in result:
|
||||
print(app_id)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Reference in New Issue
Block a user