diff --git a/library/mt_snmp.py b/library/mt_snmp.py index c9ee049..21428c2 100644 --- a/library/mt_snmp.py +++ b/library/mt_snmp.py @@ -55,67 +55,67 @@ from ansible.module_utils.basic import AnsibleModule def main(): - module = AnsibleModule( - argument_spec = dict( - hostname = dict(required=True), - username = dict(required=True), - password = dict(required=True), - settings = dict(required=False, type='dict'), - parameter = dict( - required = True, - choices = ['community', 'snmp'], - type = 'str' - ), - state = dict( - required = False, - choices = ['present', 'absent'], - type = 'str' - ), - ) + module = AnsibleModule( + argument_spec = dict( + hostname = dict(required=True), + username = dict(required=True), + password = dict(required=True), + settings = dict(required=False, type='dict'), + parameter = dict( + required = True, + choices = ['community', 'snmp'], + type = 'str' + ), + state = dict( + required = False, + choices = ['present', 'absent'], + type = 'str' + ), + ), + supports_check_mode=True + ) + + idempotent_parameter = None + params = module.params + + + if params['parameter'] == 'community': + idempotent_parameter = 'name' + params['parameter'] = "snmp/community" + + mt_obj = MikrotikIdempotent( + hostname = params['hostname'], + username = params['username'], + password = params['password'], + state = params['state'], + desired_params = params['settings'], + idempotent_param = idempotent_parameter, + api_path = '/' + str(params['parameter']), + check_mode = module.check_mode + ) + + mt_obj.sync_state() + + if mt_obj.failed: + module.fail_json( + msg = mt_obj.failed_msg ) - - idempotent_parameter = None - params = module.params - - - if params['parameter'] == 'community': - idempotent_parameter = 'name' - params['parameter'] = "snmp/community" - - mt_obj = MikrotikIdempotent( - hostname = params['hostname'], - username = params['username'], - password = params['password'], - state = params['state'], - desired_params = params['settings'], - idempotent_param = idempotent_parameter, - api_path = '/' + str(params['parameter']), - + elif mt_obj.changed: + module.exit_json( + failed=False, + changed=True, + msg=mt_obj.changed_msg, + diff={ "prepared": { + "old": mt_obj.old_params, + "new": mt_obj.new_params, + }}, + ) + else: + module.exit_json( + failed=False, + changed=False, + msg=params['settings'], ) - - mt_obj.sync_state() - - if mt_obj.failed: - module.fail_json( - msg = mt_obj.failed_msg - ) - elif mt_obj.changed: - module.exit_json( - failed=False, - changed=True, - msg=mt_obj.changed_msg, - diff={ "prepared": { - "old": mt_obj.old_params, - "new": mt_obj.new_params, - }}, - ) - else: - module.exit_json( - failed=False, - changed=False, - #msg='', - msg=params['settings'], - ) if __name__ == '__main__': main() diff --git a/tests/integration/tests.yml b/tests/integration/tests.yml index 8411d00..f6c50cf 100644 --- a/tests/integration/tests.yml +++ b/tests/integration/tests.yml @@ -1632,7 +1632,11 @@ settings: name: to_remove register: snmp_community_rem - failed_when: not ( snmp_community_rem | changed ) + failed_when: ( + not ansible_check_mode + ) and ( + not ( snmp_community_rem | changed ) + ) - name: ALWAYS_CHANGES modify existing snmp community mt_snmp: @@ -1645,7 +1649,11 @@ addresses: "10.0.0.0/8" name: icghol register: snmp_community - failed_when: not ( snmp_community | changed ) + failed_when: ( + not ansible_check_mode + ) and ( + not ( snmp_community | changed ) + ) - name: NEVER_CHANGES check idempotency on snmp community mt_snmp: @@ -1658,7 +1666,11 @@ addresses: "10.0.0.0/8" name: icghol register: snmp_community_idem - failed_when: ( snmp_community_idem | changed ) + failed_when: ( + not ansible_check_mode + ) and ( + ( snmp_community_idem | changed ) + ) - name: edit snmp settings mt_snmp: @@ -1682,7 +1694,11 @@ trap-community: icghol trap-version: 2 register: snmp_idem - failed_when: ( snmp_community_idem | changed ) + failed_when: ( + not ansible_check_mode + ) and ( + ( snmp_idem | changed ) + ) - name: ALWAYS_CHANGES check editing snmp mt_snmp: @@ -1695,7 +1711,11 @@ trap-community: icghol trap-version: 3 register: snmp_edit - failed_when: not ( snmp_edit | changed ) + failed_when: ( + not ansible_check_mode + ) and ( + not ( snmp_edit | changed ) + ) tags: snmp