[英]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:所以步骤是:
I hope it will help.我希望它会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.