| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #!/usr/bin/env python3
- # 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: hello_world
- short_description: This is my first module
- # If this is part of a collection, you need to use semantic versioning,
- # i.e. the version is of the form "2.5.0" and not "2.4".
- version_added: "1.0.0"
- description: This is my longer description explaining my test module.
- options:
- name:
- description: It is the name to display.
- required: true
- type: str
- # Specify this value according to your collection
- # in format of namespace.collection.doc_fragment_name
- # extends_documentation_fragment:
- # - my_namespace.my_collection.my_doc_fragment_name
- author:
- - Your Name (@yourGitHubHandle)
- '''
- EXAMPLES = r'''
- # Pass name to the module
- - name: Hello Steph
- my_namespace.my_collection.hello_world:
- name: Steph
- '''
- RETURN = r'''
- # These are examples of possible return values, and in general should use other names for return values.
- message:
- description: The output message that the test module generates.
- type: str
- returned: always
- sample: 'Hello World Stephane'
- '''
- from ansible.module_utils.basic import AnsibleModule
- def main():
- # define available arguments/parameters a user can pass to the module
- module_args = dict(
- name=dict(type='str', 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,
- 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:
- module.exit_json(**result)
- # 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':
- result['message'] = 'Outch !!!'
- module.fail_json(msg='The module Failed', **result)
- # use whatever logic you need to determine whether or not this module
- # made any modifications to your target
- if module.params['name'] != 'fail me':
- result['changed'] = True
- result['message'] = 'Hello World %s' % module.params['name']
- # 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)
- if __name__ == '__main__':
- main()
|