past_binner

Untitled

Apr 2nd, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. ANSIBLE_METADATA = {
  4. 'metadata_version': '1.1',
  5. 'status': ['preview'],
  6. 'supported_by': 'community'
  7. }
  8.  
  9. DOCUMENTATION = '''
  10. ---
  11. module: vm_module
  12.  
  13. short_description: Perform CRUD operation on Catalog
  14.  
  15. version_added: "0.1"
  16.  
  17. description:
  18. - This module will actively managed VDC vm instances. Catalog
  19. can be created and deleted as well as updated.
  20.  
  21. options:
  22. name:
  23. description:
  24. - The name of the Catalog
  25. required: true
  26. publish:
  27. description:
  28. - If true, vm will be shared
  29. required: false
  30.  
  31. extends_documentation_fragment:
  32. - VDC
  33.  
  34. author:
  35. - Prakash Chandra Pandey (@prakashpandey)
  36. '''
  37.  
  38. EXAMPLES = '''
  39. # Pass in a message
  40. - name: Test with a message
  41. my_new_test_module:
  42. name: hello world
  43.  
  44. # pass in a message and have changed true
  45. - name: Test with a message and changed output
  46. my_new_test_module:
  47. name: hello world
  48. new: true
  49.  
  50. # fail the module
  51. - name: Test failure of the module
  52. my_new_test_module:
  53. name: fail me
  54. '''
  55.  
  56. RETURN = '''
  57. original_message:
  58. description: The original name param that was passed in
  59. type: str
  60. message:
  61. description: The output message that the sample module generates
  62. '''
  63.  
  64. from ansible.module_utils.basic import AnsibleModule
  65. from ansible.modules.cloud.xyz.utils import LoggingUtils
  66.  
  67. logger = LoggingUtils(file_name="xyx.log").get_logger()
  68.  
  69.  
  70. def run_module():
  71. # define the available arguments/parameters that a user can pass to
  72. # the module
  73.  
  74. module_args = dict(
  75. name=dict(type='str', required=True),
  76. shared=dict(type='bool', required=False, default=False),
  77. #new=dict(type='bool', required=False, default=False)
  78. )
  79.  
  80.  
  81. for k in module_args:
  82. logger.debug("module_args k: {0}".format(module_args.get(k)))
  83.  
  84. # seed the result dict in the object
  85. # we primarily care about changed and state
  86. # change is if this module effectively modified the target
  87. # state will include any data that you want your module to pass back
  88. # for consumption, for example, in a subsequent task
  89. result = dict(
  90. changed=False,
  91. original_message='',
  92. message=''
  93. )
  94.  
  95. # the AnsibleModule object will be our abstraction working with Ansible
  96. # this includes instantiation, a couple of common attr would be the
  97. # args/params passed to the execution, as well as if the module
  98. # supports check mode
  99. module = AnsibleModule(
  100. argument_spec=module_args,
  101. supports_check_mode=True
  102. )
  103.  
  104. # if the user is working with this module in only check mode we do not
  105. # want to make any changes to the environment, just return the current
  106. # state with no modifications
  107. if module.check_mode:
  108. return result
  109.  
  110. # manipulate or modify the state as needed (this is going to be the
  111. # part where your module will do what it needs to do)
  112. result['original_message'] = module.params['name']
  113. result['message'] = 'goodbye'
  114.  
  115. # use whatever logic you need to determine whether or not this module
  116. # made any modifications to your target
  117. if module.params['new']:
  118. result['changed'] = True
  119.  
  120. # during the execution of the module, if there is an exception or a
  121. # conditional state that effectively causes a failure, run
  122. # AnsibleModule.fail_json() to pass in the message and the result
  123. if module.params['name'] == 'fail me':
  124. module.fail_json(msg='You requested this to fail', **result)
  125.  
  126. # in the event of a successful module execution, you will want to
  127. # simple AnsibleModule.exit_json(), passing the key/value results
  128. module.exit_json(**result)
  129.  
  130. def main():
  131. run_module()
  132.  
  133. if __name__ == '__main__':
  134. main()
Add Comment
Please, Sign In to add comment