add check mode to address-list module

This commit is contained in:
Valentin Gurmeza 2017-06-05 14:27:23 -07:00
parent 5c57fdccad
commit 220f44766b
2 changed files with 57 additions and 37 deletions

View file

@ -54,19 +54,20 @@ 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),
list_name = dict(required=True, type='str'), list_name = dict(required=True, type='str'),
address_list = dict(required=False, type='list'), address_list = dict(required=False, type='list'),
state = dict( state = dict(
required = False, required = False,
default = "present", default = "present",
choices = ['present', 'absent', 'force'], choices = ['present', 'absent', 'force'],
type = 'str' type = 'str'
), ),
) ),
supports_check_mode=True
) )
hostname = module.params['hostname'] hostname = module.params['hostname']
@ -75,6 +76,7 @@ def main():
ansible_list_name = module.params['list_name'] ansible_list_name = module.params['list_name']
ansible_address_list = module.params['address_list'] ansible_address_list = module.params['address_list']
state = module.params['state'] state = module.params['state']
check_mode = module.check_mode
changed = False changed = False
msg = "" msg = ""
@ -84,8 +86,8 @@ def main():
mk.login() mk.login()
except: except:
module.fail_json( module.fail_json(
msg="Could not log into Mikrotik device." + msg="Could not log into Mikrotik device." +
" Check the username and password.", " Check the username and password.",
) )
response = mk.api_print(address_list_path) response = mk.api_print(address_list_path)
@ -106,9 +108,9 @@ def main():
if state == "present": if state == "present":
if ansible_address_list == mikrotik_address_list: if ansible_address_list == mikrotik_address_list:
module.exit_json( module.exit_json(
changed = False, changed = False,
failed = False, failed = False,
msg = "list up to date", msg = "list up to date",
) )
common_list = [] common_list = []
for item in ansible_address_list: for item in ansible_address_list:
@ -141,7 +143,8 @@ def main():
"list": list_name, "list": list_name,
"comment": i['comment'] "comment": i['comment']
} }
mk.api_add(address_list_path, add_dictionary) if not check_mode:
mk.api_add(address_list_path, add_dictionary)
changed = True changed = True
##################### #####################
@ -156,7 +159,8 @@ def main():
####################################### #######################################
for i in remove_list: for i in remove_list:
remove_id = mikrotik_address_id[i] remove_id = mikrotik_address_id[i]
mk.api_remove(address_list_path, remove_id) if not check_mode:
mk.api_remove(address_list_path, remove_id)
if not changed: if not changed:
changed = True changed = True
else: else:
@ -164,21 +168,22 @@ def main():
# Remove every item # Remove every item
####################################### #######################################
for remove_id in mikrotik_address_id.values(): for remove_id in mikrotik_address_id.values():
mk.api_remove(address_list_path, remove_id) if not check_mode:
mk.api_remove(address_list_path, remove_id)
if not changed: if not changed:
changed = True changed = True
if changed: if changed:
module.exit_json( module.exit_json(
changed = True, changed = True,
failed = False, failed = False,
msg = ansible_list_name + "has been modified", msg = ansible_list_name + "has been modified",
) )
else: else:
module.exit_json( module.exit_json(
changed = False, changed = False,
failed = False, failed = False,
msg = ansible_list_name + " is up to date", msg = ansible_list_name + " is up to date",
) )

View file

@ -610,7 +610,11 @@
- address: 192.168.1.6 - address: 192.168.1.6
comment: test_comment3 comment: test_comment3
register: address_list_add_1 register: address_list_add_1
failed_when: not ( address_list_add_1 | changed ) failed_when: (
not ansible_check_mode
) and (
not ( address_list_add_1 | changed )
)
- name: ALWAYS_CHANGES Test editing a firewall address-list - name: ALWAYS_CHANGES Test editing a firewall address-list
mt_ip_firewall_addresslist: mt_ip_firewall_addresslist:
@ -626,8 +630,12 @@
comment: dns2 comment: dns2
- address: 192.168.1.19 - address: 192.168.1.19
comment: test_comment3 comment: test_comment3
register: editress_list_edit_1 register: address_list_edit_1
failed_when: not ( editress_list_edit_1 | changed ) failed_when: (
not ansible_check_mode
) and (
not ( address_list_edit_1 | changed )
)
- name: Test adding a duplicate address-list - name: Test adding a duplicate address-list
mt_ip_firewall_addresslist: mt_ip_firewall_addresslist:
@ -643,8 +651,12 @@
comment: dns2 comment: dns2
- address: 192.168.1.19 - address: 192.168.1.19
comment: test_comment3 comment: test_comment3
register: add_dupress_list_add_dup_1 register: add_address_list_add_dup_1
failed_when: ( add_dupress_list_add_dup_1 | changed ) failed_when: (
not ansible_check_mode
) and (
( add_address_list_add_dup_1 | changed )
)
- name: ALWAYS_CHANGES Test removing a firewall address-list - name: ALWAYS_CHANGES Test removing a firewall address-list
mt_ip_firewall_addresslist: mt_ip_firewall_addresslist:
@ -653,11 +665,14 @@
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
state: "absent" state: "absent"
list_name: test_list list_name: test_list
register: remress_list_rem_1 register: address_list_rem_1
failed_when: not ( remress_list_rem_1 | changed ) failed_when: (
not ansible_check_mode
tags: address_list ) and (
not ( address_list_rem_1 | changed )
)
tags: address-list
################### ###################