简体   繁体   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

I have a yaml file(config.yaml) with tags/structure similar to what is mentioned below.我有一个 yaml 文件(config.yaml),其标签/结构类似于下面提到的内容。 I need to add a new tenant(tenant3) to the list of the existing tenants.我需要将新租户 (tenant3) 添加到现有租户列表中。 How do I achieve it using the pipeline/groovy script?如何使用管道/groovy 脚本实现它? Any help/lead would be appreciated.任何帮助/领导将不胜感激。

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

I think you need to do something like that:我认为你需要做这样的事情:

@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))

So the steps are:所以步骤是:

  1. load the data from file into the memory将文件中的数据加载到 memory
  2. modify the structure in a way you like以您喜欢的方式修改结构
  3. store the modified structure to the file将修改后的结构存储到文件中

I hope it will help.我希望它会有所帮助。

暂无
暂无

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

相关问题 使用Pipeline Job和Groovy脚本创建新的Jenkins作业 - Create new Jenkins jobs using Pipeline Job and Groovy script 如何使用 Pipeline Utility Steps 插件更新现有 yaml 文件的内容 - How to update the content of an existing yaml file with Pipeline Utility Steps plugin Jenkins 管道 - 将 yaml 和 append 读取到列表 - Jenkins pipeline - Read yaml and append to a list 如何在Groovy ReactJS中的jenkins -pipeline脚本中归档新的构建文件 - How to archive new build files in jenkins -pipeline script in groovy ReactJS 管道语法:附加到现有文件而不使用 sh "" - Pipeline syntax: append to an existing file without using sh "" 当 jenkins 作业在从机上运行时,如何使用 Jenkinsfile 管道脚本从主机读取 yaml 文件? - How to read yaml file from master using Jenkinsfile pipeline script when jenkins job is running on slave machine? 如何从 jenkins 管道 groovy 脚本中的 curl 获取 json 列表 - How to get a json list from the curl in jenkins pipeline groovy script 如何在 Groovy/Jenkins 中循环遍历 YAML 列表文件 - How to loop through a YAML list file in Groovy/Jenkins 在Jenkins管道脚本中将另一个groovy文件中的类用作类型 - Using class from another groovy file as a type in a Jenkins pipeline script 使用 Jenkins 管道 docker 插件,如何拉取图像并将新标签推送给它? - Using the Jenkins pipeline docker plugin, how to pull an image and push a new tag to it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM