Jenkins需要安装Utility Steps插件
host.yaml
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("修改失败!")
Jenkins高级篇之Pipeline方法篇-Pipeline Utility Steps-4-方法readYaml和writeYaml