Ansible Playbook 将同一行多次添加到 yml 文件
Ansible Playbook adding the same line multiple times to yml file
我必须在同一文件中多次添加同一行。它必须始终插入特定行之后,这两个位置都相同。
我已经用 lineinfile
模块试过了,但是,总是只添加一行。我也尝试过 replace
模块,但是,根本没有添加任何行。
我的 lineinfile
任务如下所示:
- name: Add Line
ansible.builtin.lineinfile:
path: someyml.yml
insertbefore: "template:"
line: "{{ Cluster_API}}"
replace
任务如下所示:
- name: Replace
replace:
path: someyml.yml
regexp: '^(template:\n)(?!{{ Cluster_API }})'
replace: ' {{ Cluster_API }}\n'
我知道替换是怎么写的,它应该在 template:
之后插入一行,但它甚至没有这样做。
我已经用测试文件进行了测试:
abcdef
template:
toto
123
template:
ssss
template:
toto
剧本测试:
- name: "tips3"
hosts: localhost
tasks:
- name: replace 'template:' not followed by toto by 'template:\ntoto'
replace:
path: files/test.txt
regexp: '^(template:)(?!\n{{data}})'
replace: '\n{{data}}'
vars:
data: toto
结果:在第二个模板
之后只添加了一个toto
abcdef
template:
toto
123
template:
toto
ssss
template:
toto
我必须在同一文件中多次添加同一行。它必须始终插入特定行之后,这两个位置都相同。
我已经用 lineinfile
模块试过了,但是,总是只添加一行。我也尝试过 replace
模块,但是,根本没有添加任何行。
我的 lineinfile
任务如下所示:
- name: Add Line
ansible.builtin.lineinfile:
path: someyml.yml
insertbefore: "template:"
line: "{{ Cluster_API}}"
replace
任务如下所示:
- name: Replace
replace:
path: someyml.yml
regexp: '^(template:\n)(?!{{ Cluster_API }})'
replace: ' {{ Cluster_API }}\n'
我知道替换是怎么写的,它应该在 template:
之后插入一行,但它甚至没有这样做。
我已经用测试文件进行了测试:
abcdef
template:
toto
123
template:
ssss
template:
toto
剧本测试:
- name: "tips3"
hosts: localhost
tasks:
- name: replace 'template:' not followed by toto by 'template:\ntoto'
replace:
path: files/test.txt
regexp: '^(template:)(?!\n{{data}})'
replace: '\n{{data}}'
vars:
data: toto
结果:在第二个模板
之后只添加了一个toto
abcdef
template:
toto
123
template:
toto
ssss
template:
toto