|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +""" |
| 4 | +run commands on netonix api from ansible |
| 5 | +
|
| 6 | +** NEITHER THIS CODE NOR THE AUTHOR IS ASSOCIATED WITH NETONIX® IN ANY WAY.** |
| 7 | +
|
| 8 | +This is free and unencumbered software released into the public domain. |
| 9 | +
|
| 10 | +Anyone is free to copy, modify, publish, use, compile, sell, or |
| 11 | +distribute this software, either in source code form or as a compiled |
| 12 | +binary, for any purpose, commercial or non-commercial, and by any |
| 13 | +means. |
| 14 | +
|
| 15 | +In jurisdictions that recognize copyright laws, the author or authors |
| 16 | +of this software dedicate any and all copyright interest in the |
| 17 | +software to the public domain. We make this dedication for the benefit |
| 18 | +of the public at large and to the detriment of our heirs and |
| 19 | +successors. We intend this dedication to be an overt act of |
| 20 | +relinquishment in perpetuity of all present and future rights to this |
| 21 | +software under copyright law. |
| 22 | +
|
| 23 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 24 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 25 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 26 | +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 27 | +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 28 | +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 29 | +OTHER DEALINGS IN THE SOFTWARE. |
| 30 | +
|
| 31 | +For more information, please refer to <http://unlicense.org/> |
| 32 | +
|
| 33 | +""" |
| 34 | + |
| 35 | +DOCUMENTATION = """ |
| 36 | +--- |
| 37 | +module: netonix_command |
| 38 | +author: "shrank (info@murxs.ch)" |
| 39 | +short_description: run commands Netonix WISP Switch WebAPI |
| 40 | +description: |
| 41 | +- run commands on Netonix WISP Switch WebAPI |
| 42 | +options: |
| 43 | +
|
| 44 | +""" |
| 45 | + |
| 46 | +EXAMPLES = r""" |
| 47 | +
|
| 48 | +REMARK: use this module in combination with the "local_action" module |
| 49 | +
|
| 50 | +- hosts: all |
| 51 | + gather_facts: False |
| 52 | + connection: network_cli |
| 53 | + vars: |
| 54 | + ansible_network_os: ios |
| 55 | +
|
| 56 | + tasks: |
| 57 | + - name: get known devices from mac table |
| 58 | + block: |
| 59 | + - local_action: |
| 60 | + module: netonix_command |
| 61 | + username: "{{ ansible_user}}" |
| 62 | + password: "{{ ansible_password}}" |
| 63 | + target: "{{ inventory_hostname }}" |
| 64 | + command: mac |
| 65 | + register: output |
| 66 | + - local_action: copy content='{{output.output | find_ap(inventory_hostname,'known_devices.list') }}' dest="{{output_dir}}/{{ inventory_hostname }}.mac" |
| 67 | + |
| 68 | + tasks: |
| 69 | + - name: backup running config |
| 70 | + block: |
| 71 | + - local_action: |
| 72 | + module: netonix_command |
| 73 | + username: "{{ ansible_user}}" |
| 74 | + password: "{{ ansible_password}}" |
| 75 | + target: "{{ inventory_hostname }}" |
| 76 | + command: config |
| 77 | + register: output |
| 78 | + - local_action: copy content='{{output.config | to_nice_json(indent=4)}}' dest="{{backup_dir}}/{{ inventory_hostname }}.conf" |
| 79 | + |
| 80 | +""" |
| 81 | + |
| 82 | + |
| 83 | +from ansible.module_utils.basic import AnsibleModule |
| 84 | +from ansible.module_utils.network.common.utils import ComplexList |
| 85 | +from ansible.module_utils.network.common.parsing import Conditional |
| 86 | +from ansible.module_utils.six import string_types |
| 87 | +import netonix_api |
| 88 | +import urllib3 |
| 89 | +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +def main(): |
| 94 | + """main entry point for module execution |
| 95 | + """ |
| 96 | + argument_spec = dict( |
| 97 | + target=dict(type='str', required=True), |
| 98 | + username=dict(type='str', required=True), |
| 99 | + password=dict(type='str', required=True), |
| 100 | + command=dict(type='str', required=True) |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | + module = AnsibleModule(argument_spec=argument_spec, |
| 105 | + supports_check_mode=True) |
| 106 | + |
| 107 | + result = {'changed': False} |
| 108 | + target= module.params["target"] |
| 109 | + password= module.params["password"] |
| 110 | + username= module.params["username"] |
| 111 | + command= module.params["command"] |
| 112 | + warnings = list() |
| 113 | + a=netonix_api.Netonix() |
| 114 | + try: |
| 115 | + a.open(target,username,password) |
| 116 | + except: |
| 117 | + module.fail_json(msg="Unexpected error: " + str(e)) |
| 118 | + return |
| 119 | + if(command=="mac"): |
| 120 | + mac_all={} |
| 121 | + for i in range(3): |
| 122 | + a.getMAC() |
| 123 | + for m in a.mac: |
| 124 | + mac_address=m["MAC"].replace("-",":").lower() |
| 125 | + mac_all[mac_address]=m |
| 126 | + result.update({ |
| 127 | + 'changed': True, |
| 128 | + 'output': mac_all |
| 129 | + }) |
| 130 | + elif(command=="config"): |
| 131 | + try: |
| 132 | + a.getConfig() |
| 133 | + except: |
| 134 | + module.fail_json(msg="Unexpected error: " + str(e)) |
| 135 | + return |
| 136 | + result.update({ |
| 137 | + 'changed': True, |
| 138 | + 'config': a.config |
| 139 | + }) |
| 140 | + else: |
| 141 | + module.fail_json(msg="Unknown command: " + command) |
| 142 | + return |
| 143 | + module.exit_json(**result) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == '__main__': |
| 147 | + main() |
0 commit comments