add ovpn-client to the interfaces module

add check mode to interface_wireless module
up date checks
This commit is contained in:
Valentin Gurmeza 2017-06-05 15:02:40 -07:00
parent 220f44766b
commit 297f3228bc
3 changed files with 153 additions and 113 deletions

View file

@ -50,70 +50,69 @@ EXAMPLES = '''
''' '''
from mt_common import clean_params, MikrotikIdempotent
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from mt_common import clean_params, MikrotikIdempotent
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
hostname = dict(required=True), hostname = dict(required=True),
username = dict(required=True), username = dict(required=True),
password = dict(required=True), password = dict(required=True),
settings = dict(required=False, type='dict'), settings = dict(required=False, type='dict'),
parameter = dict( parameter = dict(
required = True, required = True,
choices = ['security-profiles'], choices = ['security-profiles'],
type = 'str' type = 'str'
), ),
state = dict( state = dict(
required = False, required = False,
choices = ['present', 'absent'], choices = ['present', 'absent'],
type = 'str' type = 'str'
), ),
) ),
supports_check_mode=True
)
idempotent_parameter = None
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 = '/interface/wireless/' + str(params['parameter']),
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:
idempotent_parameter = None module.exit_json(
params = module.params failed=False,
changed=False,
idempotent_parameter = 'name' msg=params['settings'],
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 = '/interface/wireless/' + str(params['parameter']),
) )
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__': if __name__ == '__main__':
main() main()

View file

@ -66,7 +66,7 @@ def main():
settings=dict(required=True, type='dict'), settings=dict(required=True, type='dict'),
parameter = dict( parameter = dict(
required = True, required = True,
choices = ['ethernet', 'vlan'], choices = ['ethernet', 'vlan', 'ovpn-client'],
type = 'str' type = 'str'
), ),
state = dict( state = dict(
@ -89,7 +89,7 @@ def main():
desired_params = params['settings'], desired_params = params['settings'],
idempotent_param = idempotent_parameter, idempotent_param = idempotent_parameter,
api_path = '/interface/' + str(params['parameter']), api_path = '/interface/' + str(params['parameter']),
check_mode = module.check_mode, check_mode = module.check_mode
) )
# exit if login failed # exit if login failed

View file

@ -422,68 +422,97 @@
################### ###################
- block: - block:
- name: Test adding ovpn-client - name: Test adding ovpn-client
mt_interface_ovpn_client: mt_interfaces:
hostname: "{{ mt_hostname }}" hostname: "{{ mt_hostname }}"
username: "{{ mt_user }}" username: "{{ mt_user }}"
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
state: present state: present
comment: "ansible test 1" parameter: ovpn-client
user: ansible_admin settings:
connect_to: 192.168.50.170 comment: "ansible test 1"
name: ansible_test user: ansible_admin
vpn_password: 'blablabla' connect-to: 192.168.50.170
name: ansible_test
password: 'blablabla'
tags: vpn-client-test
- name: NEVER_CHANGES Test adding duplicate ovpn-client - name: NEVER_CHANGES Test adding duplicate ovpn-client
mt_interface_ovpn_client: mt_interfaces:
hostname: "{{ mt_hostname }}" hostname: "{{ mt_hostname }}"
username: "{{ mt_user }}" username: "{{ mt_user }}"
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
state: present state: present
comment: "ansible test 1" parameter: ovpn-client
user: ansible_admin settings:
connect_to: 192.168.50.170 comment: "ansible test 1"
name: ansible_test user: ansible_admin
vpn_password: 'blablabla' connect-to: 192.168.50.170
name: ansible_test
password: 'blablabla'
register: ovpn_client_test_1_add register: ovpn_client_test_1_add
failed_when: ( ovpn_client_test_1_add | changed ) failed_when: (
not ansible_check_mode
) and (
( ovpn_client_test_1_add | changed )
)
tags: vpn-client-test
- name: ALWAYS_CHANGES Test editing an existing ovpn-client item (change address) - name: ALWAYS_CHANGES Test editing an existing ovpn-client item (change address)
mt_interface_ovpn_client: mt_interfaces:
hostname: "{{ mt_hostname }}" hostname: "{{ mt_hostname }}"
username: "{{ mt_user }}" username: "{{ mt_user }}"
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
state: present state: present
comment: "ansible test 1" parameter: ovpn-client
user: ansible_admin settings:
connect_to: 192.168.50.171 comment: "ansible test 1"
auth: "null" user: ansible_admin
name: ansible_test connect-to: 192.168.50.171
vpn_password: 'bar' auth: "null"
name: ansible_test
password: 'bar'
register: ovpn_client_test_1_edit register: ovpn_client_test_1_edit
failed_when: not ( ovpn_client_test_1_edit | changed ) failed_when: (
not ansible_check_mode
) and (
not ( ovpn_client_test_1_edit | changed )
)
tags: vpn-client-test
- name: ALWAYS_CHANGES Test adding a second ovpn-client to later remove - name: ALWAYS_CHANGES Test adding a second ovpn-client to later remove
mt_interface_ovpn_client: mt_interfaces:
hostname: "{{ mt_hostname }}" hostname: "{{ mt_hostname }}"
username: "{{ mt_user }}" username: "{{ mt_user }}"
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
comment: "ansible test 2" state: present
state: present parameter: ovpn-client
user: ansible_admin settings:
connect_to: 192.168.52.111 user: ansible_admin
name: ansible_test2 comment: "ansible test 2"
connect_to: 192.168.52.111
name: ansible_test2
register: ovpn_client_test_2_add register: ovpn_client_test_2_add
failed_when: not ( ovpn_client_test_2_add | changed ) failed_when: (
not ansible_check_mode
) and (
not ( ovpn_client_test_2_add | changed )
)
- name: ALWAYS_CHANGES Test remove ovpn-client - name: ALWAYS_CHANGES Test remove ovpn-client
mt_interface_ovpn_client: mt_interfaces:
hostname: "{{ mt_hostname }}" hostname: "{{ mt_hostname }}"
username: "{{ mt_user }}" username: "{{ mt_user }}"
password: "{{ mt_pass }}" password: "{{ mt_pass }}"
state: absent state: absent
name: ansible_test2 parameter: ovpn-client
settings:
name: ansible_test2
register: ovpn_client_test_2_rem register: ovpn_client_test_2_rem
failed_when: not ( ovpn_client_test_2_rem | changed ) failed_when: (
not ansible_check_mode
) and (
not ( ovpn_client_test_2_rem | changed )
)
tags: ovpn-client tags: ovpn-client
################### ###################
@ -2163,7 +2192,11 @@
name: test1 name: test1
supplicant-identity: test supplicant-identity: test
register: security_prof_idem register: security_prof_idem
failed_when: ( security_prof_idem | changed ) failed_when: (
not ansible_check_mode
) and (
( security_prof_idem | changed )
)
- name: ALWAYS_CHANGES add security-profiles item, check idempotency - name: ALWAYS_CHANGES add security-profiles item, check idempotency
mt_interface_wireless: mt_interface_wireless:
@ -2177,7 +2210,11 @@
supplicant-identity: test supplicant-identity: test
management-protection: allowed management-protection: allowed
register: security_prof_edit register: security_prof_edit
failed_when: not ( security_prof_edit | changed ) failed_when: (
not ansible_check_mode
) and (
not ( security_prof_edit | changed )
)
- name: ALWAYS_CHANGES rem security-profiles item - name: ALWAYS_CHANGES rem security-profiles item
mt_interface_wireless: mt_interface_wireless:
@ -2189,6 +2226,10 @@
settings: settings:
name: test1 name: test1
register: security_prof_rem register: security_prof_rem
failed_when: not ( security_prof_rem | changed ) failed_when: (
not ansible_check_mode
) and (
not ( security_prof_rem | changed )
)
tags: interface-wireless tags: interface-wireless