aruba: WIP: restore config using REST API
parent
89d22feba1
commit
01ece9dbcb
@ -0,0 +1,83 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import base64
|
||||||
|
import time
|
||||||
|
import functools
|
||||||
|
import urllib.parse
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from ansible.errors import AnsibleActionFail
|
||||||
|
from ansible.plugins.action import ActionBase
|
||||||
|
|
||||||
|
|
||||||
|
class Aruba:
|
||||||
|
def __init__(self, base_url):
|
||||||
|
self._session = requests.session()
|
||||||
|
self._base_url = base_url
|
||||||
|
|
||||||
|
def _url(self, url):
|
||||||
|
return urllib.parse.urljoin(self._base_url, url)
|
||||||
|
|
||||||
|
def login(self, username, password):
|
||||||
|
response = self._session.post(
|
||||||
|
self._url("/rest/v4/login-sessions"),
|
||||||
|
json={"userName": username, "password": password},
|
||||||
|
)
|
||||||
|
if response.status_code != requests.codes.created:
|
||||||
|
raise AnsibleActionFail("Login failed")
|
||||||
|
|
||||||
|
def logout(self):
|
||||||
|
response = self._session.delete(self._url("/rest/v4/login-sessions"))
|
||||||
|
if response.status_code != requests.codes.no_content:
|
||||||
|
raise AnsibleActionFail("Logout failed")
|
||||||
|
|
||||||
|
def restore(self, config):
|
||||||
|
response = self._session.post(
|
||||||
|
self._url(
|
||||||
|
"/rest/v4/system/config/cfg_restore/payload"
|
||||||
|
),
|
||||||
|
json={
|
||||||
|
"config_base64_encoded": base64.b64encode(config.encode()),
|
||||||
|
"is_forced_reboot_enabled": True,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if response.status_code != requests.codes.accepted:
|
||||||
|
raise AnsibleActionFail("Restore failed")
|
||||||
|
|
||||||
|
response = self._session.get(
|
||||||
|
self._url(
|
||||||
|
"/rest/v4/system/config/cfg_restore/payload/status"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
print(response.text)
|
||||||
|
|
||||||
|
|
||||||
|
class ActionModule(ActionBase):
|
||||||
|
|
||||||
|
_VALID_ARGS = frozenset(("username", "password", "config", "url"))
|
||||||
|
|
||||||
|
def _require_arg(self, name):
|
||||||
|
try:
|
||||||
|
return self._task.args[name]
|
||||||
|
except KeyError:
|
||||||
|
raise AnsibleActionFail("Missing argument: {}".format(name))
|
||||||
|
|
||||||
|
def run(self, tmp=None, task_vars=None):
|
||||||
|
task_vars = task_vars or {}
|
||||||
|
result = super().run(tmp, task_vars)
|
||||||
|
|
||||||
|
base_url = self._task.args.get("url")
|
||||||
|
username = self._require_arg("username")
|
||||||
|
password = self._require_arg("password")
|
||||||
|
config = self._require_arg("config")
|
||||||
|
|
||||||
|
aruba = Aruba(base_url)
|
||||||
|
aruba.login(username, password)
|
||||||
|
|
||||||
|
try:
|
||||||
|
aruba.restore(config)
|
||||||
|
except:
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
aruba.logout()
|
||||||
|
|
||||||
|
return result
|
@ -1,12 +1,97 @@
|
|||||||
---
|
---
|
||||||
|
- name: Generate configuration
|
||||||
- name: Generation configuration
|
|
||||||
set_fact:
|
set_fact:
|
||||||
aruba__config: "{{ lookup('template', './config.j2') }}"
|
aruba__config: "{{ lookup('template', './config.j2') }}"
|
||||||
|
when: "aruba__config is not defined"
|
||||||
|
|
||||||
|
- name: Restore configuration
|
||||||
|
aruba_cfg_restore:
|
||||||
|
url: "http://{{ inventory_hostname }}/"
|
||||||
|
username: "{{ aruba__api_username }}"
|
||||||
|
password: "{{ aruba__api_password }}"
|
||||||
|
config: "{{ aruba__config }}"
|
||||||
|
|
||||||
|
#- name: Login to switch
|
||||||
|
# delegate_to: localhost
|
||||||
|
# uri:
|
||||||
|
# url: "{{ aruba__api_base_url }}/rest/v4/login-sessions"
|
||||||
|
# method: POST
|
||||||
|
# status_code: 201
|
||||||
|
# body_format: json
|
||||||
|
# body:
|
||||||
|
# userName: "{{ aruba__api_username }}"
|
||||||
|
# password: "{{ aruba__api_password }}"
|
||||||
|
# register: login
|
||||||
|
|
||||||
|
#- name: Get diff
|
||||||
|
# delegate_to: localhost
|
||||||
|
# uri:
|
||||||
|
# url: "{{ aruba__api_base_url }}/rest/v4/system/config/cfg_restore/payload/latest_diff"
|
||||||
|
# method: POST
|
||||||
|
# body_format: json
|
||||||
|
# status_code: 202
|
||||||
|
# body:
|
||||||
|
# config_base64_encoded: "{{ aruba__config | b64encode }}"
|
||||||
|
# headers:
|
||||||
|
# Cookie: "{{ login.json.cookie }}"
|
||||||
|
# register: diff
|
||||||
|
|
||||||
|
#- name: Diff
|
||||||
|
# debug:
|
||||||
|
# msg: "{{ diff }}"
|
||||||
|
|
||||||
|
#- name: Get diff
|
||||||
|
# delegate_to: localhost
|
||||||
|
# uri:
|
||||||
|
# url: "{{ aruba__api_base_url }}/rest/v4/system/config/cfg_restore/payload/latest_diff/status"
|
||||||
|
# method: GET
|
||||||
|
# status_code: 200
|
||||||
|
# headers:
|
||||||
|
# Cookie: "{{ login.json.cookie }}"
|
||||||
|
# register: diff
|
||||||
|
|
||||||
|
#- name: Diff
|
||||||
|
# debug:
|
||||||
|
# msg: "{{ diff }}"
|
||||||
|
|
||||||
|
#- name: Restore configuration
|
||||||
|
# delegate_to: localhost
|
||||||
|
# uri:
|
||||||
|
# url: "{{ aruba__api_base_url }}/rest/v4/system/config/cfg_restore/payload"
|
||||||
|
# method: POST
|
||||||
|
# body_format: json
|
||||||
|
# status_code: 202
|
||||||
|
# body:
|
||||||
|
# config_base64_encoded: "{{ aruba__config | b64encode }}"
|
||||||
|
# is_forced_reboot_enabled: true
|
||||||
|
# headers:
|
||||||
|
# Cookie: "{{ login.json.cookie }}"
|
||||||
|
# register: status
|
||||||
|
#
|
||||||
|
#- name: XX
|
||||||
|
# debug:
|
||||||
|
# msg: "{{ status }}"
|
||||||
|
#
|
||||||
|
#- name: Get diff
|
||||||
|
# delegate_to: localhost
|
||||||
|
# uri:
|
||||||
|
# url: "{{ aruba__api_base_url }}/rest/v4/system/config/cfg_restore/payload/status"
|
||||||
|
# method: GET
|
||||||
|
# status_code: 200
|
||||||
|
# headers:
|
||||||
|
# Cookie: "{{ login.json.cookie }}"
|
||||||
|
# register: diff
|
||||||
|
|
||||||
|
#- name: Diff
|
||||||
|
# debug:
|
||||||
|
# msg: "{{ diff }}"
|
||||||
|
|
||||||
- name: Write configuration
|
#- name: Logout
|
||||||
delegate_to: localhost
|
# delegate_to: localhost
|
||||||
copy:
|
# uri:
|
||||||
content: "{{ aruba__config }}"
|
# url: "{{ aruba__api_base_url }}/rest/v4/login-sessions"
|
||||||
dest: /tmp/aruba.config
|
# method: DELETE
|
||||||
|
# status_code: 204
|
||||||
|
# headers:
|
||||||
|
# Cookie: "{{ login.json.cookie }}"
|
||||||
...
|
...
|
||||||
|
Loading…
Reference in New Issue