# Iterating over with\_items failing within blockinfile

**URL:** <https://forum.ansible.com/t/iterating-over-with-items-failing-within-blockinfile/25338>\
**Category:** Ansible Project\
**Created:** [August 25, 2017, 10:45pm UTC](https://forum.ansible.com/t/iterating-over-with-items-failing-within-blockinfile/25338 "2017-08-25T22:45:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [August 25, 2017, 10:45pm UTC](https://forum.ansible.com/t/iterating-over-with-items-failing-within-blockinfile/25338/1 "2017-08-25T22:45:07Z")

</div>

I have the following task/main.yml part of my playbook. Everything works fine except for the last section with the blockinfile. I assume I have some sort of a syntax issue.

`  
Enter code here…—

- name: Dependency resolution – Install selinux bindings  
yum:  
name: libselinux-python  
state: present

- name: Gather list of ifcfg-\* files  
shell: ls “{{ net\_path }}” | grep ^ifcfg- | grep -ve ifcfg-lo -e @ # Last piece (@) excludes ansible backup files  
register: ifcfg\_list  
changed\_when: false

- name: DEBUG  
debug:  
msg: “{{net\_path}}{{ item }}”  
with\_items: “{{ ifcfg\_list.stdout\_lines }}”

- name: Removing existing DNS entries from ifcfg-\* files  
lineinfile:  
path: “{{ net\_path }}{{ item }}”  
backup: yes  
state: absent  
regexp: ‘^DNS’  
with\_items: “{{ ifcfg\_list.stdout\_lines }}”

- name: Removing existing DNS entries from resolv.conf  
lineinfile:  
path: /etc/resolv.conf  
backup: yes  
state: absent  
regexp: ‘^nameserver’

- name: Update DNS in ifcfg files  
blockinfile:  
path: “{{ net\_path }}{{ item }}”  
marker: ‘# {mark} ANSIBLE MANAGED BLOCK – Manual changes will be overwritten’  
insertafter: EOF  
block: |  
DNS1=“{{ dns1 }}”  
DNS2=“{{ dns2 }}”  
with\_items: “{{ ifcfg\_list.stdout\_lines }}”

`

As you can see from the following results, the DEBUG iterates through it fine, but I fail in the Update DNS section. It says the variable is undefined, but I can’t see my problem. Wondering if someone can help me out.

`  
PLAY [ansibletest-oel6] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

TASK [Gathering Facts] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [ansibletest-oel6]

TASK [dns\_update : Dependency resolution – Install selinux bindings] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*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*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [ansibletest-oel6] =\> (item=ifcfg-eth0) =\> {  
“item”: “ifcfg-eth0”,  
“msg”: “/etc/sysconfig/network-scripts/ifcfg-eth0”  
}  
ok: [ansibletest-oel6] =\> (item=ifcfg-eth1) =\> {  
“item”: “ifcfg-eth1”,  
“msg”: “/etc/sysconfig/network-scripts/ifcfg-eth1”  
}

TASK [dns\_update : Removing existing DNS entries from ifcfg-\* files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [ansibletest-oel6] =\> (item=ifcfg-eth0)  
ok: [ansibletest-oel6] =\> (item=ifcfg-eth1)

TASK [dns\_update : Removing existing DNS entries from resolv.conf] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [ansibletest-oel6]

TASK [dns\_update : Update DNS in ifcfg files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [ansibletest-oel6]: FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘item’ is undefined\n\nThe error appears to have been in ‘/etc/ansible/roles/dns\_update/tasks/service.yml’: line 32, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Update DNS in ifcfg files\n ^ here\n”}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ansibletest-oel6 : ok=6 changed=0 unreachable=0 failed=1

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [August 25, 2017, 11:00pm UTC](https://forum.ansible.com/t/iterating-over-with-items-failing-within-blockinfile/25338/2 "2017-08-25T23:00:11Z")

</div>

> - name: Removing existing DNS entries from ifcfg-\* files  
> &nbsp;&nbsp;lineinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;path: "{{ net\_path }}{{ item }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;backup: yes  
> &nbsp;&nbsp;&nbsp;&nbsp;state: absent  
> &nbsp;&nbsp;&nbsp;&nbsp;regexp: '^DNS'  
> &nbsp;&nbsp;with\_items: "{{ ifcfg\_list.stdout\_lines }}"

\<snip /\>

> - name: Update DNS in ifcfg files  
> &nbsp;&nbsp;blockinfile:  
> &nbsp;&nbsp;&nbsp;&nbsp;path: "{{ net\_path }}{{ item }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;marker: '# {mark} ANSIBLE MANAGED BLOCK -- Manual changes will be  
> overwritten'  
> &nbsp;&nbsp;&nbsp;&nbsp;insertafter: EOF  
> &nbsp;&nbsp;&nbsp;&nbsp;block: |  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DNS1="{{ dns1 }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DNS2="{{ dns2 }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;with\_items: "{{ ifcfg\_list.stdout\_lines }}"

Your indentation is wrong, with\_items should be at the same level as  
blockinfile, just as you have in your with\_items in the lineinfile.

When you fix that I still thing it will fail. Since the maker: is the same on  
every loop it will just replace the same section.  
You would need to implement some kind of unique marker to make it work, one  
way is to incorporate the {{ item }}.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [August 28, 2017, 2:37pm UTC](https://forum.ansible.com/t/iterating-over-with-items-failing-within-blockinfile/25338/3 "2017-08-28T14:37:48Z")

</div>

Thanks again Kai Stian Olstad. You have been very helpful to me
