donk68254 2018-07-19 23:26
浏览 94
已采纳

YAML修改别名序列元素

I'm working on a configuration file format for a program and I was wondering if it is possible to modify specific elements of a sequence defined in an alias.

For example,

# 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$"

I am using Go to process and unmarshal the YAML into a struct. So, if this behavior is not possible with plain YAML, is there an easy way to modify the cmds sequence using Go's text templates or something similar? Also, I need to preserve the order of the commands.

  • 写回答

1条回答 默认 最新

  • douji1877 2018-07-20 00:14
    关注

    Got a solution by aliasing the cmds. Here is a working configuration that allows looping the commands in order:

    ---
    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$"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题