2017-05-25 16:30:10 -07:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
module: mt_ip_address
|
|
|
|
author:
|
|
|
|
- "Valentin Gurmeza"
|
|
|
|
- "Shaun Smiley"
|
2017-12-08 14:00:17 +01:00
|
|
|
- "Antoni Matamalas"
|
2017-05-25 16:30:10 -07:00
|
|
|
version_added: "2.3"
|
|
|
|
short_description: Manage mikrotik /ip/addresses
|
|
|
|
requirements:
|
2017-12-08 14:00:17 +01:00
|
|
|
- mt_api
|
2017-05-25 16:30:10 -07:00
|
|
|
description:
|
2017-12-08 14:00:17 +01:00
|
|
|
- Manage addresses on interfaces
|
2017-05-25 16:30:10 -07:00
|
|
|
options:
|
|
|
|
hostname:
|
|
|
|
description:
|
2017-12-08 14:00:17 +01:00
|
|
|
- hotstname of mikrotik router
|
|
|
|
required: True
|
2017-05-25 16:30:10 -07:00
|
|
|
username:
|
|
|
|
description:
|
2017-12-08 14:00:17 +01:00
|
|
|
- username used to connect to mikrotik router
|
|
|
|
required: True
|
2017-05-25 16:30:10 -07:00
|
|
|
password:
|
|
|
|
description:
|
2017-12-08 14:00:17 +01:00
|
|
|
- password used for authentication to mikrotik router
|
|
|
|
required: True
|
2018-03-11 19:30:19 +01:00
|
|
|
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
|
2017-12-08 14:00:17 +01:00
|
|
|
settings:
|
2017-05-25 16:30:10 -07:00
|
|
|
description:
|
2017-12-08 14:00:17 +01:00
|
|
|
- All Mikrotik compatible parameters for this particular endpoint.
|
|
|
|
Any yes/no values must be enclosed in double quotes
|
|
|
|
required: True
|
2017-05-25 16:30:10 -07:00
|
|
|
state:
|
|
|
|
description:
|
2018-03-11 19:30:19 +01:00
|
|
|
- Depending on the idempotent option, it will define the status of the IP
|
|
|
|
on an interface
|
|
|
|
required: False
|
|
|
|
default: present
|
2017-05-25 16:30:10 -07:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2018-03-11 19:30:19 +01:00
|
|
|
# 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"
|
|
|
|
comment: "link 3"
|
|
|
|
|
|
|
|
# Assign IP to the interface. If the interface has any previous IP, it will be
|
|
|
|
# replaced by this one.
|
2017-05-25 16:30:10 -07:00
|
|
|
- mt_ip_address:
|
|
|
|
hostname: "{{ inventory_hostname }}"
|
|
|
|
username: "{{ mt_user }}"
|
|
|
|
password: "{{ mt_pass }}"
|
2018-03-11 19:30:19 +01:00
|
|
|
idempotent: "interface"
|
|
|
|
state: "present"
|
2017-12-08 14:00:17 +01:00
|
|
|
settings:
|
|
|
|
interface: "ether2"
|
|
|
|
address: "192.168.88.2/24"
|
|
|
|
network: "192.168.88.0/24"
|
|
|
|
comment: "link 3"
|
2018-03-11 19:30:19 +01:00
|
|
|
|
|
|
|
# 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"
|
2017-05-25 16:30:10 -07:00
|
|
|
'''
|
|
|
|
|
2017-12-08 14:00:17 +01:00
|
|
|
from ansible.module_utils.mt_common import clean_params, MikrotikIdempotent
|
2017-05-25 16:30:10 -07:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
hostname = dict(required=True),
|
|
|
|
username = dict(required=True),
|
2017-12-22 10:57:19 +01:00
|
|
|
password = dict(required=True, no_log=True),
|
2018-03-11 19:30:19 +01:00
|
|
|
settings = dict(required=True, type='dict'),
|
|
|
|
idempotent = dict(
|
|
|
|
required = False,
|
|
|
|
default = 'address',
|
|
|
|
choices = ['address', 'interface'],
|
|
|
|
type = 'str'
|
|
|
|
),
|
2017-05-25 16:30:10 -07:00
|
|
|
state = dict(
|
|
|
|
required = False,
|
|
|
|
default = "present",
|
|
|
|
choices = ['present', 'absent'],
|
|
|
|
type = 'str'
|
|
|
|
),
|
2017-12-08 14:00:17 +01:00
|
|
|
),
|
|
|
|
supports_check_mode=True
|
2017-05-25 16:30:10 -07:00
|
|
|
)
|
|
|
|
|
2017-12-08 14:00:17 +01:00
|
|
|
params = module.params
|
|
|
|
mt_obj = MikrotikIdempotent(
|
|
|
|
hostname = params['hostname'],
|
|
|
|
username = params['username'],
|
|
|
|
password = params['password'],
|
|
|
|
state = params['state'],
|
|
|
|
desired_params = params['settings'],
|
2018-03-11 19:30:19 +01:00
|
|
|
idempotent_param = params['idempotent'],
|
2017-12-08 14:00:17 +01:00
|
|
|
api_path = '/ip/address',
|
|
|
|
check_mode = module.check_mode
|
|
|
|
)
|
2017-05-25 16:30:10 -07:00
|
|
|
|
2017-12-08 14:00:17 +01:00
|
|
|
# exit if login failed
|
|
|
|
if not mt_obj.login_success:
|
2017-05-25 16:30:10 -07:00
|
|
|
module.fail_json(
|
2017-12-08 14:00:17 +01:00
|
|
|
msg = mt_obj.failed_msg
|
2017-05-25 16:30:10 -07:00
|
|
|
)
|
|
|
|
|
2017-12-08 14:00:17 +01:00
|
|
|
# add, remove or edit things
|
|
|
|
mt_obj.sync_state()
|
2017-05-25 16:30:10 -07:00
|
|
|
|
2017-12-08 14:00:17 +01:00
|
|
|
if mt_obj.failed:
|
|
|
|
module.fail_json(
|
|
|
|
msg = mt_obj.failed_msg
|
2017-05-25 16:30:10 -07:00
|
|
|
)
|
2017-12-08 14:00:17 +01:00
|
|
|
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'],
|
|
|
|
)
|
2017-05-25 16:30:10 -07:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
2017-12-08 14:00:17 +01:00
|
|
|
|