Refactor of ip address to use the idempotent class. Additional test to edit address

This commit is contained in:
antoni 2017-12-08 14:00:17 +01:00
parent c23500d05b
commit 80b0e11ee1
2 changed files with 104 additions and 140 deletions

View file

@ -3,38 +3,34 @@ module: mt_ip_address
author: author:
- "Valentin Gurmeza" - "Valentin Gurmeza"
- "Shaun Smiley" - "Shaun Smiley"
- "Antoni Matamalas"
version_added: "2.3" version_added: "2.3"
short_description: Manage mikrotik /ip/addresses short_description: Manage mikrotik /ip/addresses
requirements: requirements:
- rosapi - mt_api
description: description:
- FILL ME OUT - Manage addresses on interfaces
options: options:
hostname: hostname:
description: description:
- - hotstname of mikrotik router
required: True
username: username:
description: description:
- - username used to connect to mikrotik router
required: True
password: password:
description: description:
- - password used for authentication to mikrotik router
interface: required: True
settings:
description: description:
- - All Mikrotik compatible parameters for this particular endpoint.
address: Any yes/no values must be enclosed in double quotes
description: required: True
-
network:
description:
-
state: state:
description: description:
- - absent or present
force:
description:
- True/False value to force removing the address on an interface
even if the address does not match.
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -42,6 +38,7 @@ EXAMPLES = '''
hostname: "{{ inventory_hostname }}" hostname: "{{ inventory_hostname }}"
username: "{{ mt_user }}" username: "{{ mt_user }}"
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
settings:
interface: "ether2" interface: "ether2"
address: "192.168.88.2/24" address: "192.168.88.2/24"
network: "192.168.88.0/24" network: "192.168.88.0/24"
@ -49,11 +46,7 @@ EXAMPLES = '''
comment: "link 3" comment: "link 3"
''' '''
from ansible.module_utils import mt_api from ansible.module_utils.mt_common import clean_params, MikrotikIdempotent
import socket
#import mt_action #TODO: get this working
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
@ -64,120 +57,62 @@ def main():
hostname = dict(required=True), hostname = dict(required=True),
username = dict(required=True), username = dict(required=True),
password = dict(required=True), password = dict(required=True),
interface = dict(required=True, type='str'), settings = dict(required=False, type='dict'),
address = dict(required=True, type='str', aliases=['ip', 'addr', 'ip_address']),
network = dict(required=False, type='str', default=""),
comment = dict(required=False, type='str', default=""),
state = dict( state = dict(
required = False, required = False,
default = "present", default = "present",
choices = ['present', 'absent'], choices = ['present', 'absent'],
type = 'str' type = 'str'
), ),
) ),
supports_check_mode=True
) )
hostname = module.params['hostname'] idempotent_parameter = None
username = module.params['username'] params = module.params
password = module.params['password'] idempotent_parameter = 'interface'
ip_address = module.params['address'] mt_obj = MikrotikIdempotent(
interface = module.params['interface'] hostname = params['hostname'],
network = module.params['network'] username = params['username'],
ip_state = module.params['state'] password = params['password'],
comment = module.params['comment'] state = params['state'],
changed = False desired_params = params['settings'],
msg = "" idempotent_param = idempotent_parameter,
api_path = '/ip/address',
check_mode = module.check_mode
)
interface_path = '/interface' # exit if login failed
address_path = '/ip/address' if not mt_obj.login_success:
address_print_params = {
".proplist": "interface,address,.id,network,netmask,comment"
}
interface_print_params = {
".proplist": "name,.id,type"
}
mk = mt_api.Mikrotik(hostname,username,password)
try:
mk.login()
interfaces = mk.api_print(interface_path, interface_print_params)
except:
module.fail_json( module.fail_json(
msg="Could not log into Mikrotik device." + msg = mt_obj.failed_msg
" Check the username and password.",
) )
################################### # add, remove or edit things
# Check if interface is present mt_obj.sync_state()
# exit if interface is not present
###################################
interfacelist = []
exitmessage = []
for i in range(0, len(interfaces) - 1):
interfacelist.append(interfaces[i][1]["name"])
intExists = False
if (interface in interfacelist): if mt_obj.failed:
intExists = True module.fail_json(
# module.exit_json(failed=False, changed=False, msg=interfacelist) msg = mt_obj.failed_msg
if intExists:
pass
#exitmessage.append("Interface " + interface + " exists.") #this is never used
else:
exitmessage.append("Interface " + interface + " does not exist.")
module.fail_json(failed=True, msg=exitmessage)
##############################################
# Check if IP address is set on the interface
# make no changes if address already set
##############################################
ip_addresses = mk.api_print(address_path, address_print_params)
iplist = []
for i in range(0, len(ip_addresses) - 1):
iplist.append(ip_addresses[i][1]["address"])
if ip_addresses[i][1]["address"] == ip_address:
ip_id = ip_addresses[i][1][".id"]
if ip_state == "present":
if ip_address in iplist:
module.exit_json(
failed=False,
#msg="IP Address: " + ip_address +
#" is already configured" +
#" on interface " + interface,
) )
elif mt_obj.changed:
else:
add_dict = {
'address': ip_address,
'interface': interface,
'comment': comment
}
response = mk.api_add(address_path, add_dict)
module.exit_json( module.exit_json(
failed=False, failed=False,
changed=True, changed=True,
#msg="IP address: " + ip_address + " has been configured" + msg=mt_obj.changed_msg,
#" on interface " + interface diff={ "prepared": {
"old": mt_obj.old_params,
"new": mt_obj.new_params,
}},
) )
if ip_state == "absent":
if ip_address in iplist:
response = mk.api_remove(address_path, ip_id)
module.exit_json(
failed=False,
changed=True,
#msg="IP Address: " + ip_address +
#" has been removed"
)
else: else:
module.exit_json( module.exit_json(
failed=False, failed=False,
changed=False, changed=False,
#msg="IP Address: " + ip_address + #msg='',
#" is already absent" msg=params['settings'],
) )
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -5,18 +5,33 @@
username: "{{ mt_user }}" username: "{{ mt_user }}"
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
state: "present" state: "present"
settings:
interface: "ether2" interface: "ether2"
address: "192.168.88.2/24" address: "192.168.88.2/24"
network: "192.168.88.0" network: "192.168.88.0"
register: ip_addr_add_2 register: ip_addr_add_2
failed_when: not ( ip_addr_add_2 | changed ) failed_when: not ( ip_addr_add_2 | changed )
- name: NEVER_CHANGES Test adding the same ip addr again to ether2
mt_ip_address:
hostname: "{{ mt_hostname }}"
username: "{{ mt_user }}"
password: "{{ mt_pass }}"
state: "present"
settings:
interface: "ether2"
address: "192.168.88.2/24"
network: "192.168.88.0"
register: ip_addr_add_2
failed_when: ip_addr_add_2 | changed
- name: Test adding an ip addr with comment ether3 - name: Test adding an ip addr with comment ether3
mt_ip_address: mt_ip_address:
hostname: "{{ mt_hostname }}" hostname: "{{ mt_hostname }}"
username: "{{ mt_user }}" username: "{{ mt_user }}"
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
state: "present" state: "present"
settings:
interface: "ether3" interface: "ether3"
address: "192.168.88.3/24" address: "192.168.88.3/24"
comment: "interface #3!!!" comment: "interface #3!!!"
@ -27,17 +42,31 @@
username: "{{ mt_user }}" username: "{{ mt_user }}"
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
state: "present" state: "present"
settings:
interface: "ether4" interface: "ether4"
address: "192.168.88.4/24" address: "192.168.88.4/24"
network: "192.168.88.0" network: "192.168.88.0"
comment: "interface #4!!!" comment: "interface #4!!!"
- name: Test changing an ip addr with comment and network ether4
mt_ip_address:
hostname: "{{ mt_hostname }}"
username: "{{ mt_user }}"
password: "{{ mt_pass }}"
state: "present"
settings:
interface: "ether4"
address: "192.168.88.5/24"
network: "192.168.88.0"
comment: "interface #4 new address!!!"
- name: ALWAYS_CHANGES Test removing ip addr ether2 - name: ALWAYS_CHANGES Test removing ip addr ether2
mt_ip_address: mt_ip_address:
hostname: "{{ mt_hostname }}" hostname: "{{ mt_hostname }}"
username: "{{ mt_user }}" username: "{{ mt_user }}"
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
state: "absent" state: "absent"
settings:
interface: "ether2" interface: "ether2"
address: "192.168.88.2/24" address: "192.168.88.2/24"
register: ip_addr_rem_2 register: ip_addr_rem_2