我写了一个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”字符串的文件。
答案 0 :(得分:1)
blockinfile模块不能在没有标记线的情况下工作
并且要使此模块正常工作,$("button.close").closest("panel").find(".panel-body").hide();
选项必须包含要用marker
/ {mark}
替换的BEGIN
字,以便随后的Ansible运行可以是幂等的。
正确使用END
可能是:
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
P.S。并且您可能不希望使用; 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
来生成配置,只需在内部使用一些循环制作一些config.j2模板以迭代您的项目并调用blockinfile
模块一次。
答案 1 :(得分:0)
将标记设置为"",它将在块的开头添加空行
- 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