133 lines
3.5 KiB
Python
133 lines
3.5 KiB
Python
#!/usr/bin/python
|
|
|
|
ANSIBLE_METADATA = {
|
|
'metadata_version': '1.1',
|
|
'status': ['preview'],
|
|
'supported_by': 'community'
|
|
}
|
|
|
|
DOCUMENTATION = '''
|
|
---
|
|
module: creole
|
|
|
|
short_description: This is my sample module
|
|
|
|
version_added: "2.4"
|
|
|
|
description:
|
|
- "This is my longer description explaining my sample module"
|
|
|
|
options:
|
|
name:
|
|
description:
|
|
- This is the message to send to the sample module
|
|
required: true
|
|
new:
|
|
description:
|
|
- Control to demo if the result of this module is changed or not
|
|
required: false
|
|
|
|
extends_documentation_fragment:
|
|
- azure
|
|
|
|
author:
|
|
- Your Name (@yourhandle)
|
|
'''
|
|
|
|
EXAMPLES = '''
|
|
# Pass in a message
|
|
- name: Test with a message
|
|
my_new_test_module:
|
|
name: hello world
|
|
|
|
# pass in a message and have changed true
|
|
- name: Test with a message and changed output
|
|
my_new_test_module:
|
|
name: hello world
|
|
new: true
|
|
|
|
# fail the module
|
|
- name: Test failure of the module
|
|
my_new_test_module:
|
|
name: fail me
|
|
'''
|
|
|
|
RETURN = '''
|
|
original_message:
|
|
description: The original name param that was passed in
|
|
type: str
|
|
message:
|
|
description: The output message that the sample module generates
|
|
'''
|
|
|
|
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(
|
|
name=dict(type='str', required=True),
|
|
value=dict(type='json', required=False),
|
|
load_extra=dict(type='bool', required=False, default=True),
|
|
check_mandatory=dict(type='bool', required=False, default=False),
|
|
reload_config=dict(type='bool', required=False, default=False),
|
|
)
|
|
|
|
result = dict(
|
|
changed=False,
|
|
original_message='',
|
|
message=''
|
|
)
|
|
|
|
module = AnsibleModule(
|
|
argument_spec=module_args,
|
|
supports_check_mode=True
|
|
)
|
|
|
|
update_value = not module.params['value'] is None
|
|
c = creole_loader(rw=update_value, load_extra=module.params['load_extra'])
|
|
|
|
path = c.creole.find_first(byname=module.params['name'], type_='path')
|
|
value = getattr(c, path)
|
|
|
|
result['state'] = dict(
|
|
name=module.params['name'],
|
|
value=value
|
|
)
|
|
|
|
if update_value:
|
|
setattr(c, path, module.params['value'])
|
|
if not module.check_mode:
|
|
try:
|
|
config_save_values(
|
|
c, 'creole',
|
|
check_mandatory=module.params['check_mandatory'],
|
|
reload_config=module.params['reload_config']
|
|
)
|
|
result['changed'] = True
|
|
except e:
|
|
module.fail_json(msg=str(e), **result)
|
|
else:
|
|
value = getattr(c, path)
|
|
|
|
# use whatever logic you need to determine whether or not this module
|
|
# made any modifications to your target
|
|
# if module.params['new']:
|
|
# result['changed'] = True
|
|
|
|
# during the execution of the module, if there is an exception or a
|
|
# conditional state that effectively causes a failure, run
|
|
# AnsibleModule.fail_json() to pass in the message and the result
|
|
# if module.params['name'] == 'fail me':
|
|
# module.fail_json(msg='You requested this to fail', **result)
|
|
|
|
# in the event of a successful module execution, you will want to
|
|
# simple AnsibleModule.exit_json(), passing the key/value results
|
|
module.exit_json(**result)
|
|
|
|
def main():
|
|
run_module()
|
|
|
|
if __name__ == '__main__':
|
|
main() |