pur*_*aso 9 ansible ansible-2.x
我有一个剧本,如下所示:
- hosts: localhost
tasks:
- name: update a file
blockinfile:
dest: /tmp/test
block: |
line 1
line 2
Run Code Online (Sandbox Code Playgroud)
运行剧本后,文件/tmp/test变为:
a # this is the end line of the original file
# BEGIN ANSIBLE MANAGED BLOCK
line 1
line 2
# END ANSIBLE MANAGED BLOCK
Run Code Online (Sandbox Code Playgroud)
我想在标记“ # BEGIN ANSIBLE MANAGED BLOCK”之前添加一个空行(换行符)以获得视觉效果,最简单的方法是什么?最好在任务范围内,但欢迎任何想法。如果我重新定义标记,它将影响“BEGIN”和“END”标记。
如果您有一个如下所示的文件:
some line
foobar
some other line
Run Code Online (Sandbox Code Playgroud)
并且您想在本示例中的模式“foobar”之前添加换行符,以下 ansible 代码将为您完成此操作:
- hosts: localhost
gather_facts: no
connection: local
tasks:
- name: update a file
replace:
dest: /tmp/foobar
regexp: |
(?mx) # "m" lets ^ and $ match next to imbedded \n, x allows white space for readability
(?<!\n\n) # a Zero-Length Assertion that it does not have an empty line already
^ # beginning of the line
(foobar) # match "foobar" and save it as \1, it could be "\#\ BEGIN\ ANSIBLE\ MANAGED\ BLOCK" if that is your pattern
$ # end of line
replace: "\n\\1" # add a newline before the matched pattern
Run Code Online (Sandbox Code Playgroud)
并且它是幂等的,当模式不存在时,它不会在文件末尾添加换行符。
| 归档时间: |
|
| 查看次数: |
17167 次 |
| 最近记录: |