feat: add namespace_is_hostname option

This commit is contained in:
egarette@silique.fr 2025-01-02 21:38:17 +01:00
parent 82b4a29b33
commit 773f991274
30 changed files with 1190 additions and 46 deletions

View file

@ -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 :"

View file

@ -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 <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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 ""

View file

@ -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,10 +101,19 @@ 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
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)
@ -112,6 +123,11 @@ class RougailOutputAnsible(RougailOutputJson):
for hosts in ret_hosts.values():
for host, domain_name in hosts.items():
ret['_meta']['hostvars'][host] = {'ansible_host': domain_name}
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

View file

@ -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",

View file

@ -5,19 +5,23 @@ hostnames:
hidden: true
group1:
hosts:
type: domainname
multi: true
default:
- group1.net
prefix_name:
default: GROUP1_
group2:
hosts:
type: domainname
multi: true
default:
- group2.net
prefix_name:
default: GROUP2_

View file

@ -0,0 +1,42 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"var1": "string1",
"var2": "string1"
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"var1": "string1",
"var2": "string1"
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,46 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"variable": "string1"
},
"extra": {
"variable": "string1"
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"variable": "string1"
},
"extra": {
"variable": "string1"
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -14,7 +14,11 @@
"string2",
"string3"
],
"var3": "string1",
"var3": [
"string1",
"string2",
"string3"
],
"var4": [
"string1",
"string2",
@ -55,7 +59,11 @@
"string2",
"string3"
],
"var3": "string1",
"var3": [
"string1",
"string2",
"string3"
],
"var4": [
"string1",
"string2",

View file

@ -0,0 +1,36 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,134 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"var": [
"val1",
"val2"
],
"dynval1": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
},
"dynval2": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
}
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"var": [
"val1",
"val2"
],
"dynval1": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
},
"dynval2": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
}
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,42 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"var1": "string1",
"var2": "string1"
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"var1": "string1",
"var2": "string1"
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,46 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"variable": "string1"
},
"extra": {
"variable": "string1"
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"variable": "string1"
},
"extra": {
"variable": "string1"
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -14,7 +14,11 @@
"string2",
"string3"
],
"var3": "string1",
"var3": [
"string1",
"string2",
"string3"
],
"var4": [
"string1",
"string2",
@ -55,7 +59,11 @@
"string2",
"string3"
],
"var3": "string1",
"var3": [
"string1",
"string2",
"string3"
],
"var4": [
"string1",
"string2",

View file

@ -0,0 +1,36 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,134 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"var": [
"val1",
"val2"
],
"dynval1": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
},
"dynval2": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
}
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"var": [
"val1",
"val2"
],
"dynval1": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
},
"dynval2": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
}
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,42 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"var1": null,
"var2": null
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"var1": null,
"var2": null
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,46 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"variable": "value in extra"
},
"extra": {
"variable": "value in extra"
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"variable": "value in extra"
},
"extra": {
"variable": "value in extra"
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -6,7 +6,7 @@
"rougail": {
"var1": [],
"var2": [],
"var3": null,
"var3": [],
"var4": [
"value"
],
@ -29,7 +29,7 @@
"rougail": {
"var1": [],
"var2": [],
"var3": null,
"var3": [],
"var4": [
"value"
],

View file

@ -0,0 +1,36 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,58 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"var": [
"val1",
"val2"
],
"dynval1": {
"var": []
},
"dynval2": {
"var": []
}
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"var": [
"val1",
"val2"
],
"dynval1": {
"var": []
},
"dynval2": {
"var": []
}
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,42 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"var1": "string1",
"var2": "string1"
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"var1": "string1",
"var2": "string1"
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,46 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"variable": "string1"
},
"extra": {
"variable": "string1"
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"variable": "string1"
},
"extra": {
"variable": "string1"
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -14,7 +14,11 @@
"string2",
"string3"
],
"var3": "string1",
"var3": [
"string1",
"string2",
"string3"
],
"var4": [
"string1",
"string2",
@ -55,7 +59,11 @@
"string2",
"string3"
],
"var3": "string1",
"var3": [
"string1",
"string2",
"string3"
],
"var4": [
"string1",
"string2",

View file

@ -0,0 +1,36 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,134 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"var": [
"val1",
"val2"
],
"dynval1": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
},
"dynval2": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
}
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"var": [
"val1",
"val2"
],
"dynval1": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
},
"dynval2": {
"var": [
"string1",
"string2",
"string3"
],
"dyn_string1": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string2": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
},
"dyn_string3": {
"var": "string1",
"var_identifier": "string1",
"var_identifiers": "string1"
}
}
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,23 @@
{
"_meta": {
"hostvars": {
"localhost": {
"_errors": [
"The following variables are mandatory but have no value:",
" - rougail.var1 (a first variable)",
" - rougail.var2 (a second variable)"
]
}
}
},
"all": {
"children": [
"ungrouped"
]
},
"ungrouped": {
"hosts": [
"localhost"
]
}
}

View file

@ -0,0 +1,46 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"variable": "value in extra"
},
"extra": {
"variable": "value in extra"
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"variable": "value in extra"
},
"extra": {
"variable": "value in extra"
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,36 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}

View file

@ -0,0 +1,58 @@
{
"_meta": {
"hostvars": {
"GROUP1_01": {
"ansible_host": "group1.net",
"rougail": {
"var": [
"val1",
"val2"
],
"dynval1": {
"var": []
},
"dynval2": {
"var": []
}
}
},
"GROUP2_01": {
"ansible_host": "group2.net",
"rougail": {
"var": [
"val1",
"val2"
],
"dynval1": {
"var": []
},
"dynval2": {
"var": []
}
}
}
}
},
"all": {
"children": [
"ungrouped",
"groups"
]
},
"group1": {
"hosts": [
"GROUP1_01"
]
},
"group2": {
"hosts": [
"GROUP2_01"
]
},
"groups": {
"children": [
"group1",
"group2"
]
}
}