2021-06-21 15:51:07 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# Copyright: (c) 2018, Terry Jones <terry.jones@example.org>
|
|
|
|
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = r'''
|
|
|
|
---
|
|
|
|
module: CreoleSet
|
|
|
|
|
|
|
|
short_description: This module aims to set variables on EOLE modules
|
|
|
|
|
|
|
|
version_added: "1.0.0"
|
|
|
|
|
|
|
|
description: This module aims to set variables on EOLE modules orderly and atomicly.
|
|
|
|
|
|
|
|
options:
|
|
|
|
variables:
|
|
|
|
description: Name of the variable to change.
|
|
|
|
required: true
|
|
|
|
type: list
|
2021-08-30 10:06:42 +02:00
|
|
|
state:
|
|
|
|
description: state of variables
|
|
|
|
required: false
|
|
|
|
type: str
|
2021-06-21 15:51:07 +02:00
|
|
|
author:
|
|
|
|
- Cadoles
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = r'''
|
2021-08-30 10:06:42 +02:00
|
|
|
variables:
|
|
|
|
- name: activer_ajout_hosts
|
2021-06-21 15:51:07 +02:00
|
|
|
value: 'oui'
|
2021-08-30 10:06:42 +02:00
|
|
|
- name: adresse_ip_hosts
|
2021-06-21 15:51:07 +02:00
|
|
|
value:
|
2021-08-30 10:06:42 +02:00
|
|
|
- 192.168.1.1
|
|
|
|
- name: nom_long_hosts
|
2021-06-21 15:51:07 +02:00
|
|
|
value:
|
2021-08-30 10:06:42 +02:00
|
|
|
- "seth.domain.org cloud.domain.org"
|
|
|
|
state: exact
|
2021-06-21 15:51:07 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = r'''
|
|
|
|
# These are examples of possible return values, and in general should use other names for return values.
|
|
|
|
diff:
|
|
|
|
description: variable modifications status.
|
|
|
|
type: list
|
|
|
|
returned: always
|
|
|
|
sample: ['activer_ajout_hosts: non => oui']
|
|
|
|
'''
|
|
|
|
|
2021-10-26 11:31:21 +02:00
|
|
|
import sys
|
2021-06-21 15:51:07 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
from creole.loader import creole_loader, config_save_values
|
|
|
|
|
2021-09-29 10:45:20 +02:00
|
|
|
def process_value(variable, value):
|
|
|
|
if variable.impl_is_multi():
|
|
|
|
if not isinstance(value, list):
|
|
|
|
return [value]
|
|
|
|
else:
|
|
|
|
if isinstance(value, list):
|
|
|
|
raise Exception('Variable {} is not multi'.format(variable))
|
|
|
|
return value
|
|
|
|
|
2021-10-26 11:31:21 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2021-06-21 15:51:07 +02:00
|
|
|
|
|
|
|
def run_module():
|
|
|
|
# define available arguments/parameters a user can pass to the module
|
|
|
|
module_args = dict(
|
|
|
|
variables=dict(type='list', required=True),
|
2021-08-30 10:06:42 +02:00
|
|
|
state=dict(type='str', required=False, default='exact'),
|
2021-06-21 15:51:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# seed the result dict in the object
|
|
|
|
# we primarily care about changed and state
|
|
|
|
# changed is if this module effectively modified the target
|
|
|
|
# state will include any data that you want your module to pass back
|
|
|
|
# for consumption, for example, in a subsequent task
|
|
|
|
result = dict(
|
|
|
|
changed=False,
|
|
|
|
diff=[],
|
2021-08-30 10:06:42 +02:00
|
|
|
debug=[],
|
2021-06-21 15:51:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# the AnsibleModule object will be our abstraction working with Ansible
|
|
|
|
# this includes instantiation, a couple of common attr would be the
|
|
|
|
# args/params passed to the execution, as well as if the module
|
|
|
|
# supports check mode
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=module_args,
|
|
|
|
supports_check_mode=True
|
|
|
|
)
|
|
|
|
|
|
|
|
# if the user is working with this module in only check mode we do not
|
|
|
|
# want to make any changes to the environment, just return the current
|
|
|
|
# state with no modifications
|
2021-08-30 10:06:42 +02:00
|
|
|
c = creole_loader(rw=True, reload_config=True)
|
|
|
|
d = c.cfgimpl_get_description()
|
2021-06-21 15:51:07 +02:00
|
|
|
diff = []
|
2021-08-30 10:06:42 +02:00
|
|
|
c.cfgimpl_get_settings().remove('disabled')
|
2021-10-26 11:31:21 +02:00
|
|
|
if sys.version_info < (3,):
|
|
|
|
module.params['variables'] = yml_params_to_unicode(module.params['variables'])
|
2021-08-30 10:06:42 +02:00
|
|
|
|
2021-09-29 10:45:20 +02:00
|
|
|
variables = {}
|
|
|
|
for variable in module.params['variables']:
|
|
|
|
creole_variable = c.find_first(byname=variable['name'])
|
|
|
|
variables[creole_variable] = process_value(creole_variable, variable['value'])
|
2021-08-30 10:06:42 +02:00
|
|
|
|
|
|
|
slaves = [v for v in variables if v.impl_is_master_slaves('slave')]
|
|
|
|
|
|
|
|
masters = {v: [] for v in variables if v.impl_is_master_slaves('master')}
|
|
|
|
|
|
|
|
for slave in slaves:
|
|
|
|
master = slave.impl_get_master_slaves().getmaster(slave)
|
|
|
|
masters[master].append(slave)
|
|
|
|
|
|
|
|
ordered_variables = [(v, []) for v in variables if not v.impl_is_master_slaves()]
|
|
|
|
|
|
|
|
ordered_variables.extend(masters.items())
|
|
|
|
|
|
|
|
for variable, sub_variables in ordered_variables:
|
2021-09-29 10:45:20 +02:00
|
|
|
new_value = variables[variable]
|
2021-08-30 10:06:42 +02:00
|
|
|
var_path = d.impl_get_path_by_opt(variable)
|
2021-06-21 15:51:07 +02:00
|
|
|
old_value = c.getattr(var_path)
|
2021-10-26 10:27:41 +02:00
|
|
|
new_value_set = {var_path: {'value': new_value, 'sub': [(d.impl_get_path_by_opt(sub), variables[sub]) for sub in sub_variables]}}
|
2021-09-29 10:45:20 +02:00
|
|
|
|
|
|
|
if variable.impl_is_multi():
|
2021-08-30 10:06:42 +02:00
|
|
|
method = module.params['state']
|
2021-09-29 10:45:20 +02:00
|
|
|
if method == 'exact':
|
|
|
|
if old_value != new_value:
|
|
|
|
diff.append('{}: {} => {}'.format(var_path, old_value, new_value))
|
|
|
|
if sub_variables:
|
2021-10-26 10:27:41 +02:00
|
|
|
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))
|
2021-09-30 09:17:27 +02:00
|
|
|
homeconfig, name = c.cfgimpl_get_home_by_path(var_path)
|
|
|
|
homeconfig.__delattr__(name)
|
2021-09-29 10:45:20 +02:00
|
|
|
c.cfgimpl_get_settings().remove('validator')
|
|
|
|
c.setattr(var_path, new_value)
|
2021-10-26 10:27:41 +02:00
|
|
|
for sub_variable, sub_old_value in zip(sub_variables, sub_old_values):
|
2021-09-29 10:45:20 +02:00
|
|
|
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')
|
|
|
|
else:
|
|
|
|
c.setattr(var_path, new_value)
|
|
|
|
|
|
|
|
elif method == 'present':
|
2021-10-26 10:27:41 +02:00
|
|
|
# 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:
|
2021-09-29 10:45:20 +02:00
|
|
|
is_present = True
|
|
|
|
break
|
2021-10-26 10:27:41 +02:00
|
|
|
|
2021-09-29 10:45:20 +02:00
|
|
|
if not is_present:
|
2021-10-26 10:27:41 +02:00
|
|
|
old_value.append(nv)
|
2021-09-29 10:45:20 +02:00
|
|
|
diff.append('{}: {} => {}'.format(var_path, old_value[:-1], old_value))
|
2021-10-26 10:27:41 +02:00
|
|
|
for sub_variable, sub_value in new_value_set[var_path]['sub']:
|
|
|
|
c.getattr(sub_variable)[-1] = sub_value[nv_index]
|
2021-09-29 10:45:20 +02:00
|
|
|
diff.append('{}: {} => {}'.format(sub_variable, c.getattr(sub_variable)[:-1], c.getattr(sub_variable)))
|
2021-08-30 10:06:42 +02:00
|
|
|
|
2021-09-29 10:45:20 +02:00
|
|
|
else:
|
|
|
|
if old_value != new_value:
|
|
|
|
diff.append('{}: {} => {}'.format(var_path, old_value, new_value))
|
|
|
|
c.setattr(var_path, new_value)
|
2021-08-30 10:06:42 +02:00
|
|
|
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)
|
|
|
|
if sub_old_value != sub_value:
|
|
|
|
diff.append('{}: {} => {}'.format(sub_var_path, sub_old_value, sub_value))
|
2021-09-29 10:45:20 +02:00
|
|
|
c.setattr(sub_var_path, sub_value)
|
2021-08-30 10:06:42 +02:00
|
|
|
|
|
|
|
|
2021-06-21 15:51:07 +02:00
|
|
|
result['diff'] = diff
|
2021-08-30 10:06:42 +02:00
|
|
|
c.cfgimpl_get_settings().append('disabled')
|
2021-09-30 09:17:27 +02:00
|
|
|
c.make_dict()
|
2021-08-30 10:06:42 +02:00
|
|
|
|
|
|
|
if not module.check_mode and diff:
|
2021-06-21 15:51:07 +02:00
|
|
|
config_save_values(c, 'creole')
|
2021-08-30 10:06:42 +02:00
|
|
|
result['changed'] = True
|
|
|
|
|
2021-06-21 15:51:07 +02:00
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
run_module()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|