From 773f991274e924ac04731e8e3995d958918a7124 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Thu, 2 Jan 2025 21:38:17 +0100 Subject: [PATCH] feat: add namespace_is_hostname option --- .../fr/LC_MESSAGES/rougail_output_ansible.po | 2 +- locale/rougail_output_ansible.pot | 42 ++---- src/rougail/output_ansible/__init__.py | 20 ++- src/rougail/output_ansible/config.py | 9 +- .../fr/LC_MESSAGES/rougail_output_ansible.mo | Bin 1125 -> 1125 bytes tests/hosts/00-ip.yml | 4 + ..._9default_information_other_variable2.json | 42 ++++++ .../test_namespace/00_9extra_ouside.json | 46 ++++++ .../test_namespace/01_6string_multi.json | 12 +- .../test_namespace/16_6exists_family.json | 36 +++++ ...60_6family_dynamic_sub_dynamic_empty2.json | 134 ++++++++++++++++++ ..._9default_information_other_variable2.json | 42 ++++++ .../00_9extra_ouside.json | 46 ++++++ .../01_6string_multi.json | 12 +- .../16_6exists_family.json | 36 +++++ ...60_6family_dynamic_sub_dynamic_empty2.json | 134 ++++++++++++++++++ ..._9default_information_other_variable2.json | 42 ++++++ .../00_9extra_ouside.json | 46 ++++++ .../01_6string_multi.json | 4 +- .../16_6exists_family.json | 36 +++++ ...60_6family_dynamic_sub_dynamic_empty2.json | 58 ++++++++ ..._9default_information_other_variable2.json | 42 ++++++ .../00_9extra_ouside.json | 46 ++++++ .../01_6string_multi.json | 12 +- .../16_6exists_family.json | 36 +++++ ...60_6family_dynamic_sub_dynamic_empty2.json | 134 ++++++++++++++++++ ..._9default_information_other_variable2.json | 23 +++ .../00_9extra_ouside.json | 46 ++++++ .../16_6exists_family.json | 36 +++++ ...60_6family_dynamic_sub_dynamic_empty2.json | 58 ++++++++ 30 files changed, 1190 insertions(+), 46 deletions(-) create mode 100644 tests/results/test_namespace/00_9default_information_other_variable2.json create mode 100644 tests/results/test_namespace/00_9extra_ouside.json create mode 100644 tests/results/test_namespace/16_6exists_family.json create mode 100644 tests/results/test_namespace/60_6family_dynamic_sub_dynamic_empty2.json create mode 100644 tests/results/test_namespace_mandatory/00_9default_information_other_variable2.json create mode 100644 tests/results/test_namespace_mandatory/00_9extra_ouside.json create mode 100644 tests/results/test_namespace_mandatory/16_6exists_family.json create mode 100644 tests/results/test_namespace_mandatory/60_6family_dynamic_sub_dynamic_empty2.json create mode 100644 tests/results/test_namespace_read_write/00_9default_information_other_variable2.json create mode 100644 tests/results/test_namespace_read_write/00_9extra_ouside.json create mode 100644 tests/results/test_namespace_read_write/16_6exists_family.json create mode 100644 tests/results/test_namespace_read_write/60_6family_dynamic_sub_dynamic_empty2.json create mode 100644 tests/results/test_namespace_read_write_mandatory/00_9default_information_other_variable2.json create mode 100644 tests/results/test_namespace_read_write_mandatory/00_9extra_ouside.json create mode 100644 tests/results/test_namespace_read_write_mandatory/16_6exists_family.json create mode 100644 tests/results/test_namespace_read_write_mandatory/60_6family_dynamic_sub_dynamic_empty2.json create mode 100644 tests/results/test_namespace_read_write_mandatory_errors/00_9default_information_other_variable2.json create mode 100644 tests/results/test_namespace_read_write_mandatory_errors/00_9extra_ouside.json create mode 100644 tests/results/test_namespace_read_write_mandatory_errors/16_6exists_family.json create mode 100644 tests/results/test_namespace_read_write_mandatory_errors/60_6family_dynamic_sub_dynamic_empty2.json diff --git a/locale/fr/LC_MESSAGES/rougail_output_ansible.po b/locale/fr/LC_MESSAGES/rougail_output_ansible.po index b53c2f8..418a75a 100644 --- a/locale/fr/LC_MESSAGES/rougail_output_ansible.po +++ b/locale/fr/LC_MESSAGES/rougail_output_ansible.po @@ -18,7 +18,7 @@ msgstr "" #: src/rougail/output_exporter/__init__.py:73 msgid "The following variables are mandatory but have no value:" -msgstr "Les variables suiveuses sont obligatoire mais n'ont pas de valeur :" +msgstr "Les variables suivantes sont obligatoire mais n'ont pas de valeur :" #: src/rougail/output_exporter/__init__.py:84 msgid "The following variables are inaccessible but are empty and mandatory :" diff --git a/locale/rougail_output_ansible.pot b/locale/rougail_output_ansible.pot index 6fd3c0a..cea5bc1 100644 --- a/locale/rougail_output_ansible.pot +++ b/locale/rougail_output_ansible.pot @@ -5,7 +5,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" -"POT-Creation-Date: 2024-11-01 11:03+0100\n" +"POT-Creation-Date: 2024-12-29 10:03+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -15,43 +15,19 @@ msgstr "" "Generated-By: pygettext.py 1.5\n" -#: src/rougail/output_exporter/__init__.py:73 -msgid "The following variables are mandatory but have no value:" +#: src/rougail/output_ansible/__init__.py:55 +msgid "no namespace configured" msgstr "" -#: src/rougail/output_exporter/__init__.py:84 -msgid "The following variables are inaccessible but are empty and mandatory :" +#: src/rougail/output_ansible/__init__.py:63 +msgid "cannot find hosts namespace \"{0}\"" msgstr "" -#: src/rougail/output_exporter/output/console.py:53 -msgid "Undocumented variable" +#: src/rougail/output_ansible/__init__.py:68 +msgid "malformated hosts namespace \"{0}\", should has \"hostnames\"" msgstr "" -#: src/rougail/output_exporter/output/console.py:54 -msgid "Undocumented but modified variable" -msgstr "" - -#: src/rougail/output_exporter/output/console.py:57 -msgid "Unmodifiable variable" -msgstr "" - -#: src/rougail/output_exporter/output/console.py:61 -msgid "Default value" -msgstr "" - -#: src/rougail/output_exporter/output/console.py:62 -msgid "Modified value" -msgstr "" - -#: src/rougail/output_exporter/output/console.py:63 -msgid "Original default value" -msgstr "" - -#: src/rougail/output_exporter/output/console.py:67 -msgid "Caption" -msgstr "" - -#: src/rougail/output_exporter/output/console.py:92 -msgid "Variables:" +#: src/rougail/output_ansible/__init__.py:95 +msgid "cannot find \"hostnames\" in \"{0}\" namespace" msgstr "" diff --git a/src/rougail/output_ansible/__init__.py b/src/rougail/output_ansible/__init__.py index a9bce01..e8e916d 100644 --- a/src/rougail/output_ansible/__init__.py +++ b/src/rougail/output_ansible/__init__.py @@ -20,6 +20,7 @@ from typing import Optional from json import dumps from tiramisu import groups +from rougail.utils import normalize_family from .i18n import _ from ..output_json import RougailOutputJson @@ -42,6 +43,7 @@ class RougailOutputAnsible(RougailOutputJson): except AttributeError: self.support_namespace = False self.host_namespace = self.rougailconfig["ansible.host_namespace"] + self.namespace_is_hostname = self.rougailconfig["ansible.namespace_is_hostname"] def exporter(self) -> None: super().exporter() @@ -99,11 +101,20 @@ class RougailOutputAnsible(RougailOutputJson): ret_hosts = {} for name, hosts in hostnames.items(): if 'hosts' in hosts: + if 'prefix_name' in hosts: + host_name = hosts['prefix_name'] + add_index = True + elif len(hosts["hosts"]) == 1: + host_name = hosts["hosts"][0] + add_index = False + else: + raise Exception("cannot find prefix_name") for idx, host in enumerate(hosts['hosts']): index = str(idx + 1) if idx < 9: index = '0' + index - host_name = hosts['prefix_name'] + index + if 'prefix_name' in hosts: + host_name = hosts['prefix_name'] + index ret_hosts.setdefault(name, {})[host_name] = host ret.setdefault(name, {}).setdefault('hosts', []).append(host_name) else: @@ -112,7 +123,12 @@ class RougailOutputAnsible(RougailOutputJson): 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) + if self.namespace_is_hostname: + host_namespace = normalize_family(host) + if host_namespace in self.dico: + ret['_meta']['hostvars'][host].update(self.dico[host_namespace]) + else: + ret['_meta']['hostvars'][host].update(self.dico) self.dico = ret diff --git a/src/rougail/output_ansible/config.py b/src/rougail/output_ansible/config.py index e9a5b82..617264c 100644 --- a/src/rougail/output_ansible/config.py +++ b/src/rougail/output_ansible/config.py @@ -25,8 +25,8 @@ def get_rougail_config( ) -> dict: options = """ list: - description: parameter added only to be compatible with Ansible - negative_description: parameter added only to be compatible with Ansible + description: parameter added only to be compatible with Ansible + negative_description: parameter added only to be compatible with Ansible default: false disabled: type: jinja @@ -53,6 +53,11 @@ ansible: host_namespace: description: Namespace with host values default: "hosts" + + namespace_is_hostname: + description: Only variables in hostname namespace is available by a host + negative_description: All variables is available for all hosts + default: false """ return { "name": "ansible", diff --git a/src/rougail/output_ansible/locale/fr/LC_MESSAGES/rougail_output_ansible.mo b/src/rougail/output_ansible/locale/fr/LC_MESSAGES/rougail_output_ansible.mo index fb7e0aee9ace6a4aa766b4be2f3c9c8ad457fd78..a6ac73d1240bfd44019fbfcc24ca22d4bcb63051 100644 GIT binary patch delta 31 ncmaFL@swjj2osl~u7RO~p`n$5$>unw0!HS