如何用 Ansible 替换 XML 元素的文本?

问题描述 投票:0回答:1
xml ansible xml-parsing
1个回答
0
投票

尝试 powershell 脚本

using assembly System.Xml.Linq
$input_filename = 'c:\temp\test.xml'
$output_filename = 'c:\temp\test1.xml'

$doc = [System.Xml.Linq.XDocument]::Load($input_filename)
$files = $doc.Descendants('Files')

$foos = [System.Linq.Enumerable]::Where($files, [Func[object,bool]]{ param($x) [string]$x.Value.StartsWith('public/foo')})
$pattern = '(public/foo-)(.*)(.jar)'
$newVersion = '1.2.1'

foreach($foo in $foos)
{
   $version = $foo[0].Value
   $newValue = $version -replace $pattern, '$1xyz$3'
   $newValue = $newValue -replace 'xyz', "$newVersion"

   $foo.FirstNode.SetValue($newValue)
}
$doc.Save($output_filename)
© www.soinside.com 2019 - 2024. All rights reserved.