meraki_snmp - Enable check mode (#54625)

* Add support for check mode

* Add diff support

* Remove check mode for idempotent action
This commit is contained in:
Kevin Breit 2019-06-19 10:35:10 -05:00 committed by Nathaniel Case
parent 8cff96e388
commit 8f68c8d438
3 changed files with 44 additions and 4 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- meraki_snmp - Add support for check mode.

View file

@ -153,6 +153,7 @@ import os
from ansible.module_utils.basic import AnsibleModule, json, env_fallback from ansible.module_utils.basic import AnsibleModule, json, env_fallback
from ansible.module_utils.urls import fetch_url from ansible.module_utils.urls import fetch_url
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
from ansible.module_utils.common.dict_transformations import recursive_diff
from ansible.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec from ansible.module_utils.network.meraki.meraki import MerakiModule, meraki_argument_spec
@ -203,10 +204,20 @@ def set_snmp(meraki, org_id):
snmp = get_snmp(meraki, org_id) snmp = get_snmp(meraki, org_id)
ignored_parameters = ['v3AuthPass', 'v3PrivPass', 'hostname', 'port', 'v2CommunityString', 'v3User'] ignored_parameters = ['v3AuthPass', 'v3PrivPass', 'hostname', 'port', 'v2CommunityString', 'v3User']
if meraki.is_update_required(snmp, full_compare, optional_ignore=ignored_parameters): if meraki.is_update_required(snmp, full_compare, optional_ignore=ignored_parameters):
if meraki.module.check_mode is True:
diff = recursive_diff(snmp, full_compare)
snmp.update(payload)
meraki.result['data'] = snmp
meraki.result['diff'] = {'before': diff[0],
'after': diff[1]}
meraki.exit_json(**meraki.result)
r = meraki.request(path, r = meraki.request(path,
method='PUT', method='PUT',
payload=json.dumps(payload)) payload=json.dumps(payload))
if meraki.status == 200: if meraki.status == 200:
diff = recursive_diff(snmp, r)
meraki.result['diff'] = {'before': diff[0],
'after': diff[1]}
meraki.result['changed'] = True meraki.result['changed'] = True
return r return r
else: else:
@ -260,9 +271,6 @@ def main():
# if the user is working with this module in only check mode we do not # 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 # want to make any changes to the environment, just return the current
# state with no modifications # state with no modifications
# FIXME: Work with Meraki so they can implement a check mode
if module.check_mode:
meraki.exit_json(**meraki.result)
# execute checks for argument completeness # execute checks for argument completeness
@ -275,7 +283,6 @@ def main():
org_id = meraki.params['org_id'] org_id = meraki.params['org_id']
if org_id is None: if org_id is None:
org_id = meraki.get_org_id(meraki.params['org_name']) org_id = meraki.get_org_id(meraki.params['org_name'])
if meraki.params['state'] == 'query': if meraki.params['state'] == 'query':
meraki.result['data'] = get_snmp(meraki, org_id) meraki.result['data'] = get_snmp(meraki, org_id)
elif meraki.params['state'] == 'present': elif meraki.params['state'] == 'present':

View file

@ -19,6 +19,20 @@
- debug: - debug:
msg: '{{snmp_query}}' msg: '{{snmp_query}}'
- name: Enable SNMPv2c with check mode
meraki_snmp:
auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}'
state: present
v2c_enabled: true
delegate_to: localhost
register: snmp_v2_enable_check
check_mode: yes
- assert:
that:
- snmp_v2_enable_check is not changed
- name: Enable SNMPv2c - name: Enable SNMPv2c
meraki_snmp: meraki_snmp:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'
@ -120,6 +134,23 @@
- snmp_idempotent.changed == False - snmp_idempotent.changed == False
- snmp_idempotent.data is defined - snmp_idempotent.data is defined
- name: Check for idempotency with check mode
meraki_snmp:
auth_key: '{{auth_key}}'
org_name: '{{test_org_name}}'
state: present
v3_enabled: true
v3_auth_mode: SHA
v3_auth_pass: ansiblepass
v3_priv_mode: AES128
v3_priv_pass: ansiblepass
delegate_to: localhost
register: snmp_idempotent_check
- assert:
that:
- snmp_idempotent_check is not changed
- name: Add peer IPs - name: Add peer IPs
meraki_snmp: meraki_snmp:
auth_key: '{{auth_key}}' auth_key: '{{auth_key}}'