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)
|
2024-11-27 17:19:30 +01:00
|
|
|
|
2024-11-28 22:04:39 +01:00
|
|
|
def exporter(self) -> None:
|
2025-11-21 08:25:11 +01:00
|
|
|
self.host_namespace = self.rougailconfig["ansible.host_namespace"]
|
|
|
|
|
self.export_warnings = self.rougailconfig["ansible.export_warnings"]
|
|
|
|
|
self.no_namespace_in_vars = self.rougailconfig["ansible.no_namespace_in_vars"]
|
2025-12-22 09:06:12 +01:00
|
|
|
self.hosts = {}
|
2024-11-28 22:04:39 +01:00
|
|
|
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
|
|
|
|
2025-12-22 09:06:12 +01:00
|
|
|
def parse_variable(self, option, child, namespace):
|
|
|
|
|
if self.support_namespace and namespace and "ansible_host" in option.information.get("tags", tuple()):
|
|
|
|
|
hosts = option.value.get()
|
|
|
|
|
if not isinstance(hosts, list):
|
|
|
|
|
hosts = [hosts]
|
|
|
|
|
if namespace in self.hosts:
|
|
|
|
|
self.hosts[namespace].update(hosts)
|
|
|
|
|
else:
|
|
|
|
|
self.hosts[namespace] = hosts
|
|
|
|
|
super().parse_variable(option, child, namespace)
|
|
|
|
|
|
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"))
|
2025-12-22 09:06:12 +01:00
|
|
|
hosts_config = self.config.option(self.host_namespace)
|
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-12-22 09:06:12 +01:00
|
|
|
if not self.hosts:
|
|
|
|
|
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)
|
|
|
|
|
)
|
2025-11-21 08:25:11 +01:00
|
|
|
# error is added, so replay manage_errors
|
2024-12-02 20:22:27 +01:00
|
|
|
return super().manage_errors()
|
|
|
|
|
return True
|
|
|
|
|
|
2024-11-27 17:19:30 +01:00
|
|
|
def json_to_ansible(self):
|
2025-09-10 17:42:09 +02:00
|
|
|
ret = {"_meta": {"hostvars": {}}}
|
2025-11-21 08:25:11 +01:00
|
|
|
namespaces = {}
|
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)
|
2025-12-22 09:06:12 +01:00
|
|
|
# manage groups
|
2024-12-02 20:22:27 +01:00
|
|
|
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:
|
|
|
|
|
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
|
2025-09-10 17:42:09 +02:00
|
|
|
else:
|
|
|
|
|
host_name = host
|
2025-11-21 08:25:11 +01:00
|
|
|
if "namespaces" in hosts:
|
|
|
|
|
namespaces[host] = hosts["namespaces"]
|
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
|
|
|
|
|
)
|
2024-11-28 22:04:39 +01:00
|
|
|
else:
|
|
|
|
|
ret[name] = hosts
|
2025-12-22 09:06:12 +01:00
|
|
|
# manage hostsnames and vars in hostsname
|
2024-11-28 22:04:39 +01:00
|
|
|
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-11-21 08:25:11 +01:00
|
|
|
if self.no_namespace_in_vars:
|
|
|
|
|
if host in namespaces:
|
|
|
|
|
for namespace in namespaces[host]:
|
|
|
|
|
if namespace in self.dico:
|
|
|
|
|
ret["_meta"]["hostvars"][host].update(self.dico[namespace])
|
|
|
|
|
else:
|
|
|
|
|
for ns_vars in self.dico.values():
|
|
|
|
|
ret["_meta"]["hostvars"][host].update(ns_vars)
|
2025-01-02 21:38:17 +01:00
|
|
|
else:
|
2025-11-21 08:25:11 +01:00
|
|
|
if host in namespaces:
|
|
|
|
|
for namespace in namespaces[host]:
|
|
|
|
|
if namespace in self.dico:
|
|
|
|
|
ret["_meta"]["hostvars"][host][namespace] = self.dico[namespace]
|
|
|
|
|
else:
|
|
|
|
|
ret["_meta"]["hostvars"][host].update(self.dico)
|
2025-12-22 09:06:12 +01:00
|
|
|
# manage hostnames define with tag ansible_host and add groups
|
|
|
|
|
for namespace, hosts in self.hosts.items():
|
|
|
|
|
if namespace not in ret:
|
|
|
|
|
ret[namespace] = {"hosts": []}
|
|
|
|
|
for host in hosts:
|
|
|
|
|
if host not in ret["_meta"]["hostvars"]:
|
|
|
|
|
ret["_meta"]["hostvars"][host] = {"ansible_host": host}
|
|
|
|
|
if self.no_namespace_in_vars:
|
|
|
|
|
ret["_meta"]["hostvars"][host].update(self.dico[namespace])
|
|
|
|
|
elif namespace not in ret["_meta"]["hostvars"][host]:
|
|
|
|
|
ret["_meta"]["hostvars"][host][namespace] = self.dico[namespace]
|
|
|
|
|
if host not in ret[namespace]["hosts"]:
|
|
|
|
|
ret[namespace]["hosts"].append(host)
|
2024-11-28 22:04:39 +01:00
|
|
|
self.dico = ret
|
2024-11-27 17:19:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
RougailOutput = RougailOutputAnsible
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = ("RougailOutputAnsible",)
|