簡體   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