regex - 多次将同一行添加到 yaml 文件中(ansible playbook)
问题描述
我想在特定的行之间多次添加一行。我尝试使用我的剧本,但只添加到最后一个,它应该与 From Earth 添加相同的行。
我的剧本
- name: Update the file
lineinfile:
dest: /sample/config.file
insertafter: ' Earth .* '
line: ' This is template'
我的初始文件
Hello World
[From Earth]
Hello World
[From Earth]
Hello World
[From Earth]
Hello World
[From Earth]
我的愿望输出
Hello World
[From Earth]
This is template
Hello World
[From Earth]
This is template
Hello World
[From Earth]
This is template
Hello World
[From Earth]
This is template
解决方案
尝试replace
:
- name: Replace
replace:
path: file.txt
regexp: 'Earth]\n(?!{{repl_str}})'
replace: 'Earth]\n{{repl_str}}'
vars:
repl_str: ' This is template\n'
推荐阅读
- java - GraalSDK 值作为 JSON 结构
- python - 当我想获取复选框的状态时,为什么要获取地址而不是值 - tk.IntVar
- python - Py(Spark) udf 给出 PythonException: 'TypeError: 'float' object is not subscriptable
- sql - SQL_Case 和何时
- c++ - Make a class template std::is_swappable
- java - Hibernate列别名生成问题
- angular - net::ERR_ABORTED 404(未找到)。供应商.js
- pywinauto - pywinauto - 在文件打开窗口中按“组织”按钮
- javascript - Bulma CSS 列在移动设备上反转
- css - 对齐左标签元素