hello_world.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. # Copyright: (c) 2018, Terry Jones <terry.jones@example.org>
  3. # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
  4. from __future__ import (absolute_import, division, print_function)
  5. __metaclass__ = type
  6. DOCUMENTATION = r'''
  7. ---
  8. module: hello_world
  9. short_description: This is my first module
  10. # If this is part of a collection, you need to use semantic versioning,
  11. # i.e. the version is of the form "2.5.0" and not "2.4".
  12. version_added: "1.0.0"
  13. description: This is my longer description explaining my test module.
  14. options:
  15. name:
  16. description: It is the name to display.
  17. required: true
  18. type: str
  19. # Specify this value according to your collection
  20. # in format of namespace.collection.doc_fragment_name
  21. # extends_documentation_fragment:
  22. # - my_namespace.my_collection.my_doc_fragment_name
  23. author:
  24. - Your Name (@yourGitHubHandle)
  25. '''
  26. EXAMPLES = r'''
  27. # Pass name to the module
  28. - name: Hello Steph
  29. my_namespace.my_collection.hello_world:
  30. name: Steph
  31. '''
  32. RETURN = r'''
  33. # These are examples of possible return values, and in general should use other names for return values.
  34. message:
  35. description: The output message that the test module generates.
  36. type: str
  37. returned: always
  38. sample: 'Hello World Stephane'
  39. '''
  40. from ansible.module_utils.basic import AnsibleModule
  41. def main():
  42. # define available arguments/parameters a user can pass to the module
  43. module_args = dict(
  44. name=dict(type='str', required=True),
  45. )
  46. # seed the result dict in the object
  47. # we primarily care about changed and state
  48. # changed is if this module effectively modified the target
  49. # state will include any data that you want your module to pass back
  50. # for consumption, for example, in a subsequent task
  51. result = dict(
  52. changed=False,
  53. message=''
  54. )
  55. # the AnsibleModule object will be our abstraction working with Ansible
  56. # this includes instantiation, a couple of common attr would be the
  57. # args/params passed to the execution, as well as if the module
  58. # supports check mode
  59. module = AnsibleModule(
  60. argument_spec=module_args,
  61. supports_check_mode=True
  62. )
  63. # if the user is working with this module in only check mode we do not
  64. # want to make any changes to the environment, just return the current
  65. # state with no modifications
  66. if module.check_mode:
  67. module.exit_json(**result)
  68. # during the execution of the module, if there is an exception or a
  69. # conditional state that effectively causes a failure, run
  70. # AnsibleModule.fail_json() to pass in the message and the result
  71. if module.params['name'] == 'fail me':
  72. result['message'] = 'Outch !!!'
  73. module.fail_json(msg='The module Failed', **result)
  74. # use whatever logic you need to determine whether or not this module
  75. # made any modifications to your target
  76. if module.params['name'] != 'fail me':
  77. result['changed'] = True
  78. result['message'] = 'Hello World %s' % module.params['name']
  79. # in the event of a successful module execution, you will want to
  80. # simple AnsibleModule.exit_json(), passing the key/value results
  81. module.exit_json(**result)
  82. if __name__ == '__main__':
  83. main()