84 lines
3.2 KiB
Python
84 lines
3.2 KiB
Python
"""
|
|
Silique (https://www.silique.fr)
|
|
Copyright (C) 2022-2024
|
|
|
|
This program is free software: you can redistribute it and/or modify it
|
|
under the terms of the GNU Lesser General Public License as published by the
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
|
details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
"""
|
|
|
|
from typing import Optional
|
|
from json import dumps
|
|
|
|
from ..output_json import RougailOutputJson
|
|
|
|
|
|
class RougailOutputAnsible(RougailOutputJson):
|
|
def __init__(
|
|
self,
|
|
config: "Config",
|
|
rougailconfig: "RougailConfig" = None,
|
|
user_data_errors: Optional[list] = None,
|
|
user_data_warnings: Optional[list] = None,
|
|
) -> None:
|
|
if rougailconfig is None:
|
|
from rougail import RougailConfig
|
|
|
|
rougailconfig = RougailConfig
|
|
self.rougailconfig = rougailconfig
|
|
self.host_namespace = self.rougailconfig["ansible.host_namespace"]
|
|
self.config = config
|
|
self.read_write = False
|
|
self.is_mandatory = True
|
|
self.dico = {}
|
|
self.errors = []
|
|
self.warnings = []
|
|
|
|
def run(self) -> None:
|
|
self.exporter()
|
|
print(dumps(self.json_to_ansible(), ensure_ascii=False, indent=2))
|
|
|
|
def json_to_ansible(self):
|
|
if self.host_namespace not in self.dico:
|
|
self.errors.append(_('cannot find hosts namespace "{0}"').format(self.host_namespace))
|
|
if 'hostnames' not in self.dico[self.host_namespace]:
|
|
self.errors.append(_('malformated hosts namespace "{0}", should has "hostnames"').format(self.host_namespace))
|
|
ret = {"_meta": {"hostvars": {}}, "all": {"children": ["ungrouped"]}}
|
|
# if self.errors:
|
|
# ret["_meta"]["hostvars"]["localhost"] = {'_errors': self.errors}
|
|
# ret["ungrouped"] = {"hosts": ["localhost"]}
|
|
# else:
|
|
hostnames = self.dico[self.host_namespace]['hostnames']
|
|
ret_hosts = {}
|
|
for name, hosts in hostnames.items():
|
|
if 'hosts' in hosts:
|
|
for idx, host in enumerate(hosts['hosts']):
|
|
index = str(idx + 1)
|
|
if idx < 9:
|
|
index = '0' + index
|
|
host_name = hosts['prefix_name'] + index
|
|
ret_hosts.setdefault(name, {})[host_name] = host
|
|
ret.setdefault(name, {}).setdefault('hosts', []).append(host_name)
|
|
else:
|
|
ret["all"]["children"].append(name)
|
|
ret[name] = hosts
|
|
for hosts in ret_hosts.values():
|
|
for host, domain_name in hosts.items():
|
|
ret['_meta']['hostvars'][host] = {'ansible_host': domain_name}
|
|
ret['_meta']['hostvars'][host].update(self.dico)
|
|
return ret
|
|
|
|
|
|
RougailOutput = RougailOutputAnsible
|
|
|
|
|
|
__all__ = ("RougailOutputAnsible",)
|