在ansible中如何在文件末尾添加一个文本块,标记前有一个空行?
In ansible how to add a block of text at end of a file with a blank line before the marker?
我有一个如下所示的剧本:
- hosts: localhost
tasks:
- name: update a file
blockinfile:
dest: /tmp/test
block: |
line 1
line 2
根据 运行 剧本,文件 /tmp/test
变为:
a # this is the end line of the original file
# BEGIN ANSIBLE MANAGED BLOCK
line 1
line 2
# END ANSIBLE MANAGED BLOCK
为了视觉效果,我想在标记“# BEGIN ANSIBLE MANAGED BLOCK
”前加一个空行(换行符),最简单的方法是什么?最好在任务内,但欢迎任何想法。如果我重新定义标记,它将影响“BEGIN”和“END”标记。
如果您使用的是 Ansible 2.5 或更高版本,您可以更改 marker
, marker_begin
and marker_end
.
这是一个示例剧本:
- hosts: all
gather_facts: no
tasks:
- blockinfile:
dest: /tmp/test
marker: '{mark} ANSIBLE MANAGED BLOCK'
marker_begin: '\n# BEGIN'
marker_end: '# END'
block: |
line 1
line 2
这产生了回顾:
PLAY [all] ********************************************************************************************************
TASK [blockinfile] ************************************************************************************************
changed: [localhost]
PLAY RECAP ********************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
文件 /tmp/test 最终为:
a
# BEGIN ANSIBLE MANAGED BLOCK
line 1
line 2
# END ANSIBLE MANAGED BLOCK
使用lineinfile。例如
- hosts: localhost
tasks:
- name: update a file
blockinfile:
dest: /tmp/test
block: |
line 1
line 2
- name: insert empty line before the marker
lineinfile:
dest: /tmp/test
insertbefore: '^# BEGIN ANSIBLE MANAGED BLOCK$'
line: ''
不幸的是,insertbefore 不适用于更多块。 模板 如果您坚持块之间有空行,则可能需要模块。
试试下面的剧本。不幸的是,EOF 没有按预期工作
shell> cat manage-block.yml
- name: "insert {{ my_marker }} in {{ my_dest }}"
blockinfile:
dest: "{{ my_dest }}"
marker: "# {mark} ANSIBLE MANAGED BLOCK {{ my_marker }}"
block: "{{ my_block }}"
- name: "insert empty line before {{ my_marker }}"
lineinfile:
dest: "{{ my_dest }}"
insertbefore: '^# BEGIN ANSIBLE MANAGED BLOCK {{ my_marker }}'
line: 'empty line'
when: ansible_loop.first
- name: "insert empty line after EOF"
lineinfile:
dest: "{{ my_dest }}"
insertafter: EOF
line: 'empty line'
- hosts: localhost
vars:
my_blocks:
/tmp/test:
- my_marker: block001
my_block: |
line 1
line 2
- my_marker: block002
my_block: |
line 3
line 4
tasks:
- include_tasks: manage-block.yml
with_subelements:
- "{{ my_blocks|dict2items }}"
- value
loop_control:
extended: true
vars:
my_dest: "{{ item.0.key }}"
my_marker: "{{ item.1.my_marker }}"
my_block: "{{ item.1.my_block }}"
下面在pattern前加一个空行,是幂等的,当pattern不存在时不在文件末尾添加。
- hosts: localhost
gather_facts: no
tasks:
- name: update a file
replace:
dest: /tmp/foobar
regexp: '(?smx) (?<!\n\n) ^ (foobar)$'
replace: "\n\1"
我有一个如下所示的剧本:
- hosts: localhost
tasks:
- name: update a file
blockinfile:
dest: /tmp/test
block: |
line 1
line 2
根据 运行 剧本,文件 /tmp/test
变为:
a # this is the end line of the original file
# BEGIN ANSIBLE MANAGED BLOCK
line 1
line 2
# END ANSIBLE MANAGED BLOCK
为了视觉效果,我想在标记“# BEGIN ANSIBLE MANAGED BLOCK
”前加一个空行(换行符),最简单的方法是什么?最好在任务内,但欢迎任何想法。如果我重新定义标记,它将影响“BEGIN”和“END”标记。
如果您使用的是 Ansible 2.5 或更高版本,您可以更改 marker
, marker_begin
and marker_end
.
这是一个示例剧本:
- hosts: all
gather_facts: no
tasks:
- blockinfile:
dest: /tmp/test
marker: '{mark} ANSIBLE MANAGED BLOCK'
marker_begin: '\n# BEGIN'
marker_end: '# END'
block: |
line 1
line 2
这产生了回顾:
PLAY [all] ********************************************************************************************************
TASK [blockinfile] ************************************************************************************************
changed: [localhost]
PLAY RECAP ********************************************************************************************************
localhost : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
文件 /tmp/test 最终为:
a
# BEGIN ANSIBLE MANAGED BLOCK
line 1
line 2
# END ANSIBLE MANAGED BLOCK
使用lineinfile。例如
- hosts: localhost
tasks:
- name: update a file
blockinfile:
dest: /tmp/test
block: |
line 1
line 2
- name: insert empty line before the marker
lineinfile:
dest: /tmp/test
insertbefore: '^# BEGIN ANSIBLE MANAGED BLOCK$'
line: ''
不幸的是,insertbefore 不适用于更多块。 模板 如果您坚持块之间有空行,则可能需要模块。
试试下面的剧本。不幸的是,EOF 没有按预期工作
shell> cat manage-block.yml
- name: "insert {{ my_marker }} in {{ my_dest }}"
blockinfile:
dest: "{{ my_dest }}"
marker: "# {mark} ANSIBLE MANAGED BLOCK {{ my_marker }}"
block: "{{ my_block }}"
- name: "insert empty line before {{ my_marker }}"
lineinfile:
dest: "{{ my_dest }}"
insertbefore: '^# BEGIN ANSIBLE MANAGED BLOCK {{ my_marker }}'
line: 'empty line'
when: ansible_loop.first
- name: "insert empty line after EOF"
lineinfile:
dest: "{{ my_dest }}"
insertafter: EOF
line: 'empty line'
- hosts: localhost
vars:
my_blocks:
/tmp/test:
- my_marker: block001
my_block: |
line 1
line 2
- my_marker: block002
my_block: |
line 3
line 4
tasks:
- include_tasks: manage-block.yml
with_subelements:
- "{{ my_blocks|dict2items }}"
- value
loop_control:
extended: true
vars:
my_dest: "{{ item.0.key }}"
my_marker: "{{ item.1.my_marker }}"
my_block: "{{ item.1.my_block }}"
下面在pattern前加一个空行,是幂等的,当pattern不存在时不在文件末尾添加。
- hosts: localhost
gather_facts: no
tasks:
- name: update a file
replace:
dest: /tmp/foobar
regexp: '(?smx) (?<!\n\n) ^ (foobar)$'
replace: "\n\1"