feat: add ansible.export_warnings option

This commit is contained in:
egarette@silique.fr 2025-07-04 07:43:31 +03:00
parent 2852475092
commit 03fdeddadb
6 changed files with 176 additions and 22 deletions

View file

@ -5,8 +5,8 @@
msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2025-02-15 12:43+0100\n"
"PO-Revision-Date: 2025-02-15 12:44+0100\n"
"POT-Creation-Date: 2025-07-03 22:11+0300\n"
"PO-Revision-Date: 2025-07-03 22:11+0300\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: fr\n"
@ -14,52 +14,53 @@ msgstr ""
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: pygettext.py 1.5\n"
"X-Generator: Poedit 3.5\n"
"X-Generator: Poedit 3.6\n"
#: src/rougail/output_ansible/__init__.py:57
#: src/rougail/output_ansible/__init__.py:58
msgid "no namespace configured"
msgstr "aucun espace de nom configuré"
#: src/rougail/output_ansible/__init__.py:65
#: src/rougail/output_ansible/__init__.py:67
msgid "cannot find host namespace \"{0}\""
msgstr "ne peut trouve l'espace de nom de l'hôte \"{0}\""
#: src/rougail/output_ansible/__init__.py:70
#, fuzzy
#: src/rougail/output_ansible/__init__.py:74
msgid "malformated host namespace \"{0}\", should have the \"hostnames\" key"
msgstr ""
"l'espace de nom de l'hôte \"{0}\" est mal formaté, devrait avoir la clé "
"\"hostnames\""
#: src/rougail/output_ansible/__init__.py:97
#: src/rougail/output_ansible/__init__.py:113
msgid "cannot find \"hostnames\" in \"{0}\" namespace"
msgstr "ne peut trouve \"hostnames\" dans l'espace de nom \"{0}\""
#: src/rougail/output_ansible/config.py:29
#: src/rougail/output_ansible/config.py:30
msgid "parameter added only to be compatible with Ansible"
msgstr "paramètre ajouté seulement pour être compatible avec Ansible"
#: src/rougail/output_ansible/config.py:52
#: src/rougail/output_ansible/config.py:53
msgid "Configuration of user data or output Ansible"
msgstr "Configuration de Ansible comme donnée utilisateur ou sortie"
#: src/rougail/output_ansible/config.py:62
#: src/rougail/output_ansible/config.py:64
msgid "Configuration of output Ansible"
msgstr "Configuration de la sortie Ansible"
#: src/rougail/output_ansible/config.py:72
#: src/rougail/output_ansible/config.py:76
msgid "Namespace with host values"
msgstr "Espace de nom avec les valeurs de l'hôte"
#: src/rougail/output_ansible/config.py:76
#: src/rougail/output_ansible/config.py:80
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"
#: src/rougail/output_ansible/config.py:77
msgid "All variables is available for all hosts"
msgstr "Tous les variables sont valables pour tous les hôtes"
#: src/rougail/output_ansible/config.py:84
msgid "Displays warnings inside Ansible exportation datas"
msgstr "Affiche les avertissements dans les données d'exportation Ansible"
#~ msgid "All variables is available for all hosts"
#~ msgstr "Tous les variables sont valables pour tous les hôtes"
#~ msgid "The following variables are mandatory but have no value:"
#~ msgstr "Les variables suivantes sont obligatoire mais n'ont pas de valeur :"

View file

@ -5,7 +5,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2025-05-12 08:39+0200\n"
"POT-Creation-Date: 2025-07-03 22:11+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -27,7 +27,7 @@ msgstr ""
msgid "malformated host namespace \"{0}\", should have the \"hostnames\" key"
msgstr ""
#: src/rougail/output_ansible/__init__.py:111
#: src/rougail/output_ansible/__init__.py:113
msgid "cannot find \"hostnames\" in \"{0}\" namespace"
msgstr ""
@ -51,3 +51,7 @@ msgstr ""
msgid "Only variables in host namespace is available by a host"
msgstr ""
#: src/rougail/output_ansible/config.py:84
msgid "Displays warnings inside Ansible exportation datas"
msgstr ""

View file

