From 08a06669293ecec6b755b8ed0a26b4138d044770 Mon Sep 17 00:00:00 2001 From: Rottler Tamas Date: Sat, 19 Jul 2025 13:23:51 +0200 Subject: [PATCH] cisco nexus --- ansible-role/tasks/backup.yml | 1 + routerbackup | 47 ++++++++++++++++++++++++++++++++--- routerbackup.yml | 4 +-- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/ansible-role/tasks/backup.yml b/ansible-role/tasks/backup.yml index fd715c3..50b8288 100644 --- a/ansible-role/tasks/backup.yml +++ b/ansible-role/tasks/backup.yml @@ -5,6 +5,7 @@ {% if 'routeros' in group_names %}routeros {% elif 'ciscoasa' in group_names %}asa {% elif 'ciscoios' in group_names %}ios + {% elif 'cisconexus' in group_names %}nexus {% elif 'hpcomware' in group_names %}comware {% else %}undefined {% endif %} diff --git a/routerbackup b/routerbackup index 5094d0d..58302af 100755 --- a/routerbackup +++ b/routerbackup @@ -13,12 +13,12 @@ import difflib def get_args(): parser = argparse.ArgumentParser( - description='routerbackup - save routeros/ios/asa router configuration') + description='routerbackup - save routeros/ios/nexus/asa router configuration') parser.add_argument('-t', '--type', required=True, action='store', - help='device type {routeros,ios,asa}') + help='device type {routeros,ios,nexus,asa}') parser.add_argument('-H', '--host', required=True, action='store', @@ -349,6 +349,45 @@ class IosConfig(Config): return prepared +class NexusConfig(Config): + def __init__(self, workdir): + super().__init__(workdir) + self.extra['iproute'] = { + 'type': 'text', + 'content': None + } + + def get_config(self): + shrun = [] + stdin, stdout, stderr = self.ssh.exec_command('sh run') + for line in stdout: + line = line.rstrip() + shrun.append(line) + + iproute = [] + for cmd in ('sh ip interface brief', 'sh ip route'): + stdin, stdout, stderr = self.ssh.exec_command(cmd) + iproute.append('# '+ cmd.center(76, '=')) + for line in stdout: + line = line.rstrip() + iproute.append(line) + + self.config = '\n'.join(shrun) + self.extra['iproute']['content'] = '\n'.join(iproute) + + def _prepare_config_for_diff(self, configlines): + prepared = [] + for line in configlines: + if not re.search('\S', line): + continue + if re.match('!Running configuration last done at', line): + continue + if re.match('!Time', line): + continue + prepared.append(line) + return prepared + + class ConfigWithShell(Config): def _shell_command(self, channel, cmd, waitfor=None, @@ -461,13 +500,15 @@ def main(): args = get_args() setup_logging(args.host, args.logfile, args.loglevel) try: - if args.type not in ('routeros', 'ios', 'asa', 'comware'): + if args.type not in ('routeros', 'ios', 'nexus', 'asa', 'comware'): raise ValueError(f'Invalid devtype: {args.type}') if args.type == 'routeros': cf = RouterosConfig(workdir=args.backupdir) elif args.type == 'ios': cf = IosConfig(workdir=args.backupdir) + elif args.type == 'nexus': + cf = NexusConfig(workdir=args.backupdir) elif args.type == 'asa': cf = AsaConfig(workdir=args.backupdir) elif args.type == 'comware': diff --git a/routerbackup.yml b/routerbackup.yml index d28732e..e3f9634 100644 --- a/routerbackup.yml +++ b/routerbackup.yml @@ -1,6 +1,6 @@ --- - name: set router configuration backup directory - hosts: routeros,ciscoasa,!ciscoasa_slave,ciscoios,hpcomware + hosts: routeros,ciscoasa,!ciscoasa_slave,ciscoios,cisconexus,hpcomware gather_facts: no tags: always tasks: @@ -11,7 +11,7 @@ routerbackup_maxage: 24 - name: use router backup role - hosts: routeros,ciscoasa,!ciscoasa_slave,ciscoios,hpcomware + hosts: routeros,ciscoasa,!ciscoasa_slave,ciscoios,cisconexus,hpcomware gather_facts: no roles: - routerbackup