diff --git a/example/ansible/inventory/groups.yml b/example/ansible/inventory/groups.yml index e22c529..e8a1a62 100644 --- a/example/ansible/inventory/groups.yml +++ b/example/ansible/inventory/groups.yml @@ -2,34 +2,6 @@ linux: vars: ansible_user: "root" -debian13: - vars: - ansible_python_interpreter: "/usr/bin/python3.13" - -ubuntu24: +ubuntu2404lts: vars: ansible_python_interpreter: "/usr/bin/python3.12" - -proxy: - hosts: - proxy1: - -waf: - hosts: - waf1: - waf2: - waf3: - -mysql: - hosts: - sql1: - -nfs: - hosts: - nfs1: - -www: - hosts: - www1: - www2: - www3: diff --git a/example/ansible/inventory/multipass.py b/example/ansible/inventory/multipass.py new file mode 100755 index 0000000..30082b3 --- /dev/null +++ b/example/ansible/inventory/multipass.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + +import sys +import subprocess +import json +import re + +out = subprocess.run(["multipass", "list", "--format=json"], capture_output=True) +if out.returncode != 0: + print(f"ERR executing multipass list: {out.returncode}") + sys.exit(out.returncode) + +instances = json.loads(out.stdout) +ret = { + "_meta": {"hostvars": {}}, + "linux": {"hosts": []} +} + +for instance in instances['list']: + if instance['state'] != "Running": + continue + + release = instance['release'].replace(' ', '').replace('.', '').lower() + if release not in ret: + ret[release] = {"hosts": []} + + group = re.search('^[a-z]+', instance['name']).group() + if group not in ret: + ret[group] = {"hosts": []} + + ret['_meta']['hostvars'][instance['name']] = {"ansible_host": instance['ipv4'][0] } + ret['linux']['hosts'].append(instance['name']) + ret[release]['hosts'].append(instance['name']) + ret[group]['hosts'].append(instance['name']) + +print(json.dumps(ret)) diff --git a/example/ansible/inventory/multipass.sh b/example/ansible/inventory/multipass.sh deleted file mode 100755 index 3580afd..0000000 --- a/example/ansible/inventory/multipass.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh - -multipass list --format=json |jq '{ - _meta: {hostvars: [.list[] | {(.name): {ansible_host: .ipv4[0]}}] | add}, - linux: {hosts: [.list[].name]}, - ubuntu24: {hosts: [.list[].name]}, -}'