mirror of
https://github.com/zahodi/ansible-mikrotik.git
synced 2025-07-24 04:44:46 +02:00
Merge pull request #35 from computercolin/ppp_extensions_and_fixes
Added VPN extensions to existing modules
This commit is contained in:
commit
fb9c4eddd7
5 changed files with 319 additions and 3 deletions
|
@ -37,7 +37,7 @@ EXAMPLES = '''
|
||||||
hostname: "{{ inventory_hostname }}"
|
hostname: "{{ inventory_hostname }}"
|
||||||
username: "{{ mt_user }}"
|
username: "{{ mt_user }}"
|
||||||
password: "{{ mt_pass }}"
|
password: "{{ mt_pass }}"
|
||||||
name: /system/backup/save
|
command: /system/backup/save
|
||||||
command_arguments:
|
command_arguments:
|
||||||
name: ansible_test
|
name: ansible_test
|
||||||
password: 123
|
password: 123
|
||||||
|
|
105
library/mt_ppp_profile.py
Normal file
105
library/mt_ppp_profile.py
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
module: mt_ppp_profile
|
||||||
|
author:
|
||||||
|
- "Colin Zwiebel"
|
||||||
|
version_added: "2.4.1"
|
||||||
|
short_description: Manage mikrotik ppp profiles
|
||||||
|
requirements:
|
||||||
|
- mt_api
|
||||||
|
description:
|
||||||
|
- Generic mikrotik ppp profile management module.
|
||||||
|
options:
|
||||||
|
hostname:
|
||||||
|
description:
|
||||||
|
- hostname 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
|
||||||
|
settings:
|
||||||
|
description:
|
||||||
|
- All Mikrotik compatible parameters for the ppp-profile endpoint.
|
||||||
|
Any yes/no values must be enclosed in double quotes
|
||||||
|
state:
|
||||||
|
description:
|
||||||
|
- absent or present
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
- mt_ppp_profile:
|
||||||
|
hostname: "{{ inventory_hostname }}"
|
||||||
|
username: "{{ mt_user }}"
|
||||||
|
password: "{{ mt_pass }}"
|
||||||
|
state: present
|
||||||
|
settings:
|
||||||
|
name: example-profile
|
||||||
|
local-address: 1.2.3.4
|
||||||
|
change-tcp-mss: "y"
|
||||||
|
use-compression: "y"
|
||||||
|
use-encryption: required
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible.module_utils.mt_common import clean_params, MikrotikIdempotent
|
||||||
|
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'),
|
||||||
|
state = dict(
|
||||||
|
required = False,
|
||||||
|
choices = ['present', 'absent'],
|
||||||
|
type = 'str'
|
||||||
|
),
|
||||||
|
),
|
||||||
|
supports_check_mode=True
|
||||||
|
)
|
||||||
|
|
||||||
|
params = module.params
|
||||||
|
mt_obj = MikrotikIdempotent(
|
||||||
|
hostname = params['hostname'],
|
||||||
|
username = params['username'],
|
||||||
|
password = params['password'],
|
||||||
|
state = params['state'],
|
||||||
|
desired_params = params['settings'],
|
||||||
|
idempotent_param = 'name',
|
||||||
|
api_path = '/ppp/profile',
|
||||||
|
check_mode = module.check_mode
|
||||||
|
)
|
||||||
|
|
||||||
|
mt_obj.sync_state()
|
||||||
|
|
||||||
|
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='',
|
||||||
|
msg=params['settings'],
|
||||||
|
)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
104
library/mt_ppp_secret.py
Normal file
104
library/mt_ppp_secret.py
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
module: mt_ppp_secret
|
||||||
|
author:
|
||||||
|
- "Colin Zwiebel"
|
||||||
|
version_added: "2.4.1"
|
||||||
|
short_description: Manage mikrotik ppp secrets (vpn users)
|
||||||
|
requirements:
|
||||||
|
- mt_api
|
||||||
|
description:
|
||||||
|
- Generic mikrotik ppp secret module.
|
||||||
|
options:
|
||||||
|
hostname:
|
||||||
|
description:
|
||||||
|
- hostname 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
|
||||||
|
settings:
|
||||||
|
description:
|
||||||
|
- All Mikrotik compatible parameters for the ppp secrets endpoint.
|
||||||
|
Any yes/no values must be enclosed in double quotes
|
||||||
|
state:
|
||||||
|
description:
|
||||||
|
- absent or present
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
- mt_ppp_secret:
|
||||||
|
hostname: "{{ inventory_hostname }}"
|
||||||
|
username: "{{ mt_user }}"
|
||||||
|
password: "{{ mt_pass }}"
|
||||||
|
state: present
|
||||||
|
settings:
|
||||||
|
name: user2
|
||||||
|
password: pass2
|
||||||
|
service: ovpn
|
||||||
|
remote-address: 1.2.3.4
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible.module_utils.mt_common import clean_params, MikrotikIdempotent
|
||||||
|
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'),
|
||||||
|
state = dict(
|
||||||
|
required = False,
|
||||||
|
choices = ['present', 'absent'],
|
||||||
|
type = 'str'
|
||||||
|
),
|
||||||
|
),
|
||||||
|
supports_check_mode=True
|
||||||
|
)
|
||||||
|
|
||||||
|
params = module.params
|
||||||
|
mt_obj = MikrotikIdempotent(
|
||||||
|
hostname = params['hostname'],
|
||||||
|
username = params['username'],
|
||||||
|
password = params['password'],
|
||||||
|
state = params['state'],
|
||||||
|
desired_params = params['settings'],
|
||||||
|
idempotent_param = 'name',
|
||||||
|
api_path = '/ppp/secret',
|
||||||
|
check_mode = module.check_mode
|
||||||
|
)
|
||||||
|
|
||||||
|
mt_obj.sync_state()
|
||||||
|
|
||||||
|
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'],
|
||||||
|
)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
107
library/mt_ppp_server.py
Normal file
107
library/mt_ppp_server.py
Normal file
|
@ -0,0 +1,107 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
module: mt_ppp_server
|
||||||
|
author:
|
||||||
|
- "Colin Zwiebel"
|
||||||
|
version_added: "2.3.1"
|
||||||
|
short_description: Manage mikrotik ppp servers
|
||||||
|
requirements:
|
||||||
|
- mt_api
|
||||||
|
description:
|
||||||
|
- Manage ppp servers and their settings.
|
||||||
|
options:
|
||||||
|
hostname:
|
||||||
|
description:
|
||||||
|
- hostname 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
|
||||||
|
server_type:
|
||||||
|
description:
|
||||||
|
- VPN server type to manage
|
||||||
|
required: True
|
||||||
|
options:
|
||||||
|
- l2tp
|
||||||
|
- ovpn
|
||||||
|
- pptp
|
||||||
|
- sstp
|
||||||
|
settings:
|
||||||
|
description:
|
||||||
|
- All Mikrotik compatible parameters for this type of vpn server.
|
||||||
|
Any yes/no values must be enclosed in double quotes
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
- mt_ppp_server:
|
||||||
|
hostname: "{{ inventory_hostname }}"
|
||||||
|
username: "{{ mt_user }}"
|
||||||
|
password: "{{ mt_pass }}"
|
||||||
|
server_type: pptp
|
||||||
|
settings:
|
||||||
|
disabled: no
|
||||||
|
max-mtu: 1420
|
||||||
|
authentication: mschap2
|
||||||
|
'''
|
||||||
|
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.mt_common import clean_params, MikrotikIdempotent
|
||||||
|
|
||||||
|
|
||||||
|
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'),
|
||||||
|
server_type = dict(
|
||||||
|
required = True,
|
||||||
|
choices = ['l2tp', 'ovpn', 'pptp', 'sstp'],
|
||||||
|
type = 'str'
|
||||||
|
),
|
||||||
|
),
|
||||||
|
supports_check_mode=True
|
||||||
|
)
|
||||||
|
|
||||||
|
params = module.params
|
||||||
|
mt_obj = MikrotikIdempotent(
|
||||||
|
hostname = params['hostname'],
|
||||||
|
username = params['username'],
|
||||||
|
password = params['password'],
|
||||||
|
state = None,
|
||||||
|
desired_params = params['settings'],
|
||||||
|
idempotent_param = None,
|
||||||
|
api_path = '/interface/{}-server/server'.format(params['server_type']),
|
||||||
|
check_mode = module.check_mode
|
||||||
|
)
|
||||||
|
|
||||||
|
mt_obj.sync_state()
|
||||||
|
|
||||||
|
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'],
|
||||||
|
)
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
|
@ -90,8 +90,8 @@ class MikrotikIdempotent():
|
||||||
try:
|
try:
|
||||||
self.mk.login()
|
self.mk.login()
|
||||||
self.login_success = True
|
self.login_success = True
|
||||||
except:
|
except Exception as e:
|
||||||
self.failed_msg = "Could not log into Mikrotik device." + " Check the username and password.",
|
self.failed_msg = "Could not log into Mikrotik device." + " Check the username and password. Exception {} - {}".format(type(e), e),
|
||||||
else:
|
else:
|
||||||
self.failed_msg = "Could not access RouterOS api." + " Verify API service is enabled and not blocked by firewall.",
|
self.failed_msg = "Could not access RouterOS api." + " Verify API service is enabled and not blocked by firewall.",
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue