Compare commits
2 Commits
6940cf1db5
...
53f82ae1f1
Author | SHA1 | Date | |
---|---|---|---|
53f82ae1f1 | |||
d6656ab1a9 |
64
README.md
64
README.md
@ -150,4 +150,68 @@ creole_loader->tri_variables
|
|||||||
tri_variables->e
|
tri_variables->e
|
||||||
```
|
```
|
||||||
|
|
||||||
|
``` yaml
|
||||||
|
---
|
||||||
|
- hosts: module_test
|
||||||
|
tasks:
|
||||||
|
- name: Configuration d’une variable isolée simple
|
||||||
|
creoleset:
|
||||||
|
variables:
|
||||||
|
- name: "libelle_etab"
|
||||||
|
value: "etab_test"
|
||||||
|
|
||||||
|
- name: Configuration d’une variable isolée multi
|
||||||
|
creoleset:
|
||||||
|
variables:
|
||||||
|
- name: "adresse_ip_dns"
|
||||||
|
value:
|
||||||
|
- "192.168.0.1"
|
||||||
|
- "192.168.232.2"
|
||||||
|
|
||||||
|
- name: Configuration d’un 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
|
### 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.
|
||||||
|
@ -50,6 +50,7 @@ diff:
|
|||||||
sample: ['activer_ajout_hosts: non => oui']
|
sample: ['activer_ajout_hosts: non => oui']
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
import sys
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from creole.loader import creole_loader, config_save_values
|
from creole.loader import creole_loader, config_save_values
|
||||||
|
|
||||||
@ -62,6 +63,18 @@ def process_value(variable, value):
|
|||||||
raise Exception('Variable {} is not multi'.format(variable))
|
raise Exception('Variable {} is not multi'.format(variable))
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def yml_params_to_unicode(param):
|
||||||
|
def convert_param(param):
|
||||||
|
if isinstance(param, str):
|
||||||
|
return param.decode('utf-8')
|
||||||
|
if isinstance(param, list):
|
||||||
|
return [convert_param(p) for p in param]
|
||||||
|
if isinstance(param, dict):
|
||||||
|
return {convert_param(key): convert_param(value) for key,value in param.items()}
|
||||||
|
return param
|
||||||
|
return convert_param(param)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def run_module():
|
def run_module():
|
||||||
# define available arguments/parameters a user can pass to the module
|
# define available arguments/parameters a user can pass to the module
|
||||||
@ -97,6 +110,8 @@ def run_module():
|
|||||||
d = c.cfgimpl_get_description()
|
d = c.cfgimpl_get_description()
|
||||||
diff = []
|
diff = []
|
||||||
c.cfgimpl_get_settings().remove('disabled')
|
c.cfgimpl_get_settings().remove('disabled')
|
||||||
|
if sys.version_info < (3,):
|
||||||
|
module.params['variables'] = yml_params_to_unicode(module.params['variables'])
|
||||||
|
|
||||||
variables = {}
|
variables = {}
|
||||||
for variable in module.params['variables']:
|
for variable in module.params['variables']:
|
||||||
@ -119,6 +134,7 @@ def run_module():
|
|||||||
new_value = variables[variable]
|
new_value = variables[variable]
|
||||||
var_path = d.impl_get_path_by_opt(variable)
|
var_path = d.impl_get_path_by_opt(variable)
|
||||||
old_value = c.getattr(var_path)
|
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():
|
if variable.impl_is_multi():
|
||||||
method = module.params['state']
|
method = module.params['state']
|
||||||
@ -126,16 +142,22 @@ def run_module():
|
|||||||
if old_value != new_value:
|
if old_value != new_value:
|
||||||
diff.append('{}: {} => {}'.format(var_path, old_value, new_value))
|
diff.append('{}: {} => {}'.format(var_path, old_value, new_value))
|
||||||
if sub_variables:
|
if sub_variables:
|
||||||
homeconfig, name = c.cfgimpl_get_home_by_path(var_path)
|
sub_old_values = []
|
||||||
homeconfig.__delattr__(name)
|
|
||||||
c.cfgimpl_get_settings().remove('validator')
|
|
||||||
c.setattr(var_path, new_value)
|
|
||||||
for sub_variable in sub_variables:
|
for sub_variable in sub_variables:
|
||||||
sub_value = variables[sub_variable]
|
sub_value = variables[sub_variable]
|
||||||
sub_var_path = d.impl_get_path_by_opt(sub_variable)
|
sub_var_path = d.impl_get_path_by_opt(sub_variable)
|
||||||
sub_old_value = c.getattr(sub_var_path)
|
sub_old_value = c.getattr(sub_var_path)
|
||||||
|
sub_old_values.append(sub_old_value)
|
||||||
if sub_old_value != sub_value:
|
if sub_old_value != sub_value:
|
||||||
diff.append('{}: {} => {}'.format(sub_var_path, 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)):
|
for i in range(len(new_value)):
|
||||||
c.getattr(sub_var_path)[i] = sub_value[i]
|
c.getattr(sub_var_path)[i] = sub_value[i]
|
||||||
c.cfgimpl_get_settings().append('validator')
|
c.cfgimpl_get_settings().append('validator')
|
||||||
@ -143,22 +165,29 @@ def run_module():
|
|||||||
c.setattr(var_path, new_value)
|
c.setattr(var_path, new_value)
|
||||||
|
|
||||||
elif method == 'present':
|
elif method == 'present':
|
||||||
|
# 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
|
is_present = False
|
||||||
group_dict = {}
|
for index in indexes[nv]:
|
||||||
for sub_variable in sub_variables:
|
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}
|
||||||
group_dict[d.impl_get_path_by_opt(sub_variable)] = variables[sub_variable]
|
if old_value_set == new_value_subset:
|
||||||
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_present = True
|
is_present = True
|
||||||
break
|
break
|
||||||
|
|
||||||
if not is_present:
|
if not is_present:
|
||||||
old_value.append(value)
|
old_value.append(nv)
|
||||||
diff.append('{}: {} => {}'.format(var_path, old_value[:-1], old_value))
|
diff.append('{}: {} => {}'.format(var_path, old_value[:-1], old_value))
|
||||||
for sub_variable, sub_value in group_dict.items():
|
for sub_variable, sub_value in new_value_set[var_path]['sub']:
|
||||||
if not isinstance(sub_value, list):
|
c.getattr(sub_variable)[-1] = sub_value[nv_index]
|
||||||
sub_value = [sub_value]
|
|
||||||
c.getattr(sub_variable)[-1] = sub_value[v_index]
|
|
||||||
diff.append('{}: {} => {}'.format(sub_variable, c.getattr(sub_variable)[:-1], c.getattr(sub_variable)))
|
diff.append('{}: {} => {}'.format(sub_variable, c.getattr(sub_variable)[:-1], c.getattr(sub_variable)))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
Reference in New Issue
Block a user