mirror of
https://github.com/zahodi/ansible-mikrotik.git
synced 2025-07-27 22:34:34 +02:00
Added option for idempotent parameter to allow the user to control behavior of the module
This commit is contained in:
parent
3cec44d451
commit
1111a04f73
2 changed files with 184 additions and 6 deletions
|
@ -23,6 +23,19 @@ options:
|
||||||
description:
|
description:
|
||||||
- password used for authentication to mikrotik router
|
- password used for authentication to mikrotik router
|
||||||
required: True
|
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:
|
settings:
|
||||||
description:
|
description:
|
||||||
- All Mikrotik compatible parameters for this particular endpoint.
|
- All Mikrotik compatible parameters for this particular endpoint.
|
||||||
|
@ -30,20 +43,50 @@ options:
|
||||||
required: True
|
required: True
|
||||||
state:
|
state:
|
||||||
description:
|
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 = '''
|
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:
|
- mt_ip_address:
|
||||||
hostname: "{{ inventory_hostname }}"
|
hostname: "{{ inventory_hostname }}"
|
||||||
username: "{{ mt_user }}"
|
username: "{{ mt_user }}"
|
||||||
password: "{{ mt_pass }}"
|
password: "{{ mt_pass }}"
|
||||||
|
idempotent: "address"
|
||||||
|
state: "present"
|
||||||
settings:
|
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"
|
||||||
state: "present"
|
|
||||||
comment: "link 3"
|
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
|
from ansible.module_utils.mt_common import clean_params, MikrotikIdempotent
|
||||||
|
@ -57,7 +100,13 @@ def main():
|
||||||
hostname = dict(required=True),
|
hostname = dict(required=True),
|
||||||
username = dict(required=True),
|
username = dict(required=True),
|
||||||
password = dict(required=True, no_log=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(
|
state = dict(
|
||||||
required = False,
|
required = False,
|
||||||
default = "present",
|
default = "present",
|
||||||
|
@ -68,16 +117,14 @@ def main():
|
||||||
supports_check_mode=True
|
supports_check_mode=True
|
||||||
)
|
)
|
||||||
|
|
||||||
idempotent_parameter = None
|
|
||||||
params = module.params
|
params = module.params
|
||||||
idempotent_parameter = 'address'
|
|
||||||
mt_obj = MikrotikIdempotent(
|
mt_obj = MikrotikIdempotent(
|
||||||
hostname = params['hostname'],
|
hostname = params['hostname'],
|
||||||
username = params['username'],
|
username = params['username'],
|
||||||
password = params['password'],
|
password = params['password'],
|
||||||
state = params['state'],
|
state = params['state'],
|
||||||
desired_params = params['settings'],
|
desired_params = params['settings'],
|
||||||
idempotent_param = idempotent_parameter,
|
idempotent_param = params['idempotent'],
|
||||||
api_path = '/ip/address',
|
api_path = '/ip/address',
|
||||||
check_mode = module.check_mode
|
check_mode = module.check_mode
|
||||||
)
|
)
|
||||||
|
|
|
@ -16,6 +16,21 @@
|
||||||
- "bridge3"
|
- "bridge3"
|
||||||
- "bridge4"
|
- "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
|
- name: ALWAYS_CHANGES Test adding an ip addr bridge2
|
||||||
mt_ip_address:
|
mt_ip_address:
|
||||||
hostname: "{{ mt_hostname }}"
|
hostname: "{{ mt_hostname }}"
|
||||||
|
@ -137,3 +152,119 @@
|
||||||
register: ip_addr_rem_4
|
register: ip_addr_rem_4
|
||||||
failed_when: ip_addr_rem_4 | changed
|
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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue