aruba: Add login support, but hangs

This commit is contained in:
v-lafeychine 2025-10-06 12:53:21 +02:00
parent f4096cf5af
commit 1e6d1e66d8
Signed by: v-lafeychine
GPG key ID: F46CAAD27C7AB0D5
5 changed files with 21 additions and 28 deletions

View file

@ -4,8 +4,6 @@ switch_vars:
location: "Local_de_Brassage_EdC" location: "Local_de_Brassage_EdC"
host: 10.130.4.11 host: 10.130.4.11
port: 80 port: 80
username: "{{ vault_switch.username }}"
password: "{{ vault_switch.password }}"
delete_vlans: [] delete_vlans: []
vlans: vlans:
- id: 40 - id: 40

View file

@ -3,6 +3,8 @@ from ansible_collections.ansible.netcommon.plugins.plugin_utils.httpapi_base imp
HttpApiBase, HttpApiBase,
) )
import json
class HttpApi(HttpApiBase): class HttpApi(HttpApiBase):
def login(self, username, password): def login(self, username, password):
@ -11,7 +13,7 @@ class HttpApi(HttpApiBase):
Return True if the connection has succeeded and False otherwise. Return True if the connection has succeeded and False otherwise.
""" """
data = {"userName": username, "password": password} data = {"userName": username, "password": password}
response = self.send_request(json.dumps(data), path="/login-sessions") response = self.send_request("login-sessions", data, method="POST")
if response.status_code != 201: if response.status_code != 201:
return AnsibleAuthentificationFailure(message="Plop!") return AnsibleAuthentificationFailure(message="Plop!")
@ -34,12 +36,15 @@ class HttpApi(HttpApiBase):
self.headers.pop("cookie") self.headers.pop("cookie")
return True return True
def send_request(self, data, path, method="POST"): def send_request(self, path, data, method="GET"):
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
uri = self.get_option("uri_root_path") + "/" + path api = self.connection.get_option("api")
uri = f"/rest/{api}/{path}"
if data is not None: if not data:
content = json.dumps(data) data = {}
content = json.dumps(data)
try: try:
response, content = self.connection.send(uri, content, method=method, headers=headers) response, content = self.connection.send(uri, content, method=method, headers=headers)

View file

@ -145,7 +145,7 @@ def throw_err(msg, url, status, response):
def configure(connection, config, check_mode, current_path="", create_method=None): def configure(connection, config, check_mode, current_path="", create_method=None):
path = "/" + str(config["path"]) path = str(config["path"])
url = current_path + path url = current_path + path
changed = False changed = False
before = {"path": path} before = {"path": path}
@ -157,7 +157,7 @@ def configure(connection, config, check_mode, current_path="", create_method=Non
# If removing configuration # If removing configuration
if "delete" in config and config["delete"]: if "delete" in config and config["delete"]:
# Get the configuration # Get the configuration
status, response = connection.send_request(None, url, method="GET") status, response = connection.send_request(url, None, method="GET")
if status == 404: if status == 404:
before["delete"] = True before["delete"] = True
elif status in (200, 201, 202, 203, 204): elif status in (200, 201, 202, 203, 204):
@ -206,7 +206,7 @@ def configure(connection, config, check_mode, current_path="", create_method=Non
# If create or edit # If create or edit
elif "data" in config and type(config["data"]) is dict: elif "data" in config and type(config["data"]) is dict:
# Get the configuration # Get the configuration
status, response = connection.send_request(None, url, method="GET") status, response = connection.send_request(url, None, method="GET")
new_data = {} new_data = {}
if status == 404: if status == 404:
before["delete"] = True before["delete"] = True
@ -287,7 +287,7 @@ def configure(connection, config, check_mode, current_path="", create_method=Non
connection, connection,
subconf, subconf,
api, api,
current_path=url, current_path=url + "/",
create_method=create_method, create_method=create_method,
) )
changed = changed or response["changed"] changed = changed or response["changed"]
@ -300,8 +300,6 @@ def configure(connection, config, check_mode, current_path="", create_method=Non
def run_module(): def run_module():
module_args = { module_args = {
"config": {"type": "dict", "required": True}, "config": {"type": "dict", "required": True},
"username": {"type": "str", "required": True},
"password": {"type": "str", "required": True, "no_log": True},
"port": {"type": "int", "required": True}, "port": {"type": "int", "required": True},
"host": {"type": "str", "required": True}, "host": {"type": "str", "required": True},
"version": {"type": "str", "required": False, "default": "v1"}, "version": {"type": "str", "required": False, "default": "v1"},
@ -309,9 +307,12 @@ def run_module():
} }
module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) module = AnsibleModule(argument_spec=module_args, supports_check_mode=True)
connection = Connection(module._socket_path) connection = Connection(module._socket_path)
connection.set_option("uri_root_path", module.params["host"])
connection.set_option("host", module.params["host"])
connection.set_option("port", module.params["port"])
connection.set_option("api", module.params["version"])
connection.set_option("use_proxy", module.params["use_proxy"])
result = { result = {
"changed": False, "changed": False,

View file

@ -12,8 +12,8 @@
vars: vars:
ansible_network_os: aruba ansible_network_os: aruba
ansible_user: vault_switch.username ansible_user: "{{ vault_switch.username }}"
ansible_httpapi_password: vault_switch.password ansible_password: "{{ vault_switch.password }}"
ansible_httpapi_use_ssl: false ansible_httpapi_use_ssl: false
ansible_httpapi_validate_certs: false ansible_httpapi_validate_certs: false

View file

@ -1,8 +1,6 @@
--- ---
- name: Configure switch - name: Configure switch
switch_config: switch_config:
username: "{{ switch_vars.username }}"
password: "{{ switch_vars.password }}"
port: "{{ switch_vars.port }}" port: "{{ switch_vars.port }}"
host: "{{ switch_vars.host }}" host: "{{ switch_vars.host }}"
use_proxy: "{{ switch.use_proxy }}" use_proxy: "{{ switch.use_proxy }}"
@ -15,8 +13,6 @@
- name: Configure sntp - name: Configure sntp
switch_config: switch_config:
username: "{{ switch_vars.username }}"
password: "{{ switch_vars.password }}"
port: "{{ switch_vars.port }}" port: "{{ switch_vars.port }}"
host: "{{ switch_vars.host }}" host: "{{ switch_vars.host }}"
use_proxy: "{{ switch.use_proxy }}" use_proxy: "{{ switch.use_proxy }}"
@ -29,8 +25,6 @@
- name: Configure sntp servers - name: Configure sntp servers
switch_config: switch_config:
username: "{{ switch_vars.username }}"
password: "{{ switch_vars.password }}"
port: "{{ switch_vars.port }}" port: "{{ switch_vars.port }}"
host: "{{ switch_vars.host }}" host: "{{ switch_vars.host }}"
use_proxy: "{{ switch.use_proxy }}" use_proxy: "{{ switch.use_proxy }}"
@ -53,11 +47,6 @@
- name: Configure loop-protect - name: Configure loop-protect
switch_config: switch_config:
username: "{{ switch_vars.username }}"
password: "{{ switch_vars.password }}"
port: "{{ switch_vars.port }}"
host: "{{ switch_vars.host }}"
use_proxy: "{{ switch.use_proxy }}"
version: v7 version: v7
config: config:
path: loop_protect path: loop_protect