Ansible替换变量中的多行
我有以下Ansible变量:Ansible替换变量中的多行,ansible,Ansible,我有以下Ansible变量: 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 " 我还有一个文本文件,其中包含以下行: test-auth-str1=null test-auth-str2=null test-auth-serv=n
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
"
我还有一个文本文件,其中包含以下行:
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
我希望能够替换文件中与变量中的行匹配的任何行,直到“=”符号。(正则表达式:^测试验证(.*?=))
我读了那本书。用于“lineinfle”和“replace”功能。然而
我找不到如何逐行匹配正则表达式
预期结果
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
试一试
那么在你的任务中-
- replace:
path: /your/file
regexp: '^{{ item.key }}=.*$'
replace: '{{ item.key }}={{ item.value }}'
with_dict: "{{ test_entries }}"
我还经常使用匿名字典数组,以及我命名的任何属性,而不是
key
和value
。主要区别在于它允许您控制顺序(在这里似乎无关紧要)并跳过键
&值
关键字。我不确定是否理解您要替换的内容。您可以添加一个具有预期输出的部分吗?将原始字符串拆分为行(标记),循环调用replace
模块。
- replace:
path: /your/file
regexp: '^{{ item.key }}=.*$'
replace: '{{ item.key }}={{ item.value }}'
with_dict: "{{ test_entries }}"