From 1111a04f734f1edcdad7286c41f756b40e2bf57f Mon Sep 17 00:00:00 2001 From: antoni Date: Sun, 11 Mar 2018 19:30:19 +0100 Subject: [PATCH] Added option for idempotent parameter to allow the user to control behavior of the module --- library/mt_ip_address.py | 59 ++++++++- tests/integration/tasks/test-ip-address.yml | 131 ++++++++++++++++++++ 2 files changed, 184 insertions(+), 6 deletions(-) diff --git a/library/mt_ip_address.py b/library/mt_ip_address.py index 59eb8d1..e75ed4e 100644 --- a/library/mt_ip_address.py +++ b/library/mt_ip_address.py @@ -23,6 +23,19 @@ options: description: - password used for authentication to mikrotik router required: True + idempotent: + description: + - parameter that will define the behavior for the ip address status. + - If "interface" is used, only one IP will be allowed per interface. + The "state" parameter will define if the IP is added, edited or + removed. No settings options are required to removed the IP from an + interface + - If "address" is used, and interface will be able to have multiple IPs, + but address will only be added or removed. In order to change an IP, it + will have to be first removed and then added to the interface in two + tasks. + required: False + default: address settings: description: - All Mikrotik compatible parameters for this particular endpoint. @@ -30,20 +43,50 @@ options: required: True state: description: - - absent or present + - Depending on the idempotent option, it will define the status of the IP + on an interface + required: False + default: present ''' EXAMPLES = ''' +# Add IP to an interface with a comment. If the interface has already an IP it +# will add as a sencond IP - mt_ip_address: hostname: "{{ inventory_hostname }}" username: "{{ mt_user }}" password: "{{ mt_pass }}" + idempotent: "address" + state: "present" settings: interface: "ether2" address: "192.168.88.2/24" network: "192.168.88.0/24" - state: "present" comment: "link 3" + +# Assign IP to the interface. If the interface has any previous IP, it will be +# replaced by this one. +- mt_ip_address: + hostname: "{{ inventory_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + idempotent: "interface" + state: "present" + settings: + interface: "ether2" + address: "192.168.88.2/24" + network: "192.168.88.0/24" + comment: "link 3" + +# Remove any IP from an interface +- mt_ip_address: + hostname: "{{ inventory_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + idempotent: "interface" + state: "absent" + settings: + interface: "ether2" ''' from ansible.module_utils.mt_common import clean_params, MikrotikIdempotent @@ -57,7 +100,13 @@ def main(): hostname = dict(required=True), username = dict(required=True), password = dict(required=True, no_log=True), - settings = dict(required=False, type='dict'), + settings = dict(required=True, type='dict'), + idempotent = dict( + required = False, + default = 'address', + choices = ['address', 'interface'], + type = 'str' + ), state = dict( required = False, default = "present", @@ -68,16 +117,14 @@ def main(): supports_check_mode=True ) - idempotent_parameter = None params = module.params - idempotent_parameter = 'address' mt_obj = MikrotikIdempotent( hostname = params['hostname'], username = params['username'], password = params['password'], state = params['state'], desired_params = params['settings'], - idempotent_param = idempotent_parameter, + idempotent_param = params['idempotent'], api_path = '/ip/address', check_mode = module.check_mode ) diff --git a/tests/integration/tasks/test-ip-address.yml b/tests/integration/tasks/test-ip-address.yml index 7401986..fda8be2 100644 --- a/tests/integration/tasks/test-ip-address.yml +++ b/tests/integration/tasks/test-ip-address.yml @@ -16,6 +16,21 @@ - "bridge3" - "bridge4" +- name: Remove any ip from bridge interfaces + mt_ip_address: + hostname: "{{ mt_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + state: absent + idempotent: interface + settings: + interface: "{{ item }}" + with_items: + - "bridge1" + - "bridge2" + - "bridge3" + - "bridge4" + - name: ALWAYS_CHANGES Test adding an ip addr bridge2 mt_ip_address: hostname: "{{ mt_hostname }}" @@ -137,3 +152,119 @@ register: ip_addr_rem_4 failed_when: ip_addr_rem_4 | changed +- name: Remove any IP from bridge interfaces + mt_ip_address: + hostname: "{{ mt_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + state: absent + idempotent: interface + settings: + interface: "{{ item }}" + with_items: + - "bridge1" + - "bridge2" + - "bridge3" + - "bridge4" + +- name: ALWAYS_CHANGES Add ip using idempotent interface on bridge2 + mt_ip_address: + hostname: "{{ mt_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + state: "present" + idempotent: "interface" + settings: + interface: "bridge2" + address: "192.168.89.2/24" + register: ip_addr_rem_2 + failed_when: not (ip_addr_rem_2 | changed) + +- name: NEVER_CHANGES Add AGAIN ip using idempotent interface on bridge2 + mt_ip_address: + hostname: "{{ mt_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + state: "present" + idempotent: "interface" + settings: + interface: "bridge2" + address: "192.168.89.2/24" + register: ip_addr_rem_2 + failed_when: ip_addr_rem_2 | changed + +- name: ALWAYS_CHANGES Add ip using idempotent interface on bridge3 + mt_ip_address: + hostname: "{{ mt_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + state: "present" + idempotent: "interface" + settings: + interface: "bridge3" + address: "192.168.89.3/24" + register: ip_addr_rem_3 + failed_when: not (ip_addr_rem_3 | changed) + +- name: ALWAYS_CHANGES Remove ip using idempotent interface on bridge3 + mt_ip_address: + hostname: "{{ mt_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + state: "absent" + idempotent: "interface" + settings: + interface: "bridge3" + register: ip_addr_rem_3 + failed_when: not (ip_addr_rem_3 | changed) + +- name: NEVER_CHANGES Remove ip AGAIN using idempotent interface on bridge3 + mt_ip_address: + hostname: "{{ mt_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + state: "absent" + idempotent: "interface" + settings: + interface: "bridge3" + register: ip_addr_rem_3 + failed_when: ip_addr_rem_3 | changed + +- name: ALWAYS_CHANGES Add ip using idempotent interface on bridge4 + mt_ip_address: + hostname: "{{ mt_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + state: "present" + idempotent: "interface" + settings: + interface: "bridge4" + address: "192.168.89.4/24" + register: ip_addr_rem_4 + failed_when: not (ip_addr_rem_4 | changed) + +- name: ALWAYS_CHANGES Edit ip using idempotent interface on bridge4 + mt_ip_address: + hostname: "{{ mt_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + state: "present" + idempotent: "interface" + settings: + interface: "bridge4" + address: "192.168.89.5/24" + register: ip_addr_rem_4 + failed_when: not (ip_addr_rem_4 | changed) + +- name: ALWAYS_CHANGES Add comment using idempotent interface on bridge4 + mt_ip_address: + hostname: "{{ mt_hostname }}" + username: "{{ mt_user }}" + password: "{{ mt_pass }}" + state: "present" + idempotent: "interface" + settings: + interface: "bridge4" + comment: "This is a final comment" + register: ip_addr_rem_4 + failed_when: not (ip_addr_rem_4 | changed)