繁体   English   中英

如何使用 groovy/pipeline 脚本将新标签 append 添加到 yaml 文件中现有标签的列表中

[英]How to append the new tag to the list of existing tag in the yaml file using groovy/pipeline script

我有一个 yaml 文件(config.yaml),其标签/结构类似于下面提到的内容。 我需要将新租户 (tenant3) 添加到现有租户列表中。 如何使用管道/groovy 脚本实现它? 任何帮助/领导将不胜感激。

consumer_services:
- security
- token
id: 10000
tenants:
  tenant_1:
    state: all
    web_token: true
    cluster_pairs:
    - cluster1
    datacenter: local
    client: CLIENT_TEST
  tenant_2:
    state: all
    web_token: true
    cluster_pairs:
    - cluster2
    datacenter: local
    client: CLIENT_TEST
base_network:
    subnets:
    - 10.160.10.10
    - 10.179.1.09

我认为你需要做这样的事情:

@Grab('org.yaml:snakeyaml:1.17')

import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.Yaml

def options = new DumperOptions()
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK)

Yaml yaml = new Yaml(options)

// load existing structure
def structure = yaml.load(new File("original.yml").text)

// modify the structure
structure.tenants.tenant_3 =
    [
        state        : 'all',
        web_token    : true,
        cluster_pairs: ['cluster3'],
        datacenter   : 'local',
        client       : 'CLIENT_TEST'
    ]

// save to the new file
new File("modified.yml").write(yaml.dump(structure))

所以步骤是:

  1. 将文件中的数据加载到 memory
  2. 以您喜欢的方式修改结构
  3. 将修改后的结构存储到文件中

我希望它会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM