mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Vyos CliConf refactor (#33348)
* Refactor VyOS to use cliconf * Use show configuration commands on get_config * Remove debug statement * Construct command/answer/prompt if needed and fix commit comments * Convert command/prompt/answer to bytes
This commit is contained in:
parent
6732be3e62
commit
fec39ba1f2
2 changed files with 70 additions and 39 deletions
|
@ -25,10 +25,11 @@
|
||||||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
||||||
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
#
|
#
|
||||||
|
import json
|
||||||
from ansible.module_utils._text import to_text
|
from ansible.module_utils._text import to_text
|
||||||
from ansible.module_utils.basic import env_fallback, return_values
|
from ansible.module_utils.basic import env_fallback, return_values
|
||||||
from ansible.module_utils.network_common import to_list
|
from ansible.module_utils.network_common import to_list
|
||||||
from ansible.module_utils.connection import exec_command
|
from ansible.module_utils.connection import Connection
|
||||||
|
|
||||||
_DEVICE_CONFIGS = {}
|
_DEVICE_CONFIGS = {}
|
||||||
|
|
||||||
|
@ -62,64 +63,94 @@ def get_provider_argspec():
|
||||||
return vyos_provider_spec
|
return vyos_provider_spec
|
||||||
|
|
||||||
|
|
||||||
def get_config(module, target='commands'):
|
def get_connection(module):
|
||||||
cmd = ' '.join(['show configuration', target])
|
if hasattr(module, '_vyos_connection'):
|
||||||
|
return module._vyos_connection
|
||||||
|
|
||||||
try:
|
capabilities = get_capabilities(module)
|
||||||
return _DEVICE_CONFIGS[cmd]
|
network_api = capabilities.get('network_api')
|
||||||
except KeyError:
|
if network_api == 'cliconf':
|
||||||
rc, out, err = exec_command(module, cmd)
|
module._vyos_connection = Connection(module._socket_path)
|
||||||
if rc != 0:
|
else:
|
||||||
module.fail_json(msg='unable to retrieve current config', stderr=to_text(err, errors='surrogate_or_strict'))
|
module.fail_json(msg='Invalid connection type %s' % network_api)
|
||||||
cfg = to_text(out, errors='surrogate_or_strict').strip()
|
|
||||||
_DEVICE_CONFIGS[cmd] = cfg
|
return module._vyos_connection
|
||||||
|
|
||||||
|
|
||||||
|
def get_capabilities(module):
|
||||||
|
if hasattr(module, '_vyos_capabilities'):
|
||||||
|
return module._vyos_capabilities
|
||||||
|
|
||||||
|
capabilities = Connection(module._socket_path).get_capabilities()
|
||||||
|
module._vyos_capabilities = json.loads(capabilities)
|
||||||
|
return module._vyos_capabilities
|
||||||
|
|
||||||
|
|
||||||
|
def get_config(module):
|
||||||
|
global _DEVICE_CONFIGS
|
||||||
|
|
||||||
|
if _DEVICE_CONFIGS != {}:
|
||||||
|
return _DEVICE_CONFIGS
|
||||||
|
else:
|
||||||
|
connection = get_connection(module)
|
||||||
|
out = connection.get_config()
|
||||||
|
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
||||||
|
_DEVICE_CONFIGS = cfg
|
||||||
return cfg
|
return cfg
|
||||||
|
|
||||||
|
|
||||||
def run_commands(module, commands, check_rc=True):
|
def run_commands(module, commands, check_rc=True):
|
||||||
responses = list()
|
responses = list()
|
||||||
|
connection = get_connection(module)
|
||||||
|
|
||||||
for cmd in to_list(commands):
|
for cmd in to_list(commands):
|
||||||
rc, out, err = exec_command(module, cmd)
|
try:
|
||||||
if check_rc and rc != 0:
|
cmd = json.loads(cmd)
|
||||||
module.fail_json(msg=to_text(err, errors='surrogate_or_strict'), rc=rc)
|
command = cmd['command']
|
||||||
responses.append(to_text(out, errors='surrogate_or_strict'))
|
prompt = cmd['prompt']
|
||||||
|
answer = cmd['answer']
|
||||||
|
except:
|
||||||
|
command = cmd
|
||||||
|
prompt = None
|
||||||
|
answer = None
|
||||||
|
|
||||||
|
out = connection.get(command, prompt, answer)
|
||||||
|
|
||||||
|
try:
|
||||||
|
out = to_text(out, errors='surrogate_or_strict')
|
||||||
|
except UnicodeError:
|
||||||
|
module.fail_json(msg=u'Failed to decode output from %s: %s' % (cmd, to_text(out)))
|
||||||
|
|
||||||
|
responses.append(out)
|
||||||
|
|
||||||
return responses
|
return responses
|
||||||
|
|
||||||
|
|
||||||
def load_config(module, commands, commit=False, comment=None):
|
def load_config(module, commands, commit=False, comment=None):
|
||||||
rc, out, err = exec_command(module, 'configure')
|
connection = get_connection(module)
|
||||||
if rc != 0:
|
|
||||||
module.fail_json(msg='unable to enter configuration mode', output=to_text(err, errors='surrogate_or_strict'))
|
|
||||||
|
|
||||||
for cmd in to_list(commands):
|
out = connection.edit_config(commands)
|
||||||
rc, out, err = exec_command(module, cmd)
|
|
||||||
if rc != 0:
|
|
||||||
# discard any changes in case of failure
|
|
||||||
exec_command(module, 'exit discard')
|
|
||||||
module.fail_json(msg='configuration failed')
|
|
||||||
|
|
||||||
diff = None
|
diff = None
|
||||||
if module._diff:
|
if module._diff:
|
||||||
rc, out, err = exec_command(module, 'compare')
|
out = connection.get('compare')
|
||||||
out = to_text(out, errors='surrogate_or_strict')
|
out = to_text(out, errors='surrogate_or_strict')
|
||||||
|
|
||||||
if not out.startswith('No changes'):
|
if not out.startswith('No changes'):
|
||||||
rc, out, err = exec_command(module, 'show')
|
out = connection.get('show')
|
||||||
diff = to_text(out, errors='surrogate_or_strict').strip()
|
diff = to_text(out, errors='surrogate_or_strict').strip()
|
||||||
|
|
||||||
if commit:
|
if commit:
|
||||||
cmd = 'commit'
|
try:
|
||||||
if comment:
|
out = connection.commit(comment)
|
||||||
cmd += ' comment "%s"' % comment
|
except:
|
||||||
rc, out, err = exec_command(module, cmd)
|
connection.discard_changes()
|
||||||
if rc != 0:
|
module.fail_json(msg='commit failed: %s' % out)
|
||||||
# discard any changes in case of failure
|
|
||||||
exec_command(module, 'exit discard')
|
|
||||||
module.fail_json(msg='commit failed: %s' % err)
|
|
||||||
|
|
||||||
if not commit:
|
if not commit:
|
||||||
exec_command(module, 'exit discard')
|
connection.discard_changes()
|
||||||
else:
|
else:
|
||||||
exec_command(module, 'exit')
|
connection.get('exit')
|
||||||
|
|
||||||
if diff:
|
if diff:
|
||||||
return diff
|
return diff
|
||||||
|
|
|
@ -52,18 +52,18 @@ class Cliconf(CliconfBase):
|
||||||
return device_info
|
return device_info
|
||||||
|
|
||||||
def get_config(self):
|
def get_config(self):
|
||||||
return self.send_command(b'show configuration all')
|
return self.send_command(b'show configuration commands')
|
||||||
|
|
||||||
def edit_config(self, command):
|
def edit_config(self, command):
|
||||||
for cmd in chain([b'configure'], to_list(command)):
|
for cmd in chain([b'configure'], to_list(command)):
|
||||||
self.send_command(cmd)
|
self.send_command(cmd)
|
||||||
|
|
||||||
def get(self, command, prompt=None, answer=None, sendonly=False):
|
def get(self, command, prompt=None, answer=None, sendonly=False):
|
||||||
return self.send_command(command, prompt=prompt, answer=answer, sendonly=sendonly)
|
return self.send_command(to_bytes(command), prompt=to_bytes(prompt), answer=to_bytes(answer), sendonly=sendonly)
|
||||||
|
|
||||||
def commit(self, comment=None):
|
def commit(self, comment=None):
|
||||||
if comment:
|
if comment:
|
||||||
command = b'commit comment {0}'.format(comment)
|
command = b'commit comment "{0}"'.format(comment)
|
||||||
else:
|
else:
|
||||||
command = b'commit'
|
command = b'commit'
|
||||||
self.send_command(command)
|
self.send_command(command)
|
||||||
|
|
Loading…
Reference in a new issue