zahodi.ansible-mikrotik/library/mt_tool.py

122 lines
2.8 KiB
Python
Raw Permalink Normal View History

2017-05-25 16:30:10 -07:00
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
module: mt_tool
author:
- "Valentin Gurmeza"
version_added: "2.4"
short_description: Manage mikrotik tool endpoints
requirements:
- mt_api
description:
- Generic mikrotik tool module.
options:
hostname:
description:
- hotstname of mikrotik router
required: True
username:
description:
- username used to connect to mikrotik router
required: True
password:
description:
- password used for authentication to mikrotik router
required: True
parameter:
description:
- sub endpoint for mikrotik tool
required: True
options:
- netwatch
- e-mail
settings:
description:
- All Mikrotik compatible parameters for this particular endpoint.
Any yes/no values must be enclosed in double quotes
state:
description:
- absent or present
'''
EXAMPLES = '''
- mt_tool:
hostname: "{{ inventory_hostname }}"
username: "{{ mt_user }}"
password: "{{ mt_pass }}"
parameter: e-mail
settings:
address: 192.168.1.1
from: foo@bar.com
'''
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():
2017-06-05 12:22:26 -07:00
module = AnsibleModule(
argument_spec = dict(
hostname = dict(required=True),
username = dict(required=True),
password = dict(required=True, no_log=True),
2017-06-05 12:22:26 -07:00
settings = dict(required=False, type='dict'),
parameter = dict(
required = True,
choices = ['e-mail', 'netwatch'],
type = 'str'
),
state = dict(
required = False,
choices = ['present', 'absent'],
type = 'str'
),
),
supports_check_mode=True
)
2017-05-25 16:30:10 -07:00
2017-06-05 12:22:26 -07:00
idempotent_parameter = None
params = module.params
2017-05-25 16:30:10 -07:00
2017-06-05 12:22:26 -07:00
if params['parameter'] == 'netwatch':
idempotent_parameter = 'host'
2017-05-25 16:30:10 -07:00
# clean_params(params['settings'])
2017-06-05 12:22:26 -07:00
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 = '/tool/' + str(params['parameter']),
check_mode = module.check_mode
2017-05-25 16:30:10 -07:00
2017-06-05 12:22:26 -07:00
)
2017-05-25 16:30:10 -07:00
2017-06-05 12:22:26 -07:00
mt_obj.sync_state()
2017-05-25 16:30:10 -07:00
2017-06-05 12:22:26 -07:00
if mt_obj.failed:
module.fail_json(
msg=mt_obj.failed_msg
)
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=params['settings'],
)
2017-05-25 16:30:10 -07:00
if __name__ == '__main__':
main()