commit e8761ab285630c6bd8e6e3d1148f5e805e67e54d Author: Sven Velt Date: Fri Sep 20 09:48:55 2024 +0200 Inital commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1c22cce --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +files/plugins_custom/ + +.*.sw? +*~ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..835ba7b --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +Inventory scripts for Ansible: LXC +================================== + +Dependencies +------------ +- `lxc` Python module + +Usage +----- + % ansible-inventory --graph -i inventory_lxc_ip.py + % ansible-inventory --graph -i inventory_lxc.py + +Connection +---------- + - `inventory_lxc.py` uses `ansible_connection=lxc` + - `inventory_lxc_ip.py` uses first IPv4 reported by LXC + +Author Information +------------------ + +Sven Velt - +https://git.velt.biz/velt.biz/ + diff --git a/inventory_lxc.py b/inventory_lxc.py new file mode 100755 index 0000000..26f32d3 --- /dev/null +++ b/inventory_lxc.py @@ -0,0 +1,33 @@ +#!/usr/bin/python3 + +import json +import sys + +try: + import lxc +except ImportError: + print("Could not import lxc module!") + sys.exit(1) + +######################################## +inventory ={ '_meta':{'hostvars':{} } } +######################################## + +ctnames = lxc.list_containers() + +for ctname in ctnames: + ct = lxc.Container(ctname) + + if ct.running: + for group in ct.get_running_config_item('lxc.group').rstrip().split('\n'): + if not group in inventory: + inventory[group] = { + 'hosts': [], + 'vars': { + 'ansible_connection': 'lxc', + } + } + inventory[group]['hosts'].append(ctname) + +print(json.dumps(inventory)) + diff --git a/inventory_lxc_ip.py b/inventory_lxc_ip.py new file mode 100755 index 0000000..06537ee --- /dev/null +++ b/inventory_lxc_ip.py @@ -0,0 +1,37 @@ +#!/usr/bin/python3 + +import json +import sys + +try: + import lxc +except ImportError: + print("Could not import lxc module!") + sys.exit(1) + +######################################## +inventory ={ '_meta':{'hostvars':{} } } +######################################## + +ctnames = lxc.list_containers() + +for ctname in ctnames: + ct = lxc.Container(ctname) + + if ct.running: + for group in ct.get_running_config_item('lxc.group').rstrip().split('\n'): + if not group in inventory: + inventory[group] = { + 'hosts': [], + 'vars': { + 'ansible_user': 'root', + }, + } + inventory[group]['hosts'].append(ctname) + inventory['_meta']['hostvars'][ctname] = { + 'ansible_host': ct.get_ips()[0], + } + + +print(json.dumps(inventory)) +