我试图仅替换特定字符串后的第一次出现。然而,ansible总是匹配所有的出现。文件如下:
这是我的可见任务:
- name: Update minPoolSize for CWTxDataSource dataSource
replace:
dest: "{{ op_db_path }}"
after: ".*CWTxDataSourceXA"
regexp: "^.*minPoolSize=.*$"
replace: ' <connectionManager maxPoolSize="750" minPoolSize="20" />'
backup: yes
<dataSource jndiName="CWTxDataSource">
<connectionManager maxPoolSize="750" minPoolSize="1" />
</dataSource>
<dataSource jndiName="UMDataSource">
<connectionManager maxPoolSize="750" minPoolSize="1" />
</dataSource>
在上面的示例中,两个connectionManager
标记都将得到更新,这不是期望的行为?我怎么能更新我的regexp
只更新第一场比赛??我尝试了replace
模块中的before
选项作为解决方案,但由于某种原因,这对我不起作用。
我在ansible docs中发现了下面这行:
# Prior to Ansible 2.7.10, using before and after in combination did the opposite of what was intended.
所以为了让我使用after
before
作为一个解决方案,我必须颠倒它们的值:
- name: Update minPoolSize for CWTxDataSource dataSource
# after / before values are reversed in ansible < 2.7
replace:
path: "{{ op_db_path }}"
before: 'jndiName="CWTxDataSource"'
after: "</dataSource>"
regexp: 'minPoolSize="d+"'
replace: 'minPoolSize="{{ CWTxDS_minPoolSize }}"'
注意after
before
的值没有真正的意义,但它的工作!