.. _appendix_module_utilities:
***** ***** ***** ***** ***** *
Appendix: Module Utilities
***** ***** ***** ***** ***** *
Ansible provides a number of module utilities that provide helper functions that you can use when developing your own modules. The `` basic.py `` module utility provides the main entry point for accessing the Ansible library, and all Python Ansible modules must, at minimum, import `` AnsibleModule `` ::
from ansible.module_utils.basic import AnsibleModule
If you need to share Python code between some of your own local modules, you can use Ansible's `` module_utils `` directories for this. When you run `` ansible-playbook `` , Ansible will merge any files in the local `` module_utils `` directory into the `` ansible.module_utils `` namespace. For example, if you have your own custom modules that import a `` my_shared_code `` library, you can place that into a `` ./module_utils/my_shared_code.py `` file in the root location where your playbook lives, and then import it in your modules like so::
from ansible.module_utils.my_shared_code import MySharedCodeClient
Your custom `` module_utils `` directories can live in the root directory of your playbook, or in the individual role directories, or in the directories specified by the `` ANSIBLE_MODULE_UTILS `` configuration setting.
Ansible ships with the following list of `` module_utils `` files. The module utility source code lives in the `` ./lib/ansible/module_utils `` directory under your main Ansible path - for more details on any specific module utility, please see the source code.
.. include :: shared_snippets/licensing.txt
- alicloud_ecs.py - Definitions and utilities for modules working with Alibaba Cloud ECS.
- api.py - Adds shared support for generic API modules.
- azure_rm_common.py - Definitions and utilities for Microsoft Azure Resource Manager template deployments.
- basic.py - General definitions and helper utilities for Ansible modules.
- cloudstack.py - Utilities for CloudStack modules.
- database.py - Miscellaneous helper functions for PostGRES and MySQL
- docker_common.py - Definitions and helper utilities for modules working with Docker.
- ec2.py - Definitions and utilities for modules working with Amazon EC2
- facts/- Folder containing helper functions for modules that return facts. See https://github.com/ansible/ansible/pull/23012 for more information.
- gce.py - Definitions and helper functions for modules that work with Google Compute Engine resources.
- ismount.py - Contains single helper function that fixes os.path.ismount
- keycloak.py - Definitions and helper functions for modules working with the Keycloak API
- known_hosts.py - utilities for working with known_hosts file
- manageiq.py - Functions and utilities for modules that work with ManageIQ platform and its resources.
- memset.py - Helper functions and utilities for interacting with Memset's API.
- mysql.py - Allows modules to connect to a MySQL instance
- netapp.py - Functions and utilities for modules that work with the NetApp storage platforms.
- network/a10/a10.py - Utilities used by the a10_server module to manage A10 Networks devices.
- network/aci/aci.py - Definitions and helper functions for modules that manage Cisco ACI Fabrics.
- network/aireos/aireos.py - Definitions and helper functions for modules that manage Cisco WLC devices.
- network/aos/aos.py - Module support utilities for managing Apstra AOS Server.
- network/aruba/aruba.py - Helper functions for modules working with Aruba networking devices.
- network/asa/asa.py - Module support utilities for managing Cisco ASA network devices.
- network/avi/avi.py - Helper functions for modules working with AVI networking devices.
- network/bigswitch/bigswitch_utils.py - Utilities used by the bigswitch module to manage Big Switch Networks devices.
- network/cloudengine/ce.py - Module support utilities for managing Huawei Cloudengine switch.
- network/cnos/cnos.py - Helper functions for modules working on devices running Lenovo CNOS.
- network/common/config.py - Configuration utility functions for use by networking modules
- network/common/netconf.py - Definitions and helper functions for modules that use Netconf transport.
- network/common/parsing.py - Definitions and helper functions for Network modules.
- network/common/network.py - Functions for running commands on networking devices
- network/common/utils.py - Defines commands and comparison operators and other utilises for use in networking modules
- network/dellos6/dellos6.py - Module support utilities for managing device running Dell OS6.
- network/dellos9/dellos9.py - Module support utilities for managing device running Dell OS9.
- network/dellos10/dellos10.py - Module support utilities for managing device running Dell OS10.
- network/enos/enos.py - Helper functions for modules working with Lenovo ENOS devices.
- network/eos/eos.py - Helper functions for modules working with EOS networking devices.
- network/fortios/fortios.py - Module support utilities for managing FortiOS devices.
- network/ios/ios.py - Definitions and helper functions for modules that manage Cisco IOS networking devices
- network/iosxr/iosxr.py - Definitions and helper functions for modules that manage Cisco IOS-XR networking devices.
- network/ironware/ironware.py - Module support utilities for managing Brocade IronWare devices.
- network/junos/junos.py - Definitions and helper functions for modules that manage Junos networking devices.
Meraki organization module (#38773)
* Initial commit
Query an organization within Meraki. No support is in place for managing
or creating yet
* Change output_level method and make the state parameter required.
* Implemented listing all organizations
- Updated documentation
- Parse results and return all organizations
- Parse results and return specified organization
* Framework for creating an organization
- Documentation example for organization creation
- Framework exists for creating organizations, pending PR 36809
- Created functions for HTTP calls
- Renamed from dashboard.meraki.com to api.meraki.com
- Added required_if for state
* Remove absent state
- Meraki API does not support deleting an organization so absent is removed
- Updated documentation to call it state instead of status
* Small change to documentation
* Support all parameters associated to organization
- Added all parameters needed for all organization actions.
- None of the added ones work at this time.
- Added documentation for clone.
* Integration test for meraki_organization module
* Rename module to meraki for porting to module utility
* Meraki documentation fragment
- Created initial documentation fragment for Meraki modules
* Add meraki module utility to branch. Formerly was on a separate branch.
* CRU support for Meraki organization module
* CRU is supported for Meraki organizations
* There is no DELETE function for organizations in the API
* This code is very messy and needs cleanup
* Create and Update actions don't show status as updated, must fix
* Added Meraki module utility to module utility documentation list
* Added support for organization cloning
* Renamed use_ssl to use_https
* Removed define_method()
* Removed is_org()
* Added is_org_valid() which does all org sanity checks
* Fixes for ansibot
- Changed default of use_proxy from true to false
- Removed some commented out code
- Updated documentation
* Changes for ansibot
- Removed requirement for state parameter. I may readd this.
- Updated formatting
diff --git a/lib/ansible/module_utils/network/meraki/meraki.py b/lib/ansible/module_utils/network/meraki/meraki.py
index 3acd3d1038..395ac7c4b4 100644
--- a/lib/ansible/module_utils/network/meraki/meraki.py
+++ b/lib/ansible/module_utils/network/meraki/meraki.py
@@ -42,7 +42,7 @@ def meraki_argument_spec():
return dict(auth_key=dict(type='str', no_log=True, fallback=(env_fallback, ['MERAKI_KEY'])),
host=dict(type='str', default='api.meraki.com'),
name=dict(type='str'),
- state=dict(type='str', choices=['present', 'absent', 'query'], required=True),
+ state=dict(type='str', choices=['present', 'absent', 'query']),
use_proxy=dict(type='bool', default=False),
use_https=dict(type='bool', default=True),
validate_certs=dict(type='bool', default=True),
diff --git a/lib/ansible/modules/network/meraki/meraki_organization.py b/lib/ansible/modules/network/meraki/meraki_organization.py
index 923d969366..3789be91d6 100644
--- a/lib/ansible/modules/network/meraki/meraki_organization.py
+++ b/lib/ansible/modules/network/meraki/meraki_organization.py
@@ -20,11 +20,9 @@ short_description: Manage organizations in the Meraki cloud
version_added: "2.6"
description:
- Allows for creation, management, and visibility into organizations within Meraki
-
notes:
- More information about the Meraki API can be found at U(https://dashboard.meraki.com/api_docs).
- Some of the options are likely only used for developers within Meraki
-
options:
name:
description:
@@ -32,21 +30,18 @@ options:
- If C(clone) is specified, C(name) is the name of the new organization.
state:
description:
- - Create or query organizations
- choices: ['query', 'present']
+ - Create or modify an organization
+ choices: ['present', 'query']
clone:
description:
- Organization to clone to a new organization.
- type: string
org_name:
description:
- Name of organization.
- Used when C(name) should refer to another object.
- type: string
org_id:
description:
- ID of organization
-
author:
- Kevin Breit (@kbreit)
extends_documentation_fragment: meraki
@@ -86,7 +81,6 @@ RETURN = '''
response:
description: Data returned from Meraki dashboard.
type: dict
- state: query
returned: info
'''
@@ -103,6 +97,7 @@ def main():
argument_spec = meraki_argument_spec()
argument_spec.update(clone=dict(type='str'),
+ state=dict(type='str', choices=['present', 'query']),
)
@@ -125,11 +120,9 @@ def main():
meraki.function = 'organizations'
meraki.params['follow_redirects'] = 'all'
- meraki.required_if=[
- ['state', 'present', ['name']],
- ['clone', ['name']],
- # ['vpn_PublicIP', ['name']],
- ]
+ meraki.required_if = [['state', 'present', ['name']],
+ ['clone', ['name']],
+ ]
create_urls = {'organizations': '/organizations',
}
@@ -162,23 +155,16 @@ def main():
-
- # method = None
- # org_id = None
-
-
- # meraki.fail_json(msg=meraki.is_org_valid(meraki.get_orgs(), org_name='AnsibleTestOrg'))
-
if meraki.params['state'] == 'query':
- if meraki.params['name'] is None: # Query all organizations, no matter what
- orgs = meraki.get_orgs()
- meraki.result['organization'] = orgs
- elif meraki.params['name'] is not None: # Query by organization name
- module.warn('All matching organizations will be returned, even if there are duplicate named organizations')
- orgs = meraki.get_orgs()
- for o in orgs:
- if o['name'] == meraki.params['name']:
- meraki.result['organization'] = o
+ if meraki.params['name'] is None: # Query all organizations, no matter what
+ orgs = meraki.get_orgs()
+ meraki.result['organization'] = orgs
+ elif meraki.params['name'] is not None: # Query by organization name
+ module.warn('All matching organizations will be returned, even if there are duplicate named organizations')
+ orgs = meraki.get_orgs()
+ for o in orgs:
+ if o['name'] == meraki.params['name']:
+ meraki.result['organization'] = o
elif meraki.params['state'] == 'present':
if meraki.params['clone'] is not None: # Cloning
payload = {'name': meraki.params['name']}
@@ -193,7 +179,10 @@ def main():
payload = {'name': meraki.params['name'],
'id': meraki.params['org_id'],
}
- meraki.result['response'] = json.loads(meraki.request(meraki.construct_path('update', org_id=meraki.params['org_id']), payload=json.dumps(payload), method='PUT'))
+ meraki.result['response'] = json.loads(meraki.request(meraki.construct_path('update',
+ org_id=meraki.params['org_id']),
+ payload=json.dumps(payload),
+ method='PUT'))
diff --git a/lib/ansible/utils/module_docs_fragments/meraki.py b/lib/ansible/utils/module_docs_fragments/meraki.py
index e268d02e68..3569d83b99 100644
--- a/lib/ansible/utils/module_docs_fragments/meraki.py
+++ b/lib/ansible/utils/module_docs_fragments/meraki.py
@@ -35,6 +35,7 @@ options:
description:
- Set amount of debug output during module execution
choices: ['normal', 'debug']
+ default: 'normal'
timeout:
description:
- Time to timeout for HTTP requests.
diff --git a/test/integration/targets/meraki_organization/aliases b/test/integration/targets/meraki_organization/aliases
new file mode 100644
index 0000000000..ad7ccf7ada
--- /dev/null
+++ b/test/integration/targets/meraki_organization/aliases
@@ -0,0 +1 @@
+unsupported
* Formatting fix
* Minor updates due to testing
- Made state required again
- Improved formatting for happier PEP8
- request() now sets instance method
* Fix reporting of the result
* Enhance idempotency checks
- Remove merging functionality as the proposed should be used
- Do check and reverse check to look for differences
* Rewrote and added additional integration tests. This isn't done.
* Updated is_update_required method:
- Original and proposed data is passed to method
- Added ignored_keys list so it can be skipped if needed
* Changes per comments from dag
- Optionally assign function on class instantiation
- URLs now have {} for substitution method
- Move auth_key check to module utility
- Remove is_new and get_existing
- Minor changes to documentation
* Enhancements for future modules and organization
- Rewrote construct_path method for simplicity
- Increased support for network functionality to be committed
* Changes based on Dag feedback and to debug problems
* Minor fixes for validitation testing
* Small changes for dag and Ansibot
- Changed how auth_key is processed
- Removed some commented lines
- Updated documentation fragment, but that may get reverted
* Remove blank line and comment
* Improvements for testing and code simplification
- Added network integration tests
- Modified error handling in request()
- More testing to come on this
- Rewrote construct_path again. Very simple now.
* Remove trailing whitespace
* Small changes based on dag's response
* Removed certain sections from exit_json and fail_json as they're old
7 years ago
- network/meraki/meraki.py - Utilities specifically for the Meraki network modules.
- network/netscaler/netscaler.py - Utilities specifically for the netscaler network modules.
- network/nso/nso.py - Utilities for modules that work with Cisco NSO.
- network/nxos/nxos.py - Contains definitions and helper functions specific to Cisco NXOS networking devices.
- network/onyx/onyx.py - Definitions and helper functions for modules that manage Mellanox ONYX networking devices.
- network/ordance/ordance.py - Module support utilities for managing Ordnance devices.
- network/sros/sros.py - Helper functions for modules working with Open vSwitch bridges.
- network/vyos/vyos.py - Definitions and functions for working with VyOS networking
- openstack.py - Utilities for modules that work with Openstack instances.
- openswitch.py - Definitions and helper functions for modules that manage OpenSwitch devices
- powershell.ps1 - Utilities for working with Microsoft Windows clients
- pure.py - Functions and utilities for modules that work with the Pure Storage storage platforms.
- pycompat24.py - Exception workaround for Python 2.4.
- rax.py - Definitions and helper functions for modules that work with Rackspace resources.
- redhat.py - Functions for modules that manage Red Hat Network registration and subscriptions
- service.py - Contains utilities to enable modules to work with Linux services (placeholder, not in use).
- shell.py - Functions to allow modules to create shells and work with shell commands
- six/__init__.py - Bundled copy of the `Six Python library <https://pythonhosted.org/six/> `_ to aid in writing code compatible with both Python 2 and Python 3.
- splitter.py - String splitting and manipulation utilities for working with Jinja2 templates
- urls.py - Utilities for working with http and https requests
- utm_utils.py - Contains base class for creating new Sophos UTM Modules and helper functions for handling the rest interface of Sophos UTM
- vca.py - Contains utilities for modules that work with VMware vCloud Air
- vexata.py - Utilities for modules that work with Vexata storage platforms.
- vmware.py - Contains utilities for modules that work with VMware vSphere VMs
New module: xenserver_guest - manages XenServer VMs (#41041)
* XenServer related modules - initial commit
- New module_util: xenserver. Contains common module arguments, functions
and classes useful for future XenServer related modules.
- New module_docs_fragment: xenserver. Describes common module arguments.
- New module: xenserver_guest. Supports VM deployment, reconfiguration,
removal, detection of changes, state management, fact gathering and
Ansible check mode. Module is fully documented.
- Updated: developing_module_utilities.rst.
- Module params, workflow and some functions are based on or taken from
vmware_guest module.
* Implemented support for configuring custom VM params in xenserver_guest module
* Compatibility fixes and documentation update
- xenserver module_util: implemented support for XenAPI.py version older
than 7.2.
- xenserver module_util: PEP8 fixes.
- xenserver module_util: Added missing imports.
- xenserver module_util: Copyright notice fixes.
- xenserver_guest module: updated module documentation with notes regarding
module requirements and compatibility.
- xenserver_guest module: bumped version_added to 2.7.
- xenserver_guest module: minor fixes.
* VM power state management refactoring, subargument specs in xenserver_guest module, other fixes
- VM power state management code moved from xenserver_guest module to xenserver
module_util (set_vm_power_state function).
- Code for waiting for VM IP address moved from xenserver_guest module to
xenserver module_util (wait_for_vm_ip_address function).
- xenserver module_util: implemented get_object_ref function to clean up
a lot of repeated code in xenserver_guest module.
- xenserver module_util: added additional aliases 'host' and 'pool' for
'hostname' common module argument. They are more in line with what
XenServer users are familiar with.
- xenserver module_util: minor fixes.
- xenserver_guest module: removed VM state management other than 'present',
'absent' and 'poweredon'. Other states are to be managed by separate module.
- xenserver_guest module: added subargument specs and cleaned up custom code
for subargument validation.
- xenserver_guest module: reorganized code for disk and network reconfiguation
to minimize code duplication.
- xenserver_guest module: renamed 'cdrom.iso' module argument to
'cdrom.iso_name', avoids cryptic error message when parameter is
missing.
- xenserver_guest module: documentation update.
- xenserver_guest module: changes in error messages.
- xenserver_guest module: minor fixes.
* Implemented guest OS network parameter configuration and other
- xenserver module_util: moved code for validating MAC addresses from
xenserver_guest module and implemented a range of functions for validating
IP addresses and related entities and converting prefixes to netmasks and
vice versa.
- xenserver module_util: updated fact gathering code to support guest OS
network parameters.
- xenserver module_util: added docstrings.
- xenserver module_util: minor changes.
- xenserver_guest module: implemented support for guest OS network parameter
configuration.
- xenserver_guest module: changed CD-ROM handling code.
- xenserver_guest module: changed so that user friendly version of changes
list is always returned in module result.
- xenserver_guest module: error message changes.
- xenserver_guest module: added docstrings.
- xenserver_guest module: documentation update.
- xenserver_guest module: minor changes and fixes.
* Various fixes and code cleanup
- xenserver module_util: implemented get_xenserver_version function.
- xenserver module_util: moved customization agent detection code to
gather_vm_params function. customization_agent variable is now part of
vm_params. An exception in customization agent detection code that prevented
deployment of new VMs is also fixed.
- xenserver module_util: added support for alternative VM state names with
dash and underscore in set_power_state function.
- xenserver_guest module: removed customization agent detection code
as it is now implemented in xenserver module_util.
- xenserver_guest module: fixed a bug in xenserver_data update code that
occured when "networks.mac" was not specified in module params and
other fixes.
- xenserver_guest module: some code cleanup.
- xenserver_guest module: bumped version_added to 2.8.
6 years ago
- xenserver.py - Contains utilities for modules that work with XenServer.