111 lines
3.0 KiB
Python
111 lines
3.0 KiB
Python
|
#!/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
|
||
|
author:
|
||
|
- Cadoles
|
||
|
'''
|
||
|
|
||
|
EXAMPLES = r'''
|
||
|
# Pass in a message
|
||
|
- variables:
|
||
|
- name: simple_variable
|
||
|
value: 'oui'
|
||
|
- name: list_variable
|
||
|
value:
|
||
|
- item1
|
||
|
- item2
|
||
|
- name: master_variable
|
||
|
value:
|
||
|
- item1
|
||
|
- item2
|
||
|
- name: slave_variable
|
||
|
value:
|
||
|
- item1
|
||
|
- item2
|
||
|
'''
|
||
|
|
||
|
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']
|
||
|
'''
|
||
|
|
||
|
from ansible.module_utils.basic import AnsibleModule
|
||
|
from creole.loader import creole_loader, config_save_values
|
||
|
|
||
|
|
||
|
def run_module():
|
||
|
# define available arguments/parameters a user can pass to the module
|
||
|
module_args = dict(
|
||
|
variables=dict(type='list', required=True),
|
||
|
)
|
||
|
|
||
|
# 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=[],
|
||
|
message=''
|
||
|
)
|
||
|
|
||
|
# 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
|
||
|
c = creole_loader(rw=True)
|
||
|
diff = []
|
||
|
for variable in module.params['variables']:
|
||
|
value = variable['value']
|
||
|
var_path = c.find_first(byname=variable['name'], type_='path')
|
||
|
old_value = c.getattr(var_path)
|
||
|
if old_value != value:
|
||
|
diff.append('{}: {} => {}'.format(variable, old_value, value))
|
||
|
c.setattr(var_path, value)
|
||
|
result['diff'] = diff
|
||
|
if not module.check_mode:
|
||
|
config_save_values(c, 'creole')
|
||
|
#if not module.check_mode:
|
||
|
# config_save_values(c, 'creole')
|
||
|
module.exit_json(**result)
|
||
|
|
||
|
|
||
|
def main():
|
||
|
run_module()
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|