diff --git a/changelogs/fragments/53597-meraki_snmp_check_mode.yml b/changelogs/fragments/53597-meraki_snmp_check_mode.yml new file mode 100644 index 00000000000..d17e44b8aa9 --- /dev/null +++ b/changelogs/fragments/53597-meraki_snmp_check_mode.yml @@ -0,0 +1,2 @@ +minor_changes: +- meraki_snmp - Add support for check mode. diff --git a/lib/ansible/modules/network/meraki/meraki_snmp.py b/lib/ansible/modules/network/meraki/meraki_snmp.py index 24edf29debb..923c537a1be 100644 --- a/lib/ansible/modules/network/meraki/meraki_snmp.py +++ b/lib/ansible/modules/network/meraki/meraki_snmp.py @@ -153,6 +153,7 @@ import os from ansible.module_utils.basic import AnsibleModule, json, env_fallback from ansible.module_utils.urls import fetch_url 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 @@ -203,10 +204,20 @@ def set_snmp(meraki, org_id): snmp = get_snmp(meraki, org_id) ignored_parameters = ['v3AuthPass', 'v3PrivPass', 'hostname', 'port', 'v2CommunityString', 'v3User'] 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, method='PUT', payload=json.dumps(payload)) if meraki.status == 200: + diff = recursive_diff(snmp, r) + meraki.result['diff'] = {'before': diff[0], + 'after': diff[1]} meraki.result['changed'] = True return r else: @@ -260,9 +271,6 @@ def main(): # 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 - # 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 @@ -275,7 +283,6 @@ def main(): org_id = meraki.params['org_id'] if org_id is None: org_id = meraki.get_org_id(meraki.params['org_name']) - if meraki.params['state'] == 'query': meraki.result['data'] = get_snmp(meraki, org_id) elif meraki.params['state'] == 'present': diff --git a/test/integration/targets/meraki_snmp/tasks/main.yml b/test/integration/targets/meraki_snmp/tasks/main.yml index c6abc9c0217..24627fbaf79 100644 --- a/test/integration/targets/meraki_snmp/tasks/main.yml +++ b/test/integration/targets/meraki_snmp/tasks/main.yml @@ -19,6 +19,20 @@ - debug: 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 meraki_snmp: auth_key: '{{auth_key}}' @@ -120,6 +134,23 @@ - snmp_idempotent.changed == False - 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 meraki_snmp: auth_key: '{{auth_key}}'