|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# (c) 2017 Apstra Inc, <[email protected]> |
| 4 | +# |
| 5 | +# This file is part of Ansible |
| 6 | +# |
| 7 | +# Ansible is free software: you can redistribute it and/or modify |
| 8 | +# it under the terms of the GNU General Public License as published by |
| 9 | +# the Free Software Foundation, either version 3 of the License, or |
| 10 | +# (at your option) any later version. |
| 11 | +# |
| 12 | +# Ansible is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +# GNU General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU General Public License |
| 18 | +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 19 | +# |
| 20 | + |
| 21 | +ANSIBLE_METADATA = {'status': ['preview'], |
| 22 | + 'supported_by': 'community', |
| 23 | + 'version': '1.0'} |
| 24 | + |
| 25 | +DOCUMENTATION = ''' |
| 26 | +--- |
| 27 | +module: aos_login |
| 28 | +author: [email protected] (@jeremyschulman) |
| 29 | +version_added: "2.3" |
| 30 | +short_description: Login to AOS server for session token |
| 31 | +description: |
| 32 | + - Obtain the AOS server session token by providing the required |
| 33 | + username and password credentials. Upon successful authentication, |
| 34 | + this module will return the session-token that is required by all |
| 35 | + subsequent AOS module usage. On success the module will automatically populate |
| 36 | + ansible facts with the variable I(aos_session) |
| 37 | + This module is not idempotent and do not support check mode. |
| 38 | +requirements: |
| 39 | + - "aos-pyez >= 0.6.0" |
| 40 | +options: |
| 41 | + server: |
| 42 | + description: |
| 43 | + - Address of the AOS Server on which you want to open a connection. |
| 44 | + required: true |
| 45 | + port: |
| 46 | + description: |
| 47 | + - Port number to use when connecting to the AOS server. |
| 48 | + default: 8888 |
| 49 | + user: |
| 50 | + description: |
| 51 | + - Login username to use when connecting to the AOS server. |
| 52 | + default: admin |
| 53 | + passwd: |
| 54 | + description: |
| 55 | + - Password to use when connecting to the AOS server. |
| 56 | + default: admin |
| 57 | +''' |
| 58 | + |
| 59 | +EXAMPLES = ''' |
| 60 | +
|
| 61 | +- name: Create a session with the AOS-server |
| 62 | + aos_login: |
| 63 | + server: "{{ inventory_hostname }}" |
| 64 | + user: admin |
| 65 | + passwd: admin |
| 66 | +
|
| 67 | +- name: Use the newly created session (register is not mandatory) |
| 68 | + aos_ip_pool: |
| 69 | + session: "{{ aos_session }}" |
| 70 | + name: my_ip_pool |
| 71 | + state: present |
| 72 | +''' |
| 73 | + |
| 74 | +RETURNS = ''' |
| 75 | +aos_session: |
| 76 | + description: Authenticated session information |
| 77 | + returned: always |
| 78 | + type: dict |
| 79 | + sample: { 'url': <str>, 'headers': {...} } |
| 80 | +''' |
| 81 | + |
| 82 | +from ansible.module_utils.basic import AnsibleModule |
| 83 | + |
| 84 | +try: |
| 85 | + from apstra.aosom.session import Session |
| 86 | + import apstra.aosom.exc as aosExc |
| 87 | + |
| 88 | + HAS_AOS_PYEZ = True |
| 89 | +except ImportError: |
| 90 | + HAS_AOS_PYEZ = False |
| 91 | + |
| 92 | +def aos_login(module): |
| 93 | + |
| 94 | + mod_args = module.params |
| 95 | + |
| 96 | + aos = Session(server=mod_args['server'], port=mod_args['port'], |
| 97 | + user=mod_args['user'], passwd=mod_args['passwd']) |
| 98 | + |
| 99 | + try: |
| 100 | + aos.login() |
| 101 | + except aosExc.LoginServerUnreachableError: |
| 102 | + module.fail_json( |
| 103 | + msg="AOS-server [%s] API not available/reachable, check server" % aos.server) |
| 104 | + |
| 105 | + except aosExc.LoginAuthError: |
| 106 | + module.fail_json(msg="AOS-server login credentials failed") |
| 107 | + |
| 108 | + module.exit_json(changed=False, |
| 109 | + ansible_facts=dict( aos_session=dict(url=aos.api.url, headers=aos.api.headers)), |
| 110 | + aos_session=dict(url=aos.api.url, headers=aos.api.headers)) |
| 111 | + |
| 112 | +def main(): |
| 113 | + module = AnsibleModule( |
| 114 | + argument_spec=dict( |
| 115 | + server=dict(required=True), |
| 116 | + port=dict(default='8888'), |
| 117 | + user=dict(default='admin'), |
| 118 | + passwd=dict(default='admin', no_log=True))) |
| 119 | + |
| 120 | + if not HAS_AOS_PYEZ: |
| 121 | + module.fail_json(msg='aos-pyez is not installed. Please see details ' |
| 122 | + 'here: https://github.com/Apstra/aos-pyez') |
| 123 | + |
| 124 | + aos_login(module) |
| 125 | + |
| 126 | +if __name__ == '__main__': |
| 127 | + main() |
0 commit comments