mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 05:42:50 +00:00
HTTP(S) API connection plugin (#39224)
* HTTPAPI connection * Punt run_commands to cliconf or httpapi * Fake enable_mode on eapi * Pull changes to nxos * Move load_config to edit_config for future-preparedness * Don't fail on lldp disabled * Re-enable check_rc on nxos' run_commands * Reorganize nxos httpapi plugin for compatibility * draft docs for connection: httpapi * restores docs for connection:local for eapi * Add _remote_is_local to httpapi
This commit is contained in:
@@ -163,46 +163,8 @@ class Cli:
|
||||
def run_commands(self, commands, check_rc=True):
|
||||
"""Run list of commands on remote device and return results
|
||||
"""
|
||||
responses = list()
|
||||
connection = self._get_connection()
|
||||
|
||||
for cmd in to_list(commands):
|
||||
if isinstance(cmd, dict):
|
||||
command = cmd['command']
|
||||
prompt = cmd['prompt']
|
||||
answer = cmd['answer']
|
||||
else:
|
||||
command = cmd
|
||||
prompt = None
|
||||
answer = None
|
||||
|
||||
out = connection.get(command, prompt, answer)
|
||||
out = to_text(out, errors='surrogate_or_strict')
|
||||
|
||||
try:
|
||||
out = self._module.from_json(out)
|
||||
except ValueError:
|
||||
out = str(out).strip()
|
||||
|
||||
responses.append(out)
|
||||
|
||||
return responses
|
||||
|
||||
def send_config(self, commands):
|
||||
conn = self._get_connection()
|
||||
|
||||
multiline = False
|
||||
rc = 0
|
||||
for command in to_list(commands):
|
||||
if command == 'end':
|
||||
continue
|
||||
|
||||
if command.startswith('banner') or multiline:
|
||||
multiline = True
|
||||
elif command == 'EOF' and multiline:
|
||||
multiline = False
|
||||
|
||||
conn.get(command, None, None, multiline)
|
||||
return connection.run_commands(commands, check_rc)
|
||||
|
||||
def configure(self, commands):
|
||||
"""Sends configuration commands to the remote device
|
||||
@@ -239,32 +201,12 @@ class Cli:
|
||||
return result
|
||||
|
||||
conn = self._get_connection()
|
||||
session = 'ansible_%s' % int(time.time())
|
||||
result = {'session': session}
|
||||
|
||||
out = conn.get('configure session %s' % session)
|
||||
|
||||
if replace:
|
||||
out = conn.get('rollback clean-config')
|
||||
|
||||
try:
|
||||
self.send_config(commands)
|
||||
return conn.load_config(commands, commit, replace)
|
||||
except ConnectionError as exc:
|
||||
self.close_session(session)
|
||||
message = getattr(exc, 'err', exc)
|
||||
self._module.fail_json(msg="Error on executing commands %s" % commands, data=to_text(message, errors='surrogate_then_replace'))
|
||||
|
||||
out = conn.get('show session-config diffs')
|
||||
if out:
|
||||
result['diff'] = to_text(out, errors='surrogate_then_replace').strip()
|
||||
|
||||
if commit:
|
||||
conn.get('commit')
|
||||
else:
|
||||
self.close_session(session)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
class Eapi:
|
||||
|
||||
|
||||
@@ -146,47 +146,8 @@ class Cli:
|
||||
def run_commands(self, commands, check_rc=True):
|
||||
"""Run list of commands on remote device and return results
|
||||
"""
|
||||
responses = list()
|
||||
connection = self._get_connection()
|
||||
|
||||
for item in to_list(commands):
|
||||
if item['output'] == 'json' and not is_json(item['command']):
|
||||
cmd = '%s | json' % item['command']
|
||||
elif item['output'] == 'text' and is_json(item['command']):
|
||||
cmd = item['command'].rsplit('|', 1)[0]
|
||||
else:
|
||||
cmd = item['command']
|
||||
|
||||
out = ''
|
||||
try:
|
||||
out = connection.get(cmd)
|
||||
code = 0
|
||||
except ConnectionError as e:
|
||||
code = getattr(e, 'code', 1)
|
||||
message = getattr(e, 'err', e)
|
||||
err = to_text(message, errors='surrogate_then_replace')
|
||||
|
||||
try:
|
||||
out = to_text(out, errors='surrogate_or_strict')
|
||||
except UnicodeError:
|
||||
self._module.fail_json(msg=u'Failed to decode output from %s: %s' % (cmd, to_text(out)))
|
||||
|
||||
if check_rc and code != 0:
|
||||
self._module.fail_json(msg=err)
|
||||
|
||||
if not check_rc and code != 0:
|
||||
try:
|
||||
out = self._module.from_json(err)
|
||||
except ValueError:
|
||||
out = to_text(message).strip()
|
||||
else:
|
||||
try:
|
||||
out = self._module.from_json(out)
|
||||
except ValueError:
|
||||
out = to_text(out).strip()
|
||||
|
||||
responses.append(out)
|
||||
return responses
|
||||
return connection.run_commands(commands, check_rc)
|
||||
|
||||
def load_config(self, config, return_error=False, opts=None):
|
||||
"""Sends configuration commands to the remote device
|
||||
|
||||
Reference in New Issue
Block a user