From 2958a53b7d849fdd6094824636ea39e634e1d39d Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Thu, 28 Nov 2024 22:04:39 +0100 Subject: [PATCH] fix: better errors support --- src/rougail/output_ansible/__init__.py | 57 +++++++++++++------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/rougail/output_ansible/__init__.py b/src/rougail/output_ansible/__init__.py index 0117855..8151cb6 100644 --- a/src/rougail/output_ansible/__init__.py +++ b/src/rougail/output_ansible/__init__.py @@ -19,6 +19,7 @@ along with this program. If not, see . from typing import Optional from json import dumps +from .i18n import _ from ..output_json import RougailOutputJson @@ -43,39 +44,39 @@ class RougailOutputAnsible(RougailOutputJson): self.errors = [] self.warnings = [] - def run(self) -> None: - self.exporter() - print(dumps(self.json_to_ansible(), ensure_ascii=False, indent=2)) + def exporter(self) -> None: + super().exporter() + self.json_to_ansible() def json_to_ansible(self): + ret = {"_meta": {"hostvars": {}}, "all": {"children": ["ungrouped"]}} 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]: + elif '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 + 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) + self.dico = ret RougailOutput = RougailOutputAnsible