Correction de la gestion des multi

This commit is contained in:
Benjamin Bohard 2021-10-26 10:27:41 +02:00
parent 6940cf1db5
commit d6656ab1a9
2 changed files with 94 additions and 16 deletions

View File

@ -150,4 +150,68 @@ creole_loader->tri_variables
tri_variables->e
```
``` yaml
---
- hosts: module_test
tasks:
- name: Configuration dune variable isolée simple
creoleset:
variables:
- name: "libelle_etab"
value: "etab_test"
- name: Configuration dune variable isolée multi
creoleset:
variables:
- name: "adresse_ip_dns"
value:
- "192.168.0.1"
- "192.168.232.2"
- name: Configuration dun groupe de variables
creoleset:
variables:
- name: "ip_ssh_eth0"
value:
- "192.168.0.0"
- name: "netmask_ssh_eth0"
value:
- "255.255.0.0"
- name: Configuration avec variable nécessitant activation
creoleset:
variables:
- name: "additional_repository_name"
value: "Cadoles unstable"
- name: "additional_repository_source"
value: "deb https://vulcain.cadoles.com 2.7.2-unstable main"
- name: "additional_repository_key_type"
value: "URL de la clé"
- name: "additional_repository_key_url"
value: "https://vulcain.cadoles.com/cadoles.gpg"
- name: Configuration ajoutée
creoleset:
variables:
- name: "ip_ssh_eth0"
value: "10.253.30.0"
- name: "netmask_ssh_eth0"
value: "255.255.255.0"
state: present
- name: idempotence
creoleset:
variables:
- name: "ip_ssh_eth0"
value:
- "192.168.0.0"
- "10.253.30.0"
- name: "netmask_ssh_eth0"
value:
- "255.255.0.0"
- "255.255.255.0"
```
### zephir_register
Module basé sur pexpect
Il met en œuvre une série de questions articulées entre elles pour prendre en compte les enchaînements.

View File

@ -119,6 +119,7 @@ def run_module():
new_value = variables[variable]
var_path = d.impl_get_path_by_opt(variable)
old_value = c.getattr(var_path)
new_value_set = {var_path: {'value': new_value, 'sub': [(d.impl_get_path_by_opt(sub), variables[sub]) for sub in sub_variables]}}
if variable.impl_is_multi():
method = module.params['state']
@ -126,16 +127,22 @@ def run_module():
if old_value != new_value:
diff.append('{}: {} => {}'.format(var_path, old_value, new_value))
if sub_variables:
homeconfig, name = c.cfgimpl_get_home_by_path(var_path)
homeconfig.__delattr__(name)
c.cfgimpl_get_settings().remove('validator')
c.setattr(var_path, new_value)
sub_old_values = []
for sub_variable in sub_variables:
sub_value = variables[sub_variable]
sub_var_path = d.impl_get_path_by_opt(sub_variable)
sub_old_value = c.getattr(sub_var_path)
sub_old_values.append(sub_old_value)
if sub_old_value != sub_value:
diff.append('{}: {} => {}'.format(sub_var_path, sub_old_value, sub_value))
homeconfig, name = c.cfgimpl_get_home_by_path(var_path)
homeconfig.__delattr__(name)
c.cfgimpl_get_settings().remove('validator')
c.setattr(var_path, new_value)
for sub_variable, sub_old_value in zip(sub_variables, sub_old_values):
sub_value = variables[sub_variable]
sub_var_path = d.impl_get_path_by_opt(sub_variable)
if sub_old_value != sub_value:
for i in range(len(new_value)):
c.getattr(sub_var_path)[i] = sub_value[i]
c.cfgimpl_get_settings().append('validator')
@ -143,22 +150,29 @@ def run_module():
c.setattr(var_path, new_value)
elif method == 'present':
is_present = False
group_dict = {}
for sub_variable in sub_variables:
group_dict[d.impl_get_path_by_opt(sub_variable)] = variables[sub_variable]
for v_index, value in enumerate(new_value):
for index, item in enumerate(c.getattr(var_path)):
if value == item and all([c.getattr(sv)[index] == group_dict[sv] for sv in group_dict]):
# is master value present in old values?
indexes = {}
for nv in new_value:
indexes[nv] = []
for index, ov in enumerate(old_value):
if nv == ov:
indexes[nv].append(index)
for nv in indexes:
nv_index = new_value_set[var_path]['value'].index(nv)
new_value_subset = {so: sv[nv_index] for so,sv in new_value_set[var_path]['sub']}
is_present = False
for index in indexes[nv]:
old_value_set = {d.impl_get_path_by_opt(sv): c.getattr(d.impl_get_path_by_opt(sv))[index] for sv in sub_variables}
if old_value_set == new_value_subset:
is_present = True
break
if not is_present:
old_value.append(value)
old_value.append(nv)
diff.append('{}: {} => {}'.format(var_path, old_value[:-1], old_value))
for sub_variable, sub_value in group_dict.items():
if not isinstance(sub_value, list):
sub_value = [sub_value]
c.getattr(sub_variable)[-1] = sub_value[v_index]
for sub_variable, sub_value in new_value_set[var_path]['sub']:
c.getattr(sub_variable)[-1] = sub_value[nv_index]
diff.append('{}: {} => {}'.format(sub_variable, c.getattr(sub_variable)[:-1], c.getattr(sub_variable)))
else: