Refactor IOS libs to use cliconf (#34136)
* Refactor IOS libs to use cliconf * Fix pep8 * Remove bytstrings from edit_config and additional end on load_config * Fix pep8
This commit is contained in:
parent
2b3b689616
commit
477cd3f775
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.utils import to_list, ComplexList
|
from ansible.module_utils.network.common.utils import to_list, ComplexList
|
||||||
from ansible.module_utils.connection import exec_command
|
from ansible.module_utils.connection import Connection
|
||||||
|
|
||||||
_DEVICE_CONFIGS = {}
|
_DEVICE_CONFIGS = {}
|
||||||
|
|
||||||
|
@ -63,12 +64,36 @@ def get_provider_argspec():
|
||||||
return ios_provider_spec
|
return ios_provider_spec
|
||||||
|
|
||||||
|
|
||||||
|
def get_connection(module):
|
||||||
|
if hasattr(module, '_ios_connection'):
|
||||||
|
return module._ios_connection
|
||||||
|
|
||||||
|
capabilities = get_capabilities(module)
|
||||||
|
network_api = capabilities.get('network_api')
|
||||||
|
if network_api == 'cliconf':
|
||||||
|
module._ios_connection = Connection(module._socket_path)
|
||||||
|
else:
|
||||||
|
module.fail_json(msg='Invalid connection type %s' % network_api)
|
||||||
|
|
||||||
|
return module._ios_connection
|
||||||
|
|
||||||
|
|
||||||
|
def get_capabilities(module):
|
||||||
|
if hasattr(module, '_ios_capabilities'):
|
||||||
|
return module._ios_capabilities
|
||||||
|
|
||||||
|
capabilities = Connection(module._socket_path).get_capabilities()
|
||||||
|
module._ios_capabilities = json.loads(capabilities)
|
||||||
|
return module._ios_capabilities
|
||||||
|
|
||||||
|
|
||||||
def check_args(module, warnings):
|
def check_args(module, warnings):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def get_defaults_flag(module):
|
def get_defaults_flag(module):
|
||||||
rc, out, err = exec_command(module, 'show running-config ?')
|
connection = get_connection(module)
|
||||||
|
out = connection.get('show running-config ?')
|
||||||
out = to_text(out, errors='surrogate_then_replace')
|
out = to_text(out, errors='surrogate_then_replace')
|
||||||
|
|
||||||
commands = set()
|
commands = set()
|
||||||
|
@ -83,20 +108,15 @@ def get_defaults_flag(module):
|
||||||
|
|
||||||
|
|
||||||
def get_config(module, flags=None):
|
def get_config(module, flags=None):
|
||||||
flags = [] if flags is None else flags
|
global _DEVICE_CONFIGS
|
||||||
|
|
||||||
cmd = 'show running-config '
|
if _DEVICE_CONFIGS != {}:
|
||||||
cmd += ' '.join(flags)
|
return _DEVICE_CONFIGS
|
||||||
cmd = cmd.strip()
|
else:
|
||||||
|
connection = get_connection(module)
|
||||||
try:
|
out = connection.get_config()
|
||||||
return _DEVICE_CONFIGS[cmd]
|
|
||||||
except KeyError:
|
|
||||||
rc, out, err = exec_command(module, cmd)
|
|
||||||
if rc != 0:
|
|
||||||
module.fail_json(msg='unable to retrieve current config', stderr=to_text(err, errors='surrogate_then_replace'))
|
|
||||||
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
cfg = to_text(out, errors='surrogate_then_replace').strip()
|
||||||
_DEVICE_CONFIGS[cmd] = cfg
|
_DEVICE_CONFIGS = cfg
|
||||||
return cfg
|
return cfg
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,31 +132,31 @@ def to_commands(module, commands):
|
||||||
|
|
||||||
def run_commands(module, commands, check_rc=True):
|
def run_commands(module, commands, check_rc=True):
|
||||||
responses = list()
|
responses = list()
|
||||||
commands = to_commands(module, to_list(commands))
|
connection = get_connection(module)
|
||||||
for cmd in commands:
|
|
||||||
cmd = module.jsonify(cmd)
|
for cmd in to_list(commands):
|
||||||
rc, out, err = exec_command(module, cmd)
|
if isinstance(cmd, dict):
|
||||||
if check_rc and rc != 0:
|
command = cmd['command']
|
||||||
module.fail_json(msg=to_text(err, errors='surrogate_then_replace'), rc=rc)
|
prompt = cmd['prompt']
|
||||||
responses.append(to_text(out, errors='surrogate_then_replace'))
|
answer = cmd['answer']
|
||||||
|
else:
|
||||||
|
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):
|
def load_config(module, commands):
|
||||||
response = []
|
connection = get_connection(module)
|
||||||
rc, out, err = exec_command(module, 'configure terminal')
|
|
||||||
if rc != 0:
|
|
||||||
module.fail_json(msg='unable to enter configuration mode', err=to_text(out, errors='surrogate_then_replace'))
|
|
||||||
|
|
||||||
for command in to_list(commands):
|
out = connection.edit_config(commands)
|
||||||
if command == 'end':
|
|
||||||
continue
|
|
||||||
rc, out, err = exec_command(module, command)
|
|
||||||
if rc != 0:
|
|
||||||
exec_command(module, 'end')
|
|
||||||
module.fail_json(msg=to_text(err, errors='surrogate_then_replace'), command=command, rc=rc)
|
|
||||||
response.append({command: out})
|
|
||||||
|
|
||||||
exec_command(module, 'end')
|
|
||||||
|
|
||||||
return response
|
|
||||||
|
|
|
@ -65,10 +65,21 @@ class Cliconf(CliconfBase):
|
||||||
@enable_mode
|
@enable_mode
|
||||||
def edit_config(self, command):
|
def edit_config(self, command):
|
||||||
for cmd in chain(['configure terminal'], to_list(command), ['end']):
|
for cmd in chain(['configure terminal'], to_list(command), ['end']):
|
||||||
self.send_command(to_bytes(cmd))
|
try:
|
||||||
|
cmd = json.loads(cmd)
|
||||||
|
command = cmd['command']
|
||||||
|
prompt = cmd['prompt']
|
||||||
|
answer = cmd['answer']
|
||||||
|
except:
|
||||||
|
command = cmd
|
||||||
|
prompt = None
|
||||||
|
answer = None
|
||||||
|
|
||||||
|
self.send_command(to_bytes(command), to_bytes(prompt), to_bytes(answer))
|
||||||
|
|
||||||
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 get_capabilities(self):
|
def get_capabilities(self):
|
||||||
result = {}
|
result = {}
|
||||||
|
|
Loading…
Add table
Reference in a new issue