Ansible 从变量中替换多行 [英] Ansible replace multiple lines from variable
问题描述
我有以下 Ansible 变量:
I have the following Ansible variable:
test_entries:
"
test-auth-ip=192.168.1.22
test-auth-serv=example1.com
test-auth-net=192.168.1.254
test-auth-blabla=test123
"
我还有一个包含以下几行的文本文件:
I also have a text file which contains the following lines:
test-auth-str1=null
test-auth-str2=null
test-auth-serv=null
test-auth-net=0.0.0.0
test-auth-str3=null
test-auth-str4=null
我希望能够替换文件中与变量中的行匹配的任何行,直到="符号.(正则表达式:^test-auth(.*?=))
I would like to be able to replace any line from the file which matches line from the variable until the "=" sign. (regex: ^test-auth(.*?=))
我阅读了 Ansible 文档.用于lineinfile"和替换"功能.然而,我找不到如何用正则表达式逐行匹配.
I read the Ansible doc. for "lineinfile" and "replace" functionalities. However, I couldn't find how to match line by line with regex.
预期结果
test-auth-str1=null
test-auth-str2=null
test-auth-serv=example1.com
test-auth-net=192.168.1.254
test-auth-str3=null
test-auth-str4=null
推荐答案
尝试
test_entries:
test-auth-ip : 192.168.1.22
test-auth-serv : example1.com
test-auth-net : 192.168.1.254
test-auth-blabla : test123
然后在你的任务中 -
- replace:
path: /your/file
regexp: '^{{ item.key }}=.*$'
replace: '{{ item.key }}={{ item.value }}'
with_dict: "{{ test_entries }}"
我也经常使用匿名字典数组,以及我命名的属性,而不是key
和value
.主要区别在于它可以让您控制顺序(这在这里似乎无关紧要)并跳过 key
&value
关键字.
I also often use an array of anonymous dictionaries, and whatever I named the attributes instead of key
and value
. The major differences are that it lets you control order (which doesn't seem to matter here) and skip the key
& value
keywords.
这篇关于Ansible 从变量中替换多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!