feat: can remove namespace name in host vars

This commit is contained in:
egarette@silique.fr 2025-11-21 08:25:11 +01:00
parent 58b68a9627
commit 6c0cb52c69
5 changed files with 50 additions and 36 deletions

View file

@ -5,8 +5,8 @@
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: \n" "Project-Id-Version: \n"
"POT-Creation-Date: 2025-07-03 22:11+0300\n" "POT-Creation-Date: 2025-11-21 08:24+0100\n"
"PO-Revision-Date: 2025-07-03 22:11+0300\n" "PO-Revision-Date: 2025-11-21 08:24+0100\n"
"Last-Translator: \n" "Last-Translator: \n"
"Language-Team: \n" "Language-Team: \n"
"Language: fr\n" "Language: fr\n"
@ -14,23 +14,23 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n" "Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n" "Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n" "Generated-By: pygettext.py 1.5\n"
"X-Generator: Poedit 3.6\n" "X-Generator: Poedit 3.8\n"
#: src/rougail/output_ansible/__init__.py:58 #: src/rougail/output_ansible/__init__.py:59
msgid "no namespace configured" msgid "no namespace configured"
msgstr "aucun espace de nom configuré" msgstr "aucun espace de nom configuré"
#: src/rougail/output_ansible/__init__.py:67 #: src/rougail/output_ansible/__init__.py:68
msgid "cannot find host namespace \"{0}\"" msgid "cannot find host namespace \"{0}\""
msgstr "ne peut trouve l'espace de nom de l'hôte \"{0}\"" msgstr "ne peut trouve l'espace de nom de l'hôte \"{0}\""
#: src/rougail/output_ansible/__init__.py:74 #: src/rougail/output_ansible/__init__.py:75
msgid "malformated host namespace \"{0}\", should have the \"hostnames\" key" msgid "malformated host namespace \"{0}\", should have the \"hostnames\" key"
msgstr "" msgstr ""
"l'espace de nom de l'hôte \"{0}\" est mal formaté, devrait avoir la clé " "l'espace de nom de l'hôte \"{0}\" est mal formaté, devrait avoir la clé "
"\"hostnames\"" "\"hostnames\""
#: src/rougail/output_ansible/__init__.py:113 #: src/rougail/output_ansible/__init__.py:105
msgid "cannot find \"hostnames\" in \"{0}\" namespace" msgid "cannot find \"hostnames\" in \"{0}\" namespace"
msgstr "ne peut trouve \"hostnames\" dans l'espace de nom \"{0}\"" msgstr "ne peut trouve \"hostnames\" dans l'espace de nom \"{0}\""
@ -42,23 +42,26 @@ msgstr "paramètre ajouté seulement pour être compatible avec Ansible"
msgid "Configuration of user data or output Ansible" msgid "Configuration of user data or output Ansible"
msgstr "Configuration de Ansible comme donnée utilisateur ou sortie" msgstr "Configuration de Ansible comme donnée utilisateur ou sortie"
#: src/rougail/output_ansible/config.py:64 #: src/rougail/output_ansible/config.py:65
msgid "Configuration of output Ansible" msgid "Configuration of output Ansible"
msgstr "Configuration de la sortie Ansible" msgstr "Configuration de la sortie Ansible"
#: src/rougail/output_ansible/config.py:76 #: src/rougail/output_ansible/config.py:77
msgid "Namespace with host values" msgid "Namespace with host values"
msgstr "Espace de nom avec les valeurs de l'hôte" msgstr "Espace de nom avec les valeurs de l'hôte"
#: src/rougail/output_ansible/config.py:80 #: src/rougail/output_ansible/config.py:81
msgid "Only variables in host namespace is available by a host" msgid "Remove namespace name in host vars"
msgstr "" msgstr "Supprime le nom de l'espace de nom dans les vars de l'hôte"
"Seules les variables de l'espace de nom de l'hôte est valable pour un hôte"
#: src/rougail/output_ansible/config.py:84 #: src/rougail/output_ansible/config.py:85
msgid "Displays warnings inside Ansible exportation datas" msgid "Displays warnings inside Ansible exportation datas"
msgstr "Affiche les avertissements dans les données d'exportation Ansible" msgstr "Affiche les avertissements dans les données d'exportation Ansible"
#~ msgid "Only variables in host namespace is available by a host"
#~ msgstr ""
#~ "Seules les variables de l'espace de nom de l'hôte est valable pour un hôte"
#~ msgid "All variables is available for all hosts" #~ msgid "All variables is available for all hosts"
#~ msgstr "Tous les variables sont valables pour tous les hôtes" #~ msgstr "Tous les variables sont valables pour tous les hôtes"

View file

