From fcd02f63843efdf65a39be14813dede37e5e3699 Mon Sep 17 00:00:00 2001 From: Valentin Gurmeza Date: Mon, 5 Jun 2017 12:22:26 -0700 Subject: [PATCH] add check mode to tests.yml --- library/mt_tool.py | 110 ++++++++++++++++++------------------ tests/integration/tests.yml | 24 ++++++-- 2 files changed, 75 insertions(+), 59 deletions(-) diff --git a/library/mt_tool.py b/library/mt_tool.py index 73ca5af..c81508a 100644 --- a/library/mt_tool.py +++ b/library/mt_tool.py @@ -53,69 +53,69 @@ from mt_common import clean_params, MikrotikIdempotent 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 = ['e-mail', 'netwatch'], - 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 = ['e-mail', 'netwatch'], + type = 'str' + ), + state = dict( + required = False, + choices = ['present', 'absent'], + type = 'str' + ), + ), + supports_check_mode=True + ) - idempotent_parameter = None - params = module.params + idempotent_parameter = None + params = module.params - if params['parameter'] == 'netwatch': - idempotent_parameter = 'host' + if params['parameter'] == 'netwatch': + idempotent_parameter = 'host' # clean_params(params['settings']) - 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 = '/tool/' + str(params['parameter']), + 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 = '/tool/' + str(params['parameter']), + check_mode = module.check_mode + ) + + mt_obj.sync_state() + + if mt_obj.failed: + module.fail_json( + msg=mt_obj.failed_msg ) - - 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'], - ) + 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'], + ) if __name__ == '__main__': main() diff --git a/tests/integration/tests.yml b/tests/integration/tests.yml index dc31706..0458866 100644 --- a/tests/integration/tests.yml +++ b/tests/integration/tests.yml @@ -1515,7 +1515,11 @@ address: 192.168.1.3 from: email@localhost.com register: email_edit - failed_when: not ( email_edit | changed ) + failed_when: ( + not ansible_check_mode + ) and ( + not ( email_edit | changed ) + ) - name: add netwatch item mt_tool: @@ -1539,7 +1543,11 @@ host: '192.168.10.1' up-script: test register: netwatch_idem - failed_when: ( netwatch_idem | changed ) + failed_when: ( + not ansible_check_mode + ) and ( + ( netwatch_idem | changed ) + ) - name: ALWAYS_CHANGES edit netwatch item, change up-script mt_tool: @@ -1552,7 +1560,11 @@ host: '192.168.10.1' up-script: test2 register: netwatch_edit - failed_when: not ( netwatch_edit | changed ) + failed_when: ( + not ansible_check_mode + ) and ( + not ( netwatch_edit | changed ) + ) - name: ALWAYS_CHANGES remove netwatch item mt_tool: @@ -1564,7 +1576,11 @@ settings: host: '192.168.10.1' register: netwatch_rem - failed_when: not ( netwatch_rem | changed ) + failed_when: ( + not ansible_check_mode + ) and ( + not ( netwatch_rem | changed ) + ) tags: tool