zahodi.ansible-mikrotik/library/mt_ip_address.py

166 lines
4.4 KiB
Python
Raw Permalink Normal View History

2017-05-25 16:30:10 -07:00
DOCUMENTATION = '''
module: mt_ip_address
author:
- "Valentin Gurmeza"
- "Shaun Smiley"
- "Antoni Matamalas"
2017-05-25 16:30:10 -07:00
version_added: "2.3"
short_description: Manage mikrotik /ip/addresses
requirements:
- mt_api
2017-05-25 16:30:10 -07:00
description:
- Manage addresses on interfaces
2017-05-25 16:30:10 -07:00
options:
hostname:
description:
- hotstname of mikrotik router
required: True
2017-05-25 16:30:10 -07:00
username:
description:
- username used to connect to mikrotik router
required: True
2017-05-25 16:30:10 -07:00
password:
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:
2017-05-25 16:30:10 -07:00
description:
- 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:
- 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 = '''
# 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 }}"
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"
2017-05-25 16:30:10 -07: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),
password = dict(required=True, no_log=True),
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'
),
),
supports_check_mode=True
2017-05-25 16:30:10 -07:00
)
params = module.params
mt_obj = MikrotikIdempotent(
hostname = params['hostname'],
username = params['username'],
password = params['password'],
state = params['state'],
desired_params = params['settings'],
idempotent_param = params['idempotent'],
api_path = '/ip/address',
check_mode = module.check_mode
)
2017-05-25 16:30:10 -07:00
# exit if login failed
if not mt_obj.login_success:
2017-05-25 16:30:10 -07:00
module.fail_json(
msg = mt_obj.failed_msg
2017-05-25 16:30:10 -07:00
)
# add, remove or edit things
mt_obj.sync_state()
2017-05-25 16:30:10 -07:00
if mt_obj.failed:
module.fail_json(
msg = mt_obj.failed_msg
2017-05-25 16:30:10 -07: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()