YAML修改别名序列元素

时间:2018-07-19 23:26:17

标签: go configuration yaml

我正在为程序配置文件格式,我想知道是否可以修改别名中定义的序列的特定元素。

例如,

# Example multi-model device configuration.
---
aliases:
  - &cisco_default
    vendor: cisco
    cmds:
      - terminal length 0                   # keep
      - show version | include Model number # keep
      - show boot | include BOOT path-list  # change this one below
      - "dir flash: | include bin$"         # and this one
      - quit                                # keep

config:
  - *cisco_default
  - <<: *cisco_default
    models:
      - c4500
      - c3650
    cmds:
      - show boot | include BOOT variable
      - "dir bootflash: | include bin$"

我正在使用Go处理并将YAML解组为结构。因此,如果纯YAML无法实现这种行为,是否有一种简单的方法可以使用Go的文本模板或类似方法来修改cmds序列?另外,我需要保留命令的顺序。

1 个答案:

答案 0 :(得分:0)

通过别名cmds来获得解决方案。这是一个允许按顺序循环命令的有效配置:

---
aliases:
  - &cisco_default
    vendor: cisco
    cmds: &cisco_cmds
      0: terminal length 0
      1: show version | include Model number
      2: show boot | include BOOT path-list
      3: "dir flash: | include bin$"
      4: quit

config:
  # Default Cisco configuration.
  - *cisco_default

  # Cisco 4500 and 3650 model configuration.
  - <<: *cisco_default
    models:
      - c4500
      - c3650
    cmds:
      <<: *cisco_cmds
      2: show boot | include BOOT variable
      3: "dir bootflash: | include bin$"