zahodi.ansible-mikrotik/library/mt_system_scheduler.py

139 lines
3.3 KiB
Python
Raw Normal View History

2017-05-25 16:30:10 -07:00
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
module: mt_system_scheduler
author:
- "Valentin Gurmeza"
version_added: "2.3"
short_description: Manage mikrotik system scheduler
requirements:
- mt_api
description:
- add, remove, or modify a system scheduler task
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
state:
description:
- scheduler task present or absent
required: True
choices:
- present
- absent
comment:
description:
- task comment
interval:
description:
- interval between two script executions, if time interval is set to zero, the script is only executed at its start time, otherwise it is executed repeatedly at the time interval is specified
name:
description:
- name of the task
required: True
on_event:
description:
- name of the script to execute.
start_date:
description:
- date of the first script execution
start_time:
description:
- time of the first script execution
policy:
description:
- policy
'''
EXAMPLES = '''
- mt_system_scheduler:
hostname: "{{ inventory_hostname }}"
username: "{{ mt_user }}"
password: "{{ mt_pass }}"
state: present
name: test_by_ansible
comment: ansible_test
on_event: put "hello"
'''
2018-01-24 15:15:44 -08: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(
2017-06-05 16:13:41 -07:00
argument_spec=dict(
2018-01-24 15:15:44 -08:00
hostname=dict(required=True),
username=dict(required=True),
password=dict(required=True, no_log=True),
settings=dict(required=True, type='dict'),
parameter = dict(
required = True,
choices = ['scheduler'],
type = 'str'
2017-06-05 16:13:41 -07:00
),
2018-01-24 15:15:44 -08:00
state = dict(
required = False,
choices = ['present', 'absent'],
type = 'str'
)
2017-06-05 16:13:41 -07:00
),
supports_check_mode=True
2017-05-25 16:30:10 -07:00
)
2018-01-24 15:15:44 -08:00
params = module.params
idempotent_parameter = 'name'
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 = '/system/' + str(params['parameter']),
check_mode = module.check_mode
)
2017-05-25 16:30:10 -07:00
2018-01-24 15:15:44 -08:00
# exit if login failed
if not mt_obj.login_success:
2017-05-25 16:30:10 -07:00
module.fail_json(
2018-01-24 15:15:44 -08:00
msg = mt_obj.failed_msg
2017-05-25 16:30:10 -07:00
)
2018-01-24 15:15:44 -08:00
# add, remove or edit things
mt_obj.sync_state()
2017-05-25 16:30:10 -07:00
2018-01-24 15:15:44 -08:00
if mt_obj.failed:
module.fail_json(
msg = mt_obj.failed_msg
)
elif mt_obj.changed:
2017-05-25 16:30:10 -07:00
module.exit_json(
2018-01-24 15:15:44 -08:00
failed=False,
changed=True,
msg=mt_obj.changed_msg,
diff={ "prepared": {
"old": mt_obj.old_params,
"new": mt_obj.new_params,
}},
2017-05-25 16:30:10 -07:00
)
else:
module.exit_json(
2018-01-24 15:15:44 -08:00
failed=False,
changed=False,
msg=params['settings'],
2017-05-25 16:30:10 -07:00
)
2018-01-24 15:15:44 -08:00
2017-05-25 16:30:10 -07:00
if __name__ == '__main__':
main()