zahodi.ansible-mikrotik/library/mt_ip_address.py

119 lines
2.8 KiB
Python
Raw 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
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:
- absent or present
2017-05-25 16:30:10 -07:00
'''
EXAMPLES = '''
- mt_ip_address:
hostname: "{{ inventory_hostname }}"
username: "{{ mt_user }}"
password: "{{ mt_pass }}"
settings:
interface: "ether2"
address: "192.168.88.2/24"
network: "192.168.88.0/24"
state: "present"
comment: "link 3"
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=False, type='dict'),
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
)
idempotent_parameter = None
params = module.params
idempotent_parameter = 'interface'
mt_obj = MikrotikIdempotent(
hostname = params['hostname'],
username = params['username'],
password = params['password'],
state = params['state'],
desired_params = params['settings'],
idempotent_param = idempotent_parameter,
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()