Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- ANSIBLE_METADATA = {
- 'metadata_version': '1.1',
- 'status': ['preview'],
- 'supported_by': 'community'
- }
- DOCUMENTATION = '''
- ---
- module: vm_module
- short_description: Perform CRUD operation on Catalog
- version_added: "0.1"
- description:
- - This module will actively managed VDC vm instances. Catalog
- can be created and deleted as well as updated.
- options:
- name:
- description:
- - The name of the Catalog
- required: true
- publish:
- description:
- - If true, vm will be shared
- required: false
- extends_documentation_fragment:
- - VDC
- author:
- - Prakash Chandra Pandey (@prakashpandey)
- '''
- 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 ansible.modules.cloud.xyz.utils import LoggingUtils
- logger = LoggingUtils(file_name="xyx.log").get_logger()
- def run_module():
- # define the available arguments/parameters that a user can pass to
- # the module
- module_args = dict(
- name=dict(type='str', required=True),
- shared=dict(type='bool', required=False, default=False),
- #new=dict(type='bool', required=False, default=False)
- )
- for k in module_args:
- logger.debug("module_args k: {0}".format(module_args.get(k)))
- # seed the result dict in the object
- # we primarily care about changed and state
- # change 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,
- original_message='',
- 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
- if module.check_mode:
- return result
- # manipulate or modify the state as needed (this is going to be the
- # part where your module will do what it needs to do)
- result['original_message'] = module.params['name']
- result['message'] = 'goodbye'
- # 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()
Add Comment
Please, Sign In to add comment