V Mahaja
V Mahaja

Reputation: 365

"False" gets added at the start of block for blockfile module

I wrote ansible task to generate single file with same template block but with some different attribute values. The task is able to generate new file as expected but "False" get added at the start of each block.

Here is my code:

- 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

template:

This is test to generate new file
        Count # {{ item }}

generated file:

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 command output:

$ 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

Want to generate file without "False" string.

Upvotes: 1

Views: 1409

Answers (2)

V Mahaja
V Mahaja

Reputation: 365

Set marker to "", it will add blank line at the start of the block

- name: Blockfile test
  blockinfile:
    path: blockfile_test
    create: yes
    marker: ""
    insertafter: EOF
    content: "{{ lookup('template', 'blockfile_template') }}"
  with_items:
    - One
    - Two
    - Three

output:

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

Upvotes: 0

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

blockinfile module is not supposed to work without marker line.
And for this module to work correctly marker option must contain {mark} word that is to be replaced with BEGIN/END so subsequent Ansible runs can be idempotent.

Correct use of blockinfile could be:

- 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

And the result will be:

; 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. And you probable don't want to use blockinfile to generate configs, just make some config.j2 template with some loop inside to iterate over your items and call template module once.

Upvotes: 1

Related Questions