groovy处理yaml文件,增加列表、字典
  TEZNKK3IfmPf 2023年11月14日 94 0

插件

Jenkins需要安装Utility Steps插件
groovy处理yaml文件,增加列表、字典

host.yaml

DB:
  type: mysql
  hostIP: 192.168.1.2

脚本

#!groovy

//读取yaml文件,转换为一个对象
def datas = readYaml file: '/etc/ansible/host.yaml'
println datas
println "数据库IP是"      //读取yaml文件中的值

//修改yaml文件的值,不能直接写到原有的文件,需删除旧文件
sh "rm -rf /etc/ansible/host.yaml"
datas.DB.hostIP = "192.168.1.3"		//修改值
datas.DB.port = [["name": "port1","value":3306],["name": "port2","value":3307]]		//增加一个列表
datas.DB.user = ["user1":123,"user2":456]		//增加一个字典
try {
     
       
    writeYaml file: '/etc/ansible/host.yaml', data: datas
    sh "cat /etc/ansible/host.yaml"
}
catch(Exception err)
{
     
       
	throw new Exception("修改失败!")
}

groovy处理yaml文件,增加列表、字典

参考文章

Jenkins高级篇之Pipeline方法篇-Pipeline Utility Steps-4-方法readYaml和writeYaml

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月14日   29   0   0 vimyaml
  TEZNKK3IfmPf   2023年11月12日   80   0   0 groovygradle
  TEZNKK3IfmPf   2023年11月12日   35   0   0 SpringBootyaml
  TEZNKK3IfmPf   2023年11月13日   20   0   0 k8syaml
  AnyLlCIhvKpr   2023年11月12日   45   0   0 pythonyaml
TEZNKK3IfmPf