Blockinfile first loop is not written

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.