2022-10-01 22:33:11 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
'''
|
|
|
|
Example custom dynamic inventory script for Ansible, in Python.
|
|
|
|
'''
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2022-12-21 16:35:58 +01:00
|
|
|
from json import load as json_load, dumps, JSONEncoder
|
2022-10-01 22:33:11 +02:00
|
|
|
from os import remove
|
|
|
|
from os.path import isfile
|
|
|
|
from asyncio import run
|
2022-12-21 16:35:58 +01:00
|
|
|
from traceback import print_exc
|
2022-10-01 22:33:11 +02:00
|
|
|
|
2022-12-21 16:35:58 +01:00
|
|
|
from risotto.machine import load, TIRAMISU_CACHE, VALUES_CACHE, INFORMATIONS_CACHE, ROUGAIL_NAMESPACE, ROUGAIL_NAMESPACE_DESCRIPTION
|
|
|
|
from tiramisu import Config
|
2022-10-01 22:33:11 +02:00
|
|
|
from tiramisu.error import PropertiesOptionError
|
|
|
|
from rougail.utils import normalize_family
|
2022-12-21 16:35:58 +01:00
|
|
|
from rougail import RougailSystemdTemplate, RougailConfig
|
2022-10-01 22:33:11 +02:00
|
|
|
from rougail.template.base import RougailLeader, RougailExtra
|
|
|
|
|
2022-12-21 16:35:58 +01:00
|
|
|
|
|
|
|
DEBUG = False
|
2022-10-01 22:33:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class RougailEncoder(JSONEncoder):
|
|
|
|
def default(self, obj):
|
|
|
|
if isinstance(obj, RougailLeader):
|
|
|
|
return obj._follower
|
|
|
|
if isinstance(obj, RougailExtra):
|
|
|
|
return obj._suboption
|
|
|
|
if isinstance(obj, PropertiesOptionError):
|
|
|
|
return 'PropertiesOptionError'
|
|
|
|
return JSONEncoder.default(self, obj)
|
|
|
|
|
|
|
|
|
|
|
|
class RisottoInventory(object):
|
|
|
|
def __init__(self):
|
|
|
|
parser = ArgumentParser()
|
|
|
|
parser.add_argument('--list', action='store_true')
|
|
|
|
parser.add_argument('--host', action='store')
|
2022-12-21 16:35:58 +01:00
|
|
|
parser.add_argument('--nocache', action='store_true')
|
|
|
|
parser.add_argument('--debug', action='store_true')
|
2022-10-01 22:33:11 +02:00
|
|
|
self.args = parser.parse_args()
|
2022-12-21 16:35:58 +01:00
|
|
|
if self.args.debug:
|
|
|
|
global DEBUG
|
|
|
|
DEBUG = True
|
2022-10-01 22:33:11 +02:00
|
|
|
|
|
|
|
async def run(self):
|
2022-12-21 16:35:58 +01:00
|
|
|
if self.args.list and self.args.host:
|
|
|
|
raise Exception('cannot have --list and --host together')
|
|
|
|
if self.args.list or self.args.nocache:
|
2022-10-01 22:33:11 +02:00
|
|
|
if isfile(TIRAMISU_CACHE):
|
|
|
|
remove(TIRAMISU_CACHE)
|
|
|
|
if isfile(VALUES_CACHE):
|
|
|
|
remove(VALUES_CACHE)
|
2022-12-21 16:35:58 +01:00
|
|
|
if isfile(INFORMATIONS_CACHE):
|
|
|
|
remove(INFORMATIONS_CACHE)
|
|
|
|
config = await load(TIRAMISU_CACHE,
|
|
|
|
VALUES_CACHE,
|
|
|
|
INFORMATIONS_CACHE,
|
|
|
|
)
|
|
|
|
if self.args.list:
|
|
|
|
return await self.do_inventory(config)
|
2022-10-01 22:33:11 +02:00
|
|
|
elif self.args.host:
|
2022-12-21 16:35:58 +01:00
|
|
|
return await self.get_vars(config, self.args.host)
|
2022-10-01 22:33:11 +02:00
|
|
|
raise Exception('pfff')
|
|
|
|
|
2022-12-21 16:35:58 +01:00
|
|
|
async def do_inventory(self,
|
|
|
|
config: Config,
|
|
|
|
) -> dict:
|
|
|
|
servers = [await subconfig.option.doc() for subconfig in await config.option.list('optiondescription') if await subconfig.information.get('module') == 'host']
|
2022-10-01 22:33:11 +02:00
|
|
|
return dumps({
|
|
|
|
'group': {
|
|
|
|
'hosts': servers,
|
|
|
|
'vars': {
|
|
|
|
# FIXME
|
2022-12-21 16:35:58 +01:00
|
|
|
# 'ansible_ssh_host': '192.168.0.100',
|
2022-10-01 22:33:11 +02:00
|
|
|
'ansible_ssh_user': 'root',
|
|
|
|
'ansible_python_interpreter': '/usr/bin/python3'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
async def get_vars(self,
|
2022-12-21 16:35:58 +01:00
|
|
|
config: Config,
|
2022-10-01 22:33:11 +02:00
|
|
|
host_name: str,
|
|
|
|
) -> dict:
|
|
|
|
ret = {}
|
2022-12-21 16:35:58 +01:00
|
|
|
rougailconfig = RougailConfig.copy()
|
|
|
|
rougailconfig['variable_namespace'] = ROUGAIL_NAMESPACE
|
|
|
|
rougailconfig['variable_namespace_description'] = ROUGAIL_NAMESPACE_DESCRIPTION
|
|
|
|
for subconfig in await config.option.list('optiondescription'):
|
|
|
|
server_name = await subconfig.option.description()
|
|
|
|
module_name = await subconfig.option(await subconfig.information.get('provider:global:module_name')).value.get()
|
|
|
|
if module_name == 'host' and server_name != host_name:
|
2022-10-01 22:33:11 +02:00
|
|
|
continue
|
|
|
|
engine = RougailSystemdTemplate(subconfig, rougailconfig)
|
|
|
|
await engine.load_variables()
|
2022-12-21 16:35:58 +01:00
|
|
|
if module_name != 'host' and engine.rougail_variables_dict['general']['host'] != host_name:
|
2022-10-01 22:33:11 +02:00
|
|
|
continue
|
|
|
|
ret[server_name] = engine.rougail_variables_dict
|
2022-12-21 16:35:58 +01:00
|
|
|
ret['modules'] = await config.information.get('modules')
|
2022-10-17 18:51:04 +02:00
|
|
|
ret['delete_old_image'] = False
|
2022-10-01 22:33:11 +02:00
|
|
|
ret['configure_host'] = True
|
|
|
|
ret['only_machine'] = None
|
2023-01-23 20:23:32 +01:00
|
|
|
ret['copy_templates'] = False
|
|
|
|
ret['copy_tests'] = False
|
2022-12-21 16:35:58 +01:00
|
|
|
ret['host_install_dir'] = ret[host_name].pop('host_install_dir')
|
2022-10-01 22:33:11 +02:00
|
|
|
return dumps(ret, cls=RougailEncoder)
|
|
|
|
|
|
|
|
|
|
|
|
# Get the inventory.
|
|
|
|
async def main():
|
2022-12-21 16:35:58 +01:00
|
|
|
try:
|
|
|
|
inv = RisottoInventory()
|
|
|
|
values = await inv.run()
|
|
|
|
print(values)
|
|
|
|
except Exception as err:
|
|
|
|
if DEBUG:
|
|
|
|
print_exc()
|
2023-01-23 20:23:32 +01:00
|
|
|
print(err)
|
2022-10-01 22:33:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
run(main())
|