[英]regex ansible lineinfile subsection
I am fairly new to ansible and have been solving the following problem with a shell script, but the proper way I believe is to use the lineinfile
module, just not sure how to accomplish this. 我对ansible相当陌生,并且一直在使用shell脚本解决以下问题,但是我认为正确的方法是使用
lineinfile
模块,只是不确定如何做到这一点。
Let's say I have a file with the following text in it. 假设我有一个文件,其中包含以下文本。
<cpu>
<alarm>
active = yes
</alarm>
</cpu>
<disk>
<alarm>
active = yes
<fixed>
<#>
active = yes
description = File system /
<inode_error>
active = yes
threshold = 2
message = InodeError
</inode_error>
</#>
<#boot>
active = yes
description = File system /boot
percent = yes
<error>
active = yes
threshold = 5
message = DiskError
</error>
</#boot>
</fixed>
</alarm>
</disk>
I want to make sure the following section is set correctly. 我要确保正确设置以下部分。
<disk><alarm><fixed><#boot><error>"threshold = 2"</error></#boot></fixed></alarm></disk>
is there a way to only (modify/make sure exists) that line, normally this file is much larger with many more sections, but I erased some so the question is readable. 有没有办法只(修改/确保存在)该行,通常此文件更大,包含更多部分,但我删除了一些内容,因此该问题可读。
Update: Modifying this as it is not valid XML and the XML module will not parse the file correctly. 更新:修改它,因为它不是有效的XML,并且XML模块将无法正确解析文件。
Thanks! 谢谢!
lineinfile
scans file per line, so you can't define complex multiline regexp for context definition. lineinfile
每行扫描文件,因此您无法为上下文定义定义复杂的多行正则表达式。
replace
module support multiline regexp. replace
模块支持多行正则表达式。
If you have threshold = X
in the file and want to be sure it is set to specific value, you can use this regexp: 如果文件中的
threshold = X
,并且要确保将其设置为特定值,则可以使用此regexp:
- replace:
path: ./test.txt
regexp: '(<disk>[\s\S]*<alarm>[\s\S]*<#boot>[\s\S]*<error>[\s\S]*)(threshold\s*=\s*\d+)([\s\S]*?<\/error>)'
replace: '\1threshold = 2\3'
It searches for line threshold\\s*=\\s*\\d+
inside <disk>...<alarm>...<#boot>...<error>...
. 它在
<disk>...<alarm>...<#boot>...<error>...
搜索线threshold\\s*=\\s*\\d+
。 This code is idempotent – so if threshold = 2
, then nothing is done. 该代码是幂等的-因此,如果
threshold = 2
,则什么也不做。
But if there is no threshold = X
string, it will fail. 但是,如果没有
threshold = X
字符串,它将失败。 You should construct more complex regular expression for that case. 您应该为这种情况构造更复杂的正则表达式。
you could use the lineinfile module ( http://docs.ansible.com/ansible/latest/lineinfile_module.html ) where you could write a regex to modify/add the line and use the validate function to run a command to ensure that the xml file has the proper syntax. 您可以使用lineinfile模块( http://docs.ansible.com/ansible/latest/lineinfile_module.html ),在其中可以编写正则表达式来修改/添加行,并使用validate函数运行命令以确保xml文件具有正确的语法。
If you are on Ansible 2.4 you can use the xml module ( https://docs.ansible.com/ansible/2.4/xml_module.html ) and use the attribute parameter to check if the xpath in xml file is set, like that: 如果您使用的是Ansible 2.4,则可以使用xml模块( https://docs.ansible.com/ansible/2.4/xml_module.html )并使用attribute参数来检查是否已设置xml文件中的xpath,如下所示:
- name: Read attribute value
xml:
path: /foo/bar.xml
xpath: /business/website/validxhtml
content: attribute
attribute: validatedon
register: xmlresp
regards 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.