# How to loop through blockinfile?

**URL:** <https://forum.ansible.com/t/how-to-loop-through-blockinfile/21613>\
**Category:** Ansible Project\
**Created:** [May 31, 2016, 5:50pm UTC](https://forum.ansible.com/t/how-to-loop-through-blockinfile/21613 "2016-05-31T17:50:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdelaporte](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jdelaporte/32/366_2.png) [@jdelaporte](https://forum.ansible.com/u/jdelaporte)\
**Post date:** [May 31, 2016, 5:50pm UTC](https://forum.ansible.com/t/how-to-loop-through-blockinfile/21613/1 "2016-05-31T17:50:29Z")

</div>

I have the following task, which appears to execute successfully, but I end up with only one block (the last), while I actually want to have a block for each item entered into my file. How do I do that?

task:

- name: configure dns zone SRV records  
delegate\_to: 127.0.0.1  
blockinfile:  
dest: /home/ansible/env/playbooks/vars/bind.yml  
state: present  
backup: yes  
insertafter: ‘^bind\_zone\_services:.\*’  
marker: “# {mark} ANSIBLE MANAGED SRV BLOCK”  
block: |
- name: “{{ item.name }}”  
weight: 100  
port: “{{ item.port }}”  
target: “{{ ansible\_hostname }}”  
with\_items:
- { name: \_ldap.\_tcp, port: 389 }
- { name: \_kerberos.\_tcp, port: 88 }
- { name: \_kerberos.\_udp, port: 88 }
- { name: \_kerberos-master.\_tcp, port: 88 }
- { name: \_kerberos-master.\_udp, port: 88 }
- { name: \_kpasswd.\_tcp, port: 464 }
- { name: \_kpasswd.\_udp, port: 464 }
- { name: \_ntp.\_udp, port: 123 }

Result

- name: \_ntp.\_udp  
weight: 100  
port: 123  
target: hostname
