Arista:配置差异和归档

网络工程
2021-07-28 23:07:52

查看 Arista EOS 的文档 - 该configuration replace命令仅在 4.14 中引入,<4.14 没有任何形式的替换配置(而不是简单的合并)。

除了使用手动命令之外,从 EOS <=4.13 上的文件加载配置的首选方法是什么,这将完全替换正在运行的配置。

这是一个例子,

running-config

! device: arista-test (DCS-7124S, EOS-4.13.10M)
!
! boot system flash:EOS.swi
!
transceiver qsfp default-mode 4x10G
!
hostname arista-test
ip name-server vrf default 8.8.8.8

new-config

! device: arista-test (DCS-7124S, EOS-4.13.10M)
!
! boot system flash:EOS.swi
!
transceiver qsfp default-mode 4x10G
!
hostname arista-test-new

然后运行copy flash:new-config running-config,输出将是,

! device: arista-test (DCS-7124S, EOS-4.13.10M)
!
! boot system flash:EOS.swi
!
transceiver qsfp default-mode 4x10G
!
hostname arista-test-new
ip name-server vrf default 8.8.8.8

而期望的输出是,

! device: arista-test (DCS-7124S, EOS-4.13.10M)
!
! boot system flash:EOS.swi
!
transceiver qsfp default-mode 4x10G
!
hostname arista-test-new
1个回答

我编写了一个小的 bash 脚本来使用 Aristaalias调用它来自动化这个过程

创建目录/mnt/flash/bin并在其中创建以下文件,

config-replace

#!/bin/bash

APPLY_CHANGES=1

ARCHIVE_RETENTION=30
CONFIG_ARCHIVE="/mnt/flash/config"
TMP_DIR="/var/tmp"

NEW_CONFIG="$1"
RUNNING_CONFIG="/mnt/flash/config/.running-config"
TMP_CONFIG="/var/tmp/new-config"

function error()
{
    echo "$@"
    exit 0
}

function cli()
{
    FastCli -p15 -c "$@"
}

function cleanup()
{
    [ -f "$TMP_CONFIG" ] && rm $TMP_CONFIG
}

trap cleanup EXIT

[ -z "$NEW_CONFIG" ] && error "Configuration file must be set"
[ ! -f "$NEW_CONFIG" ] && error "Configuration file ($NEW_CONFIG) doesn't exist"
[ ! -d "$CONFIG_ARCHIVE" ] && mkdir -p $CONFIG_ARCHIVE

# Backup existing configuration to a file
[ ! -f "/mnt/flash/config/.running-config" ] && UPDATE_RUNNING_CONFIG=1
[ -n "$UPDATE_RUNNING_CONFIG" ] && cli "copy running-config file:/mnt/flash/config/.running-config"

DIFF_PRINT=$(diff -up -I 'last modified at' $RUNNING_CONFIG $NEW_CONFIG)
[ $(echo "$DIFF_PRINT" | wc -l) -le 1 ] && error "Configuration is the same, no changes made"

# Show the existing differences
echo "$DIFF_PRINT"
echo "---"
read -p "Do you want to apply the changes? [y/N]: " APPLY

[[ ! "$APPLY" == "y" ]] && error "Aborted"

diff -I 'last modified at' \
--new-line-format='%l
' \
--old-line-format='no %l
' \
--unchanged-line-format='%l
' \
$RUNNING_CONFIG $NEW_CONFIG | \
sed -E '/^no !$/d; /^$/d; s/^no\s{3}/   no/g' > $TMP_CONFIG

NEGATE_LINE=
while IFS='' read LINE; do
    if [[ "$LINE" == "no"* ]]; then
        NEGATE_LINE=1
    elif [ -n "$NEGATE_LINE" ] && [[ "$LINE" == "   "* ]]; then
        continue
    else
        NEGATE_LINE=
    fi
    echo "$LINE"
done < $TMP_CONFIG > $TMP_CONFIG.tmp
mv $TMP_CONFIG{.tmp,}

if [ -n "$APPLY_CHANGES" ]; then

    cli "copy file:$TMP_CONFIG running-config"

    # Backup and rotate configurations
    while read FILE; do
        IFS='.' read -ra FILE_PARTS <<< "$FILE"
        NEW_FILE="/mnt/flash/config/arista.conf.$(( ${FILE_PARTS[2]} + 1 )).gz"
        mv $FILE $NEW_FILE
        [ -z "$LAST_FILE" ] && LAST_FILE=$NEW_FILE
    done < <(find /mnt/flash/config -type f -name "*.gz" | sort -Vr)
    [ -f "$LAST_FILE" ] && rm $LAST_FILE
    gzip -c /mnt/flash/config/.running-config > $CONFIG_ARCHIVE/arista.conf.0.gz

    find /mnt/flash/config -type f -name "*.gz" | tail -n+${ARCHIVE_RETENTION} | xargs rm
    rm /mnt/flash/config/.running-config

fi

exit
alias load "bash /mnt/flash/bin/config-replace %1"

然后从 CLI 界面 - 添加别名

enable
conf term
alias load "bash /mnt/flash/bin/config-replace %1"
write
end

然后要使用新命令,只需将新配置文件上传到您选择的位置 - 然后在运行环境中调用该load命令。

例如。

arista-test# load /mnt/flash/startup-config

Copy completed successfully.
--- /mnt/flash/config/.running-config   2016-04-06 16:46:09.000000000 +0100
+++ /mnt/flash/startup-config   2016-04-06 16:43:46.000000000 +0100
@@ -1,13 +1,13 @@
-! device: arista-sw1 (DCS-7124S, EOS-4.13.10M)
+! Startup-config last modified at  Wed Apr  6 16:06:56 2016 by admin
+! device: arista-test (DCS-7124S, EOS-4.13.10M)
 !
 ! boot system flash:EOS.swi
 !
-hostname arista-test
-ip name-server vrf default 8.8.8.8
+hostname arista-test-new
---
Do you want to apply the changes? [y/N]:

然后,如果您按y- 它将应用差异化的更改(包括否定相应的命令) - 并/mnt/flash/config为最多$ARCHIVE_RETENTION版本创建配置备份

这是受 JunOS 方法的启发,所以我可能也会写一个rollback别名。

Arista 缺少这些基本的配置管理工具,这是一种耻辱。


在这里找到脚本,https://gist.github.com/anonymous/51d86cb90e51683ee417317aee86f306