# Blockinfile first loop is not written

**URL:** <https://forum.ansible.com/t/blockinfile-first-loop-is-not-written/24561>\
**Category:** Ansible Project\
**Created:** [May 24, 2017, 7:45am UTC](https://forum.ansible.com/t/blockinfile-first-loop-is-not-written/24561 "2017-05-24T07:45:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rcst.eoin.kim](https://avatars.discourse-cdn.com/v4/letter/r/f19dbf/32.png) [@rcst.eoin.kim](https://forum.ansible.com/u/rcst.eoin.kim)\
**Post date:** [May 24, 2017, 7:45am UTC](https://forum.ansible.com/t/blockinfile-first-loop-is-not-written/24561/1 "2017-05-24T07:45:36Z")

</div>

Hi all,

I am having problem with looping Blockinfile module. It seems as if the first loop doesn’t insert a new block into the target file. When I run the playbook the log shows that new block is inserted but it actually doesn’t exist. When I run the same playbook again, the block is inserted. I am wondering if this is a bug or I am using the Ansible incorrectly.

The code is:

- name: update zones on master nodes  
blockinfile:  
dest: /etc/icinga2/zones.conf  
backup: yes  
marker: “// {mark} configuration for {{ groupname }}”  
block: |  
{% for host in groups[groupname] %}  
object Endpoint “{{ hostvars[host].ansible\_fqdn }}” {  
host = “{{ hostvars[host].ansible\_eth0.ipv4.address }}”  
}

{% endfor %}

object Zone “{{ groupname }}” {  
endpoints = [{% for host in groups[groupname] -%} “{{ hostvars[host].ansible\_fqdn }}” {%- if not loop.last %}, {% endif -%} {%- endfor %} ]

parent = “master”  
}  
insertafter: ‘satellite configuration’  
delegate\_to: “{{ item }}”  
vars:  
ansible\_become\_pass: “{{ hostvars[item].ansible\_become\_pass }}”  
with\_items:

- “{{ groups[‘icinga2-master’] }}”  
when: inventory\_hostname == groups[groupname][0]  
notify: restart icinga2 on master

Does this mean to do like that? When I run with -vvv option, I can see the correct blocks in stdout but it actually is not written to the target file. Could I please get some advice regarding this issue? Thanks a lot.
