#!/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))