2024-09/plugins/inventory/lxc.py

76 lines
2.2 KiB
Python
Raw Normal View History

2024-09-20 13:57:42 +00:00
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = r'''
'''
EXAMPLES = r'''
'''
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable
from ansible.errors import AnsibleError
try:
import lxc
except ImportError:
raise AnsibleError('the lxc inventory plugin requires lxc-python.')
class InventoryModule(BaseInventoryPlugin, Constructable):
NAME = 'lxc'
def parse(self, inventory, loader, path, cache=True):
super(InventoryModule, self).parse(
inventory,
loader,
path,
cache=cache
)
config_data = self._read_config_data(path)
self._consume_options(config_data)
ctnames = lxc.list_containers()
for ctname in ctnames:
ct = lxc.Container(ctname)
if ct.running:
self.inventory.add_host(ctname)
for group in ct.get_running_config_item('lxc.group').rstrip().split('\n'):
self.inventory.add_group(group)
self.inventory.add_child(group, ctname)
self.inventory.set_variable(ctname, 'ansible_connection', 'lxc')
# Get variables for compose
# variables = self.inventory.hosts[ctname].get_vars()
# print(variables)
# print(ctname)
# print( self.get_option('compose') )
# print( self.get_option('strict') )
# # Set composed variables
# self._set_composite_vars(
# self.get_option('compose'),
# variables,
# ctname,
# self.get_option('strict'),
# )
# print('XXX')
#
# # Add host to composed groups
# self._add_host_to_composed_groups(
# self.get_option('groups'),
# variables,
# inventory_hostname,
# self.get_option('strict'),
# )
#
# # Add host to keyed groups
# self._add_host_to_keyed_groups(
# self.get_option('keyed_groups'),
# variables,
# inventory_hostname,
# self.get_option('strict'),
# )