@ -45,6 +45,7 @@ class RougailOutputAnsible(RougailOutputJson):
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:
super().exporter()
@ -92,10 +93,12 @@ class RougailOutputAnsible(RougailOutputJson):
def json_to_ansible(self):
ret = {"_meta": {"hostvars": {}}, "all": {"children": ["ungrouped"]}}
if "_warnings" in self.dico:
ret["_meta"]["hostvars"]["localhost"] = {
"_warnings": self.dico.pop("_warnings")
}
ret["ungrouped"] = {"hosts": ["localhost"]}
_warnings = self.dico.pop("_warnings")
if self.export_warnings:
ret["_meta"]["hostvars"]["localhost"] = {
"_warnings": _warnings,
}
ret["ungrouped"] = {"hosts": ["localhost"]}
if "_errors" in self.dico:
ret["_meta"]["hostvars"].setdefault("localhost", {})["_errors"] = (
self.dico.pop("_errors")

View file

@ -79,6 +79,10 @@ ansible:
namespace_is_hostname:
description: {_('Only variables in host namespace is available by a host')}
default: false
export_warnings:
description: {_('Displays warnings inside Ansible exportation datas')}
default: true
""",
]
return {

View file

@ -1,7 +1,10 @@
from pytest import fixture # , raises
from pathlib import Path
from yaml import safe_load
from rougail import Rougail
from rougail.output_ansible import RougailOutputAnsible as RougailOutput
from rougail.user_data_yaml import RougailUserDataYaml
from rougail_tests.utils import get_structures_list, get_rougail_config, get_values_for_config
@ -94,3 +97,142 @@ def test_dictionaries_ansible_namespace_mandatory(test_dir):
def test_dictionaries_ansible_namespace_mandatory_read_only(test_dir):
_test_dictionaries(test_dir, True, EXT, read_write=False, mandatory=True)
def test_warnings():
rougailconfig = get_rougail_config(Path('tests/warnings/structures'), True)
##################################
rougailconfig['step.output'] = 'ansible'
rougailconfig['step.user_data'] = ['yaml']
rougailconfig['yaml.filename'] = ['tests/warnings/yaml/config.yml']
extra_dictionaries = rougailconfig["extra_dictionaries"]
extra_dictionaries["hosts"] = [str(Path(__file__).parent / "hosts")]
rougailconfig['extra_dictionaries'] = extra_dictionaries
##################################
rougail = Rougail(rougailconfig)
config = rougail.run()
generated_user_data = RougailUserDataYaml(config, rougailconfig=rougailconfig).run()
err_warn = rougail.user_datas(generated_user_data)
output = RougailOutput(
config=config,
rougailconfig=rougailconfig,
user_data_errors=err_warn["errors"],
user_data_warnings=err_warn["warnings"],
)
ret = output.run()
assert ret[0] is True
assert safe_load(ret[1]) == {
"_meta": {
"hostvars": {
"localhost": {
"_warnings": [
"variable or family \"rougail.an_unknown_var\" does not exist, it will be ignored when loading from the YAML file \"tests/warnings/yaml/config.yml\""
]
},
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"a_var": "a_value"
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"a_var": "a_value"
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"ungrouped": {
"hosts": [
"localhost"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}
def test_no_warnings():
rougailconfig = get_rougail_config(Path('tests/warnings/structures'), True)
##################################
rougailconfig['step.output'] = 'ansible'
rougailconfig['ansible.export_warnings'] = False
rougailconfig['step.user_data'] = ['yaml']
rougailconfig['yaml.filename'] = ['tests/warnings/yaml/config.yml']
extra_dictionaries = rougailconfig["extra_dictionaries"]
extra_dictionaries["hosts"] = [str(Path(__file__).parent / "hosts")]
rougailconfig['extra_dictionaries'] = extra_dictionaries
##################################
rougail = Rougail(rougailconfig)
config = rougail.run()
generated_user_data = RougailUserDataYaml(config, rougailconfig=rougailconfig).run()
err_warn = rougail.user_datas(generated_user_data)
output = RougailOutput(
config=config,
rougailconfig=rougailconfig,
user_data_errors=err_warn["errors"],
user_data_warnings=err_warn["warnings"],
)
ret = output.run()
assert ret[0] is True
assert safe_load(ret[1]) == {
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"a_var": "a_value"
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"a_var": "a_value"
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}