@ -5,7 +5,7 @@
msgid "" msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2025-07-03 22:11+0300\n" "POT-Creation-Date: 2025-11-21 08:24+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@ -15,19 +15,19 @@ msgstr ""
"Generated-By: pygettext.py 1.5\n" "Generated-By: pygettext.py 1.5\n"
#: src/rougail/output_ansible/__init__.py:58 #: src/rougail/output_ansible/__init__.py:59
msgid "no namespace configured" msgid "no namespace configured"
msgstr "" msgstr ""
#: src/rougail/output_ansible/__init__.py:67 #: src/rougail/output_ansible/__init__.py:68
msgid "cannot find host namespace \"{0}\"" msgid "cannot find host namespace \"{0}\""
msgstr "" msgstr ""
#: src/rougail/output_ansible/__init__.py:74 #: src/rougail/output_ansible/__init__.py:75
msgid "malformated host namespace \"{0}\", should have the \"hostnames\" key" msgid "malformated host namespace \"{0}\", should have the \"hostnames\" key"
msgstr "" msgstr ""
#: src/rougail/output_ansible/__init__.py:113 #: src/rougail/output_ansible/__init__.py:105
msgid "cannot find \"hostnames\" in \"{0}\" namespace" msgid "cannot find \"hostnames\" in \"{0}\" namespace"
msgstr "" msgstr ""
@ -39,19 +39,19 @@ msgstr ""
msgid "Configuration of user data or output Ansible" msgid "Configuration of user data or output Ansible"
msgstr "" msgstr ""
#: src/rougail/output_ansible/config.py:64 #: src/rougail/output_ansible/config.py:65
msgid "Configuration of output Ansible" msgid "Configuration of output Ansible"
msgstr "" msgstr ""
#: src/rougail/output_ansible/config.py:76 #: src/rougail/output_ansible/config.py:77
msgid "Namespace with host values" msgid "Namespace with host values"
msgstr "" msgstr ""
#: src/rougail/output_ansible/config.py:80 #: src/rougail/output_ansible/config.py:81
msgid "Only variables in host namespace is available by a host" msgid "Remove namespace name in host vars"
msgstr "" msgstr ""
#: src/rougail/output_ansible/config.py:84 #: src/rougail/output_ansible/config.py:85
msgid "Displays warnings inside Ansible exportation datas" msgid "Displays warnings inside Ansible exportation datas"
msgstr "" msgstr ""

View file

@ -43,11 +43,11 @@ class RougailOutputAnsible(RougailOutputJson):
self.support_namespace = True self.support_namespace = True
except AttributeError: except AttributeError:
self.support_namespace = False self.support_namespace = False
self.host_namespace = self.rougailconfig["ansible.host_namespace"]
self.namespace_is_hostname = self.rougailconfig["ansible.namespace_is_hostname"]
self.export_warnings = self.rougailconfig["ansible.export_warnings"]
def exporter(self) -> None: def exporter(self) -> None:
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"]
super().exporter() super().exporter()
self.json_to_ansible() self.json_to_ansible()
# never return code 1, error are in the output data # never return code 1, error are in the output data
@ -76,11 +76,13 @@ class RougailOutputAnsible(RougailOutputJson):
'malformated host namespace "{0}", should have the "hostnames" key' 'malformated host namespace "{0}", should have the "hostnames" key'
).format(self.host_namespace) ).format(self.host_namespace)
) )
# error is added, so replay manage_errors
return super().manage_errors() return super().manage_errors()
return True return True
def json_to_ansible(self): def json_to_ansible(self):
ret = {"_meta": {"hostvars": {}}} ret = {"_meta": {"hostvars": {}}}
namespaces = {}
if "_warnings" in self.dico: if "_warnings" in self.dico:
_warnings = self.dico.pop("_warnings") _warnings = self.dico.pop("_warnings")
if self.export_warnings: if self.export_warnings:
@ -120,6 +122,8 @@ class RougailOutputAnsible(RougailOutputJson):
host_name = hosts["prefix_name"] + index host_name = hosts["prefix_name"] + index
else: else:
host_name = host host_name = host
if "namespaces" in hosts:
namespaces[host] = hosts["namespaces"]
ret_hosts.setdefault(name, {})[host_name] = host ret_hosts.setdefault(name, {})[host_name] = host
ret.setdefault(name, {}).setdefault("hosts", []).append( ret.setdefault(name, {}).setdefault("hosts", []).append(
host_name host_name
@ -129,12 +133,19 @@ class RougailOutputAnsible(RougailOutputJson):
for hosts in ret_hosts.values(): for hosts in ret_hosts.values():
for host, domain_name in hosts.items(): for host, domain_name in hosts.items():
ret["_meta"]["hostvars"][host] = {"ansible_host": domain_name} ret["_meta"]["hostvars"][host] = {"ansible_host": domain_name}
if self.namespace_is_hostname: if self.no_namespace_in_vars:
host_namespace = normalize_family(host) if host in namespaces:
if host_namespace in self.dico: for namespace in namespaces[host]:
ret["_meta"]["hostvars"][host].update( if namespace in self.dico:
self.dico[host_namespace] ret["_meta"]["hostvars"][host].update(self.dico[namespace])
) else:
for ns_vars in self.dico.values():
ret["_meta"]["hostvars"][host].update(ns_vars)
else:
if host in namespaces:
for namespace in namespaces[host]:
if namespace in self.dico:
ret["_meta"]["hostvars"][host][namespace] = self.dico[namespace]
else: else:
ret["_meta"]["hostvars"][host].update(self.dico) ret["_meta"]["hostvars"][host].update(self.dico)
self.dico = ret self.dico = ret

View file

@ -77,8 +77,8 @@ ansible:
description: {_('Namespace with host values')} description: {_('Namespace with host values')}
default: "hosts" default: "hosts"
namespace_is_hostname: no_namespace_in_vars:
description: {_('Only variables in host namespace is available by a host')} description: {_('Remove namespace name in host vars')}
default: false default: false
export_warnings: export_warnings: