2024-11-27 17:19:30 +01:00
|
|
|
"""
|
|
|
|
|
Silique (https://www.silique.fr)
|
2025-02-10 09:57:38 +01:00
|
|
|
Copyright (C) 2022-2025
|
2024-11-27 17:19:30 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2024-12-02 20:22:27 +01:00
|
|
|
from tiramisu import groups
|
2025-06-18 06:42:52 +02:00
|
|
|
from rougail.tiramisu import normalize_family
|
2024-12-02 20:22:27 +01:00
|
|
|
|
2024-11-28 22:04:39 +01:00
|
|
|
from .i18n import _
|
2025-04-09 21:31:38 +02:00
|
|
|
from .__version__ import __version__
|
2024-11-27 17:19:30 +01:00
|
|
|
from ..output_json import RougailOutputJson
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RougailOutputAnsible(RougailOutputJson):
|
2024-12-02 20:22:27 +01:00
|
|
|
output_name = "ansible"
|
|
|
|
|
|
2024-11-27 17:19:30 +01:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
config: "Config",
|
2024-12-02 20:22:27 +01:00
|
|
|
*,
|
2024-11-27 17:19:30 +01:00
|
|
|
rougailconfig: "RougailConfig" = None,
|
2024-12-02 20:22:27 +01:00
|
|
|
**kwargs,
|
2024-11-27 17:19:30 +01:00
|
|
|
) -> None:
|
2024-12-02 20:22:27 +01:00
|
|
|
super().__init__(config, rougailconfig=rougailconfig, **kwargs)
|
|
|
|
|
try:
|
|
|
|
|
groups.namespace
|
|
|
|
|
self.support_namespace = True
|
|
|
|
|
except AttributeError:
|
|
|
|
|
self.support_namespace = False
|
2024-11-27 17:19:30 +01:00
|
|
|
self.host_namespace = self.rougailconfig["ansible.host_namespace"]
|
2025-01-02 21:38:17 +01:00
|
|
|
self.namespace_is_hostname = self.rougailconfig["ansible.namespace_is_hostname"]
|
2025-07-04 06:43:31 +02:00
|
|
|
self.export_warnings = self.rougailconfig["ansible.export_warnings"]
|
2024-11-27 17:19:30 +01:00
|
|
|
|
2024-11-28 22:04:39 +01:00
|
|
|
def exporter(self) -> None:
|
|
|
|
|
super().exporter()
|
|
|
|
|
self.json_to_ansible()
|
2025-02-10 09:57:38 +01:00
|
|
|
# never return code 1, error are in the output data
|
|
|
|
|
return True
|
2024-11-27 17:19:30 +01:00
|
|
|
|
2024-12-02 20:22:27 +01:00
|
|
|
def manage_errors(self) -> bool:
|
|
|
|
|
if not super().manage_errors():
|
|
|
|
|
if not self.support_namespace:
|
2025-05-11 19:14:26 +02:00
|
|
|
self.errors.append(_("no namespace configured"))
|
|
|
|
|
hosts_config = self.config.option("hosts")
|
2024-12-02 20:22:27 +01:00
|
|
|
try:
|
|
|
|
|
if hosts_config.group_type() != groups.namespace:
|
|
|
|
|
hosts_config = None
|
|
|
|
|
except AttributeError:
|
|
|
|
|
hosts_config = None
|
|
|
|
|
if not hosts_config:
|
2025-05-11 19:14:26 +02:00
|
|
|
self.errors.append(
|
|
|
|
|
_('cannot find host namespace "{0}"').format(self.host_namespace)
|
|
|
|
|
)
|
2024-12-02 20:22:27 +01:00
|
|
|
else:
|
|
|
|
|
try:
|
2025-05-11 19:14:26 +02:00
|
|
|
hosts_config.option("hostnames").name()
|
2024-12-02 20:22:27 +01:00
|
|
|
except AttributeError:
|
2025-05-11 19:14:26 +02:00
|
|
|
self.errors.append(
|
|
|
|
|
_(
|
|
|
|
|
'malformated host namespace "{0}", should have the "hostnames" key'
|
|
|
|
|
).format(self.host_namespace)
|
|
|
|
|
)
|
2024-12-02 20:22:27 +01:00
|
|
|
return super().manage_errors()
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def parse_family(
|
|
|
|
|
self,
|
|
|
|
|
conf,
|
|
|
|
|
child,
|
|
|
|
|
):
|
|
|
|
|
if self.read_write and conf.path() == self.host_namespace:
|
|
|
|
|
self.config.property.read_only()
|
|
|
|
|
super().parse_family(conf, child)
|
|
|
|
|
if self.read_write and conf.path() == self.host_namespace:
|
|
|
|
|
self.config.property.read_write()
|
|
|
|
|
|
2024-11-27 17:19:30 +01:00
|
|
|
def json_to_ansible(self):
|
2025-09-08 16:53:15 +02:00
|
|
|
ret = {"_meta": {"hostvars": {}}, "all": {"children": ["ungrouped"], "hosts": []}}
|
|
|
|
|
all_children = []
|
2025-05-11 19:14:26 +02:00
|
|
|
if "_warnings" in self.dico:
|
2025-07-04 06:43:31 +02:00
|
|
|
_warnings = self.dico.pop("_warnings")
|
|
|
|
|
if self.export_warnings:
|
|
|
|
|
ret["_meta"]["hostvars"]["localhost"] = {
|
|
|
|
|
"_warnings": _warnings,
|
|
|
|
|
}
|
|
|
|
|
ret["ungrouped"] = {"hosts": ["localhost"]}
|
2025-05-11 19:14:26 +02:00
|
|
|
if "_errors" in self.dico:
|
|
|
|
|
ret["_meta"]["hostvars"].setdefault("localhost", {})["_errors"] = (
|
|
|
|
|
self.dico.pop("_errors")
|
|
|
|
|
)
|
2024-12-02 20:22:27 +01:00
|
|
|
if "ungrouped" not in ret:
|
|
|
|
|
ret["ungrouped"] = {"hosts": ["localhost"]}
|
|
|
|
|
if self.host_namespace in self.dico:
|
|
|
|
|
hosts = self.dico.pop(self.host_namespace)
|
|
|
|
|
if "hostnames" not in hosts:
|
2025-05-11 19:14:26 +02:00
|
|
|
ret["_meta"]["hostvars"].setdefault("localhost", {}).setdefault(
|
|
|
|
|
"_errors", []
|
|
|
|
|
).append(
|
|
|
|
|
_('cannot find "hostnames" in "{0}" namespace').format(
|
|
|
|
|
self.host_namespace
|
|
|
|
|
)
|
|
|
|
|
)
|
2024-12-02 20:22:27 +01:00
|
|
|
if "ungrouped" not in ret:
|
|
|
|
|
ret["ungrouped"] = {"hosts": ["localhost"]}
|
|
|
|
|
hostnames = {}
|
|
|
|
|
else:
|
2025-05-11 19:14:26 +02:00
|
|
|
hostnames = hosts["hostnames"]
|
2024-11-28 22:04:39 +01:00
|
|
|
ret_hosts = {}
|
|
|
|
|
for name, hosts in hostnames.items():
|
2025-05-11 19:14:26 +02:00
|
|
|
if "hosts" in hosts:
|
|
|
|
|
if "prefix_name" in hosts:
|
|
|
|
|
host_name = hosts["prefix_name"]
|
2025-01-02 21:38:17 +01:00
|
|
|
add_index = True
|
|
|
|
|
elif len(hosts["hosts"]) == 1:
|
|
|
|
|
host_name = hosts["hosts"][0]
|
|
|
|
|
add_index = False
|
|
|
|
|
else:
|
|
|
|
|
raise Exception("cannot find prefix_name")
|
2025-05-11 19:14:26 +02:00
|
|
|
for idx, host in enumerate(hosts["hosts"]):
|
2024-11-28 22:04:39 +01:00
|
|
|
index = str(idx + 1)
|
|
|
|
|
if idx < 9:
|
2025-05-11 19:14:26 +02:00
|
|
|
index = "0" + index
|
|
|
|
|
if "prefix_name" in hosts:
|
|
|
|
|
host_name = hosts["prefix_name"] + index
|
2024-11-28 22:04:39 +01:00
|
|
|
ret_hosts.setdefault(name, {})[host_name] = host
|
2025-05-11 19:14:26 +02:00
|
|
|
ret.setdefault(name, {}).setdefault("hosts", []).append(
|
|
|
|
|
host_name
|
|
|
|
|
)
|
2025-09-08 16:53:15 +02:00
|
|
|
if host not in all_children:
|
|
|
|
|
all_children.append(host)
|
|
|
|
|
ret["all"]["hosts"].append(host_name)
|
2024-11-28 22:04:39 +01:00
|
|
|
else:
|
|
|
|
|
ret[name] = hosts
|
|
|
|
|
for hosts in ret_hosts.values():
|
|
|
|
|
for host, domain_name in hosts.items():
|
2025-05-11 19:14:26 +02:00
|
|
|
ret["_meta"]["hostvars"][host] = {"ansible_host": domain_name}
|
2025-01-02 21:38:17 +01:00
|
|
|
if self.namespace_is_hostname:
|
|
|
|
|
host_namespace = normalize_family(host)
|
|
|
|
|
if host_namespace in self.dico:
|
2025-05-11 19:14:26 +02:00
|
|
|
ret["_meta"]["hostvars"][host].update(
|
|
|
|
|
self.dico[host_namespace]
|
|
|
|
|
)
|
2025-01-02 21:38:17 +01:00
|
|
|
else:
|
2025-05-11 19:14:26 +02:00
|
|
|
ret["_meta"]["hostvars"][host].update(self.dico)
|
2024-11-28 22:04:39 +01:00
|
|
|
self.dico = ret
|
2024-11-27 17:19:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
RougailOutput = RougailOutputAnsible
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ("RougailOutputAnsible",)
|