feat: add vars for an specified host
This commit is contained in:
parent
deefccbd4c
commit
66a98e633a
6 changed files with 160 additions and 3 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
"""
|
"""
|
||||||
Silique (https://www.silique.fr)
|
Silique (https://www.silique.fr)
|
||||||
Copyright (C) 2022-2025
|
Copyright (C) 2022-2026
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify it
|
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
|
under the terms of the GNU Lesser General Public License as published by the
|
||||||
|
|
@ -91,6 +91,7 @@ class RougailOutputAnsible(RougailOutputJson):
|
||||||
def json_to_ansible(self):
|
def json_to_ansible(self):
|
||||||
ret = {"_meta": {"hostvars": {}}}
|
ret = {"_meta": {"hostvars": {}}}
|
||||||
namespaces = {}
|
namespaces = {}
|
||||||
|
extra_vars = {}
|
||||||
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:
|
||||||
|
|
@ -123,6 +124,10 @@ class RougailOutputAnsible(RougailOutputJson):
|
||||||
ret_hosts = {}
|
ret_hosts = {}
|
||||||
for name, hosts in hostnames.items():
|
for name, hosts in hostnames.items():
|
||||||
if "hosts" in hosts:
|
if "hosts" in hosts:
|
||||||
|
if "vars" in hosts and "all" in hosts["vars"]:
|
||||||
|
current_extra_vars = hosts["vars"]["all"]
|
||||||
|
else:
|
||||||
|
current_extra_vars = {}
|
||||||
for idx, host in enumerate(hosts["hosts"]):
|
for idx, host in enumerate(hosts["hosts"]):
|
||||||
index = str(idx + 1)
|
index = str(idx + 1)
|
||||||
if idx < 9:
|
if idx < 9:
|
||||||
|
|
@ -137,6 +142,11 @@ class RougailOutputAnsible(RougailOutputJson):
|
||||||
ret.setdefault(name, {}).setdefault("hosts", []).append(
|
ret.setdefault(name, {}).setdefault("hosts", []).append(
|
||||||
host_name
|
host_name
|
||||||
)
|
)
|
||||||
|
if "vars" in hosts and str(idx) in hosts["vars"]:
|
||||||
|
extra_vars[host_name] = current_extra_vars.copy()
|
||||||
|
extra_vars[host_name].update(hosts["vars"][str(idx)])
|
||||||
|
elif current_extra_vars:
|
||||||
|
extra_vars[host_name] = current_extra_vars
|
||||||
else:
|
else:
|
||||||
ret[name] = hosts
|
ret[name] = hosts
|
||||||
# manage hostsnames and vars in hostsname
|
# manage hostsnames and vars in hostsname
|
||||||
|
|
@ -158,6 +168,8 @@ class RougailOutputAnsible(RougailOutputJson):
|
||||||
ret["_meta"]["hostvars"][host][namespace] = self.dico[namespace]
|
ret["_meta"]["hostvars"][host][namespace] = self.dico[namespace]
|
||||||
else:
|
else:
|
||||||
ret["_meta"]["hostvars"][host].update(self.dico)
|
ret["_meta"]["hostvars"][host].update(self.dico)
|
||||||
|
if host in extra_vars:
|
||||||
|
ret["_meta"]["hostvars"][host].update(extra_vars[host])
|
||||||
# manage hostnames define with tag ansible_host and add groups
|
# manage hostnames define with tag ansible_host and add groups
|
||||||
for namespace, hosts in self.hosts.items():
|
for namespace, hosts in self.hosts.items():
|
||||||
if namespace not in ret:
|
if namespace not in ret:
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
"""
|
"""
|
||||||
Silique (https://www.silique.fr)
|
Silique (https://www.silique.fr)
|
||||||
Copyright (C) 2024-2025
|
Copyright (C) 2024-2026
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify it
|
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
|
under the terms of the GNU Lesser General Public License as published by the
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
"""Internationalisation utilities
|
"""Internationalisation utilities
|
||||||
Silique (https://www.silique.fr)
|
Silique (https://www.silique.fr)
|
||||||
Copyright (C) 2024-2025
|
Copyright (C) 2024-2026
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify it
|
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
|
under the terms of the GNU Lesser General Public License as published by the
|
||||||
|
|
|
||||||
50
tests/hosts-with-vars/00-ip.yml
Normal file
50
tests/hosts-with-vars/00-ip.yml
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
---
|
||||||
|
version: "1.1"
|
||||||
|
|
||||||
|
hostnames:
|
||||||
|
|
||||||
|
group1:
|
||||||
|
|
||||||
|
hosts:
|
||||||
|
type: domainname
|
||||||
|
multi: true
|
||||||
|
default:
|
||||||
|
- group1.net
|
||||||
|
|
||||||
|
prefix_name:
|
||||||
|
default: GROUP1_
|
||||||
|
|
||||||
|
group2:
|
||||||
|
|
||||||
|
hosts:
|
||||||
|
type: domainname
|
||||||
|
multi: true
|
||||||
|
default:
|
||||||
|
- group2.net
|
||||||
|
- group3.net
|
||||||
|
|
||||||
|
prefix_name:
|
||||||
|
default: GROUP2_
|
||||||
|
|
||||||
|
vars:
|
||||||
|
|
||||||
|
all:
|
||||||
|
|
||||||
|
added_var1: true
|
||||||
|
|
||||||
|
a_family:
|
||||||
|
|
||||||
|
with_a_variable: 1
|
||||||
|
|
||||||
|
"0":
|
||||||
|
|
||||||
|
added_var2: true
|
||||||
|
|
||||||
|
group3:
|
||||||
|
|
||||||
|
hosts:
|
||||||
|
type: domainname
|
||||||
|
multi: true
|
||||||
|
default:
|
||||||
|
- group4.net
|
||||||
|
- group5.net
|
||||||
11
tests/hosts-with-vars/10-group.yml
Normal file
11
tests/hosts-with-vars/10-group.yml
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
---
|
||||||
|
version: "1.1"
|
||||||
|
|
||||||
|
hostnames:
|
||||||
|
|
||||||
|
groups:
|
||||||
|
children:
|
||||||
|
multi: true
|
||||||
|
default:
|
||||||
|
- group1
|
||||||
|
- group2
|
||||||
|
|
@ -280,3 +280,87 @@ def test_no_warnings():
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_host_with_vars():
|
||||||
|
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_namespaces = rougailconfig["extra_namespaces"]
|
||||||
|
extra_namespaces["hosts"] = [str(Path(__file__).parent / "hosts-with-vars")]
|
||||||
|
rougailconfig['extra_namespaces'] = extra_namespaces
|
||||||
|
##################################
|
||||||
|
rougail = Rougail(rougailconfig)
|
||||||
|
config = rougail.run()
|
||||||
|
generated_user_data = RougailUserDataYaml(config, rougailconfig=rougailconfig).run()
|
||||||
|
err_warn = rougail.user_data(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',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'GROUP2_02': {
|
||||||
|
'ansible_host': 'group3.net',
|
||||||
|
'rougail': {
|
||||||
|
'a_var': 'a_value',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'group4.net': {
|
||||||
|
'ansible_host': 'group4.net',
|
||||||
|
'rougail': {
|
||||||
|
'a_var': 'a_value',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'group5.net': {
|
||||||
|
'ansible_host': 'group5.net',
|
||||||
|
"rougail": {
|
||||||
|
"a_var": "a_value"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'group1': {
|
||||||
|
'hosts': [
|
||||||
|
'GROUP1_01',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
'group2': {
|
||||||
|
'hosts': [
|
||||||
|
'GROUP2_01',
|
||||||
|
'GROUP2_02',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
'group3': {
|
||||||
|
'hosts': [
|
||||||
|
'group4.net',
|
||||||
|
'group5.net',
|
||||||
|
],
|
||||||
|
},
|
||||||
|
'groups': {
|
||||||
|
'children': [
|
||||||
|
'group1',
|
||||||
|
'group2',
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue