mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
roll up of fixes for sros modules (#22972)
* fixes action handlers for sros * fixes sros_config module execution to use AnsibleModule * fixes sros_command module to use socket connection * adds sros to constants
This commit is contained in:
@@ -30,7 +30,7 @@ import re
|
||||
from ansible.module_utils.six.moves import zip
|
||||
from ansible.module_utils.network_common import to_list
|
||||
|
||||
DEFAULT_COMMENT_TOKENS = ['#', '!', '/*', '*/']
|
||||
DEFAULT_COMMENT_TOKENS = ['#', '!', '/*', '*/', 'echo']
|
||||
|
||||
|
||||
class ConfigLine(object):
|
||||
|
||||
@@ -28,82 +28,93 @@
|
||||
# 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.
|
||||
#
|
||||
import re
|
||||
from ansible.module_utils.basic import env_fallback
|
||||
from ansible.module_utils.network_common import to_list, ComplexList
|
||||
from ansible.module_utils.connection import exec_command
|
||||
|
||||
from ansible.module_utils.network import NetworkModule, NetworkError
|
||||
from ansible.module_utils.network import register_transport, to_list
|
||||
from ansible.module_utils.shell import CliBase
|
||||
from ansible.module_utils.netcli import Command
|
||||
_DEVICE_CONFIGS = {}
|
||||
|
||||
sros_argument_spec = {
|
||||
'host': dict(),
|
||||
'port': dict(type='int'),
|
||||
'username': dict(fallback=(env_fallback, ['ANSIBLE_NET_USERNAME'])),
|
||||
'password': dict(fallback=(env_fallback, ['ANSIBLE_NET_PASSWORD']), no_log=True),
|
||||
'ssh_keyfile': dict(fallback=(env_fallback, ['ANSIBLE_NET_SSH_KEYFILE']), type='path'),
|
||||
'timeout': dict(type='int'),
|
||||
'provider': dict(type='dict')
|
||||
}
|
||||
|
||||
def check_args(module, warnings):
|
||||
provider = module.params['provider'] or {}
|
||||
for key in sros_argument_spec:
|
||||
if key != 'provider' and module.params[key]:
|
||||
warnings.append('argument %s has been deprecated and will be '
|
||||
'removed in a future version' % key)
|
||||
|
||||
def get_config(module, flags=[]):
|
||||
cmd = 'admin display-config '
|
||||
cmd += ' '.join(flags)
|
||||
cmd = cmd.strip()
|
||||
|
||||
try:
|
||||
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=err)
|
||||
cfg = str(out).strip()
|
||||
_DEVICE_CONFIGS[cmd] = cfg
|
||||
return cfg
|
||||
|
||||
def to_commands(module, commands):
|
||||
spec = {
|
||||
'command': dict(key=True),
|
||||
'prompt': dict(),
|
||||
'answer': dict()
|
||||
}
|
||||
transform = ComplexList(spec, module)
|
||||
return transform(commands)
|
||||
|
||||
|
||||
class Cli(CliBase):
|
||||
def run_commands(module, commands, check_rc=True):
|
||||
responses = list()
|
||||
commands = to_commands(module, to_list(commands))
|
||||
for cmd in commands:
|
||||
cmd = module.jsonify(cmd)
|
||||
rc, out, err = exec_command(module, cmd)
|
||||
if check_rc and rc != 0:
|
||||
module.fail_json(msg=err, rc=rc)
|
||||
responses.append(out)
|
||||
return responses
|
||||
|
||||
NET_PASSWD_RE = re.compile(r"[\r\n]?password: $", re.I)
|
||||
def load_config(module, commands):
|
||||
for command in to_list(commands):
|
||||
rc, out, err = exec_command(module, command)
|
||||
if rc != 0:
|
||||
module.fail_json(msg=err, command=command, rc=rc)
|
||||
exec_command(module, 'exit all')
|
||||
|
||||
CLI_PROMPTS_RE = [
|
||||
re.compile(r"[\r\n]?[\w+\-\.:\/\[\]]+(?:\([^\)]+\)){,3}(?:>|#) ?$"),
|
||||
re.compile(r"\[\w+\@[\w\-\.]+(?: [^\]])\] ?[>#\$] ?$")
|
||||
]
|
||||
|
||||
CLI_ERRORS_RE = [
|
||||
re.compile(r"^\r\nError:", re.M),
|
||||
]
|
||||
|
||||
def __init__(self):
|
||||
super(Cli, self).__init__()
|
||||
self._rollback_enabled = None
|
||||
|
||||
@property
|
||||
def rollback_enabled(self):
|
||||
if self._rollback_enabled is not None:
|
||||
return self._rollback_enabled
|
||||
resp = self.execute(['show system rollback'])
|
||||
match = re.search(r'^Rollback Location\s+:\s(\S+)', resp[0], re.M)
|
||||
self._rollback_enabled = match.group(1) != 'None'
|
||||
def rollback_enabled(self):
|
||||
if self._rollback_enabled is not None:
|
||||
return self._rollback_enabled
|
||||
resp = self.execute(['show system rollback'])
|
||||
match = re.search(r'^Rollback Location\s+:\s(\S+)', resp[0], re.M)
|
||||
self._rollback_enabled = match.group(1) != 'None'
|
||||
return self._rollback_enabled
|
||||
|
||||
def connect(self, params, **kwargs):
|
||||
super(Cli, self).connect(params, kickstart=False, **kwargs)
|
||||
self.shell.send('environment no more')
|
||||
self._connected = True
|
||||
def load_config_w_rollback(self, commands):
|
||||
if self.rollback_enabled:
|
||||
self.execute(['admin rollback save'])
|
||||
|
||||
### implementation of netcli.Cli ###
|
||||
|
||||
def run_commands(self, commands, **kwargs):
|
||||
return self.execute(to_list(commands))
|
||||
|
||||
### implementation of netcfg.Config ###
|
||||
|
||||
def configure(self, commands, **kwargs):
|
||||
cmds = to_list(commands)
|
||||
responses = self.execute(cmds)
|
||||
self.execute(['exit all'])
|
||||
return responses
|
||||
|
||||
def get_config(self, detail=False, **kwargs):
|
||||
cmd = 'admin display-config'
|
||||
if detail:
|
||||
cmd += ' detail'
|
||||
return self.execute(cmd)[0]
|
||||
|
||||
def load_config(self, commands):
|
||||
try:
|
||||
self.configure(commands)
|
||||
except NetworkError:
|
||||
if self.rollback_enabled:
|
||||
self.execute(['admin rollback save'])
|
||||
self.execute(['admin rollback revert latest-rb',
|
||||
'admin rollback delete latest-rb'])
|
||||
raise
|
||||
|
||||
try:
|
||||
self.configure(commands)
|
||||
except NetworkError:
|
||||
if self.rollback_enabled:
|
||||
self.execute(['admin rollback revert latest-rb',
|
||||
'admin rollback delete latest-rb'])
|
||||
raise
|
||||
|
||||
if self.rollback_enabled:
|
||||
self.execute(['admin rollback delete latest-rb'])
|
||||
|
||||
def save_config(self):
|
||||
self.execute(['admin save'])
|
||||
|
||||
Cli = register_transport('cli', default=True)(Cli)
|
||||
if self.rollback_enabled:
|
||||
self.execute(['admin rollback delete latest-rb'])
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user