"False" 被添加到块文件模块的块开头
"False" gets added at the start of block for blockfile module
我编写了 ansible 任务来生成具有相同模板块但具有一些不同属性值的单个文件。该任务能够按预期生成新文件,但 "False" 在每个块的开头添加。
这是我的代码:
- name: Test to generate config file
hosts: localhost
tasks:
- name: Blockfile test
blockinfile:
path: blockfile_test
create: yes
marker: no
insertafter: EOF
content: "{{ lookup('template', 'blockfile_template') }}"
with_items:
- One
- Two
- Three
模板:
This is test to generate new file
Count # {{ item }}
生成的文件:
False
This is test to generate new file
Count # One
False
This is test to generate new file
Count # Two
False
This is test to generate new file
Count # Three
False
ansible 命令输出:
$ ansible-playbook test.yml
PLAY [Test to generate config file] *************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [localhost]
TASK [Blockfile test] ***************************************************************************************************************************************
changed: [localhost] => (item=One)
changed: [localhost] => (item=Two)
changed: [localhost] => (item=Three)
PLAY RECAP **************************************************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
想要生成没有 "False" 字符串的文件。
blockinfile 模块不应该在没有标记线的情况下工作。
为了使该模块正常工作,marker
选项必须包含 {mark}
单词,该单词将被 BEGIN
/END
替换,以便后续的 Ansible 运行可以是幂等的。
blockinfile
的正确用法可以是:
- name: Blockfile test
blockinfile:
path: blockfile_test
create: yes
marker: "; {mark} block for item {{ item }}"
insertafter: EOF
content: "{{ lookup('template', 'blockfile_template') }}"
with_items:
- One
- Two
- Three
结果将是:
; BEGIN block for item One
This is test to generate new file
Count # One
; END block for item One
; BEGIN block for item Two
This is test to generate new file
Count # Two
; END block for item Two
; BEGIN block for item Three
This is test to generate new file
Count # Three
; END block for item Tree
P.S。而且您可能不想使用 blockinfile
生成配置,只需制作一些带有内部循环的 config.j2 模板来迭代您的项目并调用一次 template
模块。
将标记设置为“”,它会在块的开头添加空行
- name: Blockfile test
blockinfile:
path: blockfile_test
create: yes
marker: ""
insertafter: EOF
content: "{{ lookup('template', 'blockfile_template') }}"
with_items:
- One
- Two
- Three
输出:
This is test to generate new file
Count # One
This is test to generate new file
Count # Two
This is test to generate new file
Count # Three
我编写了 ansible 任务来生成具有相同模板块但具有一些不同属性值的单个文件。该任务能够按预期生成新文件,但 "False" 在每个块的开头添加。
这是我的代码:
- name: Test to generate config file
hosts: localhost
tasks:
- name: Blockfile test
blockinfile:
path: blockfile_test
create: yes
marker: no
insertafter: EOF
content: "{{ lookup('template', 'blockfile_template') }}"
with_items:
- One
- Two
- Three
模板:
This is test to generate new file
Count # {{ item }}
生成的文件:
False
This is test to generate new file
Count # One
False
This is test to generate new file
Count # Two
False
This is test to generate new file
Count # Three
False
ansible 命令输出:
$ ansible-playbook test.yml
PLAY [Test to generate config file] *************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [localhost]
TASK [Blockfile test] ***************************************************************************************************************************************
changed: [localhost] => (item=One)
changed: [localhost] => (item=Two)
changed: [localhost] => (item=Three)
PLAY RECAP **************************************************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0
想要生成没有 "False" 字符串的文件。
blockinfile 模块不应该在没有标记线的情况下工作。
为了使该模块正常工作,marker
选项必须包含 {mark}
单词,该单词将被 BEGIN
/END
替换,以便后续的 Ansible 运行可以是幂等的。
blockinfile
的正确用法可以是:
- name: Blockfile test
blockinfile:
path: blockfile_test
create: yes
marker: "; {mark} block for item {{ item }}"
insertafter: EOF
content: "{{ lookup('template', 'blockfile_template') }}"
with_items:
- One
- Two
- Three
结果将是:
; BEGIN block for item One
This is test to generate new file
Count # One
; END block for item One
; BEGIN block for item Two
This is test to generate new file
Count # Two
; END block for item Two
; BEGIN block for item Three
This is test to generate new file
Count # Three
; END block for item Tree
P.S。而且您可能不想使用 blockinfile
生成配置,只需制作一些带有内部循环的 config.j2 模板来迭代您的项目并调用一次 template
模块。
将标记设置为“”,它会在块的开头添加空行
- name: Blockfile test
blockinfile:
path: blockfile_test
create: yes
marker: ""
insertafter: EOF
content: "{{ lookup('template', 'blockfile_template') }}"
with_items:
- One
- Two
- Three
输出:
This is test to generate new file
Count # One
This is test to generate new file
Count # Two
This is test to generate new file
Count # Three