add check mode to system module

This commit is contained in:
Valentin Gurmeza 2017-06-05 12:32:08 -07:00
parent fcd02f6384
commit 1326fad5c1
2 changed files with 76 additions and 62 deletions

View file

@ -57,67 +57,69 @@ from ansible.module_utils.basic import AnsibleModule
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
hostname = dict(required=True), hostname = dict(required=True),
username = dict(required=True), username = dict(required=True),
password = dict(required=True), password = dict(required=True),
settings = dict(required=False, type='dict'), settings = dict(required=False, type='dict'),
parameter = dict( parameter = dict(
required = True, required = True,
choices = ['ntp_client', 'clock', 'identity', 'logging', 'routerboard_settings'], choices = ['ntp_client', 'clock', 'identity', 'logging', 'routerboard_settings'],
type = 'str' type = 'str'
), ),
state = dict( state = dict(
required = False, required = False,
choices = ['present', 'absent'], choices = ['present', 'absent'],
type = 'str' type = 'str'
), ),
) ),
supports_check_mode=True
)
params = module.params
if params['parameter'] == 'routerboard_settings':
params['parameter'] = 'routerboard/settings'
if params['parameter'] == 'ntp_client':
params['parameter'] = 'ntp/client'
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= None,
api_path = '/system/' + params['parameter'],
check_mode = module.check_mode
)
mt_obj.sync_state()
if mt_obj.failed:
module.fail_json(
msg = mt_obj.failed_msg
) )
elif mt_obj.changed:
params = module.params module.exit_json(
failed=False,
if params['parameter'] == 'routerboard_settings': changed=True,
params['parameter'] = 'routerboard/settings' msg=mt_obj.changed_msg,
diff={ "prepared": {
if params['parameter'] == 'ntp_client': "old": mt_obj.old_params,
params['parameter'] = 'ntp/client' "new": mt_obj.new_params,
}},
clean_params(params['settings']) )
mt_obj = MikrotikIdempotent( else:
hostname = params['hostname'], module.exit_json(
username = params['username'], failed=False,
password = params['password'], changed=False,
state = params['state'], #msg='',
desired_params = params['settings'], msg=params['settings'],
idempotent_param= None,
api_path = '/system/' + params['parameter'],
) )
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__': if __name__ == '__main__':
main() main()

View file

@ -1432,7 +1432,11 @@
settings: settings:
time-zone-name: GMT time-zone-name: GMT
register: mt_clock register: mt_clock
failed_when: not ( mt_clock | changed ) failed_when: (
not ansible_check_mode
) and (
not ( mt_clock | changed )
)
- name: set ntp client - name: set ntp client
mt_system: mt_system:
@ -1456,7 +1460,11 @@
primary-ntp: 199.182.221.11 primary-ntp: 199.182.221.11
secondary-ntp: 67.215.197.149 secondary-ntp: 67.215.197.149
register: mt_ntp_client register: mt_ntp_client
failed_when: ( mt_ntp_client | changed ) failed_when: (
not ansible_check_mode
) and (
( mt_ntp_client | changed )
)
- name: ALWAYS_CHANGES modify ntp client - name: ALWAYS_CHANGES modify ntp client
mt_system: mt_system:
@ -1469,7 +1477,11 @@
primary-ntp: 199.182.221.11 primary-ntp: 199.182.221.11
secondary-ntp: 67.215.197.149 secondary-ntp: 67.215.197.149
register: mt_ntp_client_change register: mt_ntp_client_change
failed_when: not ( mt_ntp_client_change | changed ) failed_when: (
not ansible_check_mode
) and (
not ( mt_ntp_client_change | changed )
)
############################################## ##############################################
# WIP # WIP