我需要修改文件中的一行。只有问题,这一行出现多次,但在不同的范围。如wso2配置手册中的示例所示:
<KeyStore>
<Location>${carbon.home}/resources/security/wso2carbon.jks</Location>
<Type>JKS</Type>
<Password>wso2carbon</Password>
<KeyAlias>wso2carbon</KeyAlias>
<KeyPassword>wso2carbon</KeyPassword>
</KeyStore>
<TrustStore>
<!-- trust-store file location -->
<Location>${carbon.home}/repository/resources/security/client-truststore.jks</Location>
<!-- trust-store type (JKS/PKCS12 etc.) -->
<Type>JKS</Type>
<!-- trust-store password -->
<Password>wso2carbon</Password>
</TrustStore>例如,我需要修改<Password>条目,在<Keystore>作用域中使用一个值,在<TrustStore>作用域中使用另一个不同的值,以便有不同的密码。lineinfile模块能做到这一点吗?或者还有别的办法吗?
PS。使用模板不是我想要的解决方案,因为我希望用它来修改现有的服务器,并且不会丢失任何本地修改。
发布于 2016-10-04 20:52:33
您不能用线状完成这一任务,因为它分别处理文件的每一行-因此不可能从其他行生成上下文。
lineinfile中的Regex不是多行的。
您可以使用替换 --它使用多行正则表达式。
例如:
- replace:
backup: yes
dest: config.xml
regexp: '(<{{ item.scope }}>[\S\s]*<Password>)(?!{{ item.password }}<).*(</Password>[\S\s]*</{{ item.scope }}>)'
replace: '\1{{ item.password }}\2'
with_items:
- scope: KeyStore
password: foo
- scope: TrustStore
password: bar请记住,这个解决方案不是防弹的-范围名称和密码不应该有任何XML特殊字符或正则表达式序列。此外,我也不能确定它如何处理具有相同作用域名称的嵌套XML块。
但在一般情况下,这应该是可以的。
甚至有一个幂等的尝试-如果密码是相同的,它将不匹配块。
发布于 2016-10-04 19:34:30
不确定的lineinfile regex参数可以匹配多行。这是没有问题的,除此之外,正则表达式将是长期和难以理解和维护的。
当您试图操作XML时,ansible-xml模块可能是解决此问题的一个好选择。
https://stackoverflow.com/questions/39858486
复制相似问题