# Find line in file

**URL:** <https://forum.ansible.com/t/find-line-in-file/35955>\
**Category:** Ansible Project\
**Created:** [November 3, 2021, 12:19am UTC](https://forum.ansible.com/t/find-line-in-file/35955 "2021-11-03T00:19:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [November 3, 2021, 12:19am UTC](https://forum.ansible.com/t/find-line-in-file/35955/1 "2021-11-03T00:19:20Z")

</div>

One of the most recommended things is to use “lineinfiles” with some special attributes for just finding a line in a file.

I have a weird issue with this. If I just lookup one line, like so:

- name: TEST - Check /tmp/out.log  
lineinfile:  
path: /tmp/out.log  
regexp: “^bla”  
state: absent  
check\_mode: yes  
changed\_when: false  
register: checklog  
failed\_when: not checklog.found

This sequence fails (as expected), because the “bla” line is not in the file. If I enter a string, which is in the file, it doesn’t fail. So far so good.

Now I wanted to examine several lines, but this never fails. “line1” to “line4” are in, “bla” not.  
But the result is OK…

- name: TEST - Check /tmp/out.log  
lineinfile:  
path: /tmp/out.log  
regexp: “{{ item }}”  
state: absent  
check\_mode: yes  
changed\_when: false  
register: checklog  
with\_items:
- {item: “^line1”}
- {item: “^line2”}
- {item: “^line3”}
- {item: “^line4”}
- {item: “^bla”}  
failed\_when: not checklog.found

Does that make sense at all?

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [November 3, 2021, 12:26am UTC](https://forum.ansible.com/t/find-line-in-file/35955/2 "2021-11-03T00:26:36Z")

</div>

Oops. Disregard please:

regexp: “{{ item.item }}”

---

<div class="post-metadata">

**Author:** ![Jorge\_Rua1](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@Jorge\_Rua1](https://forum.ansible.com/u/Jorge_Rua1)\
**Post date:** [November 3, 2021, 10:43am UTC](https://forum.ansible.com/t/find-line-in-file/35955/3 "2021-11-03T10:43:24Z")

</div>

If it never fails is just because you are iterating over items and registering the result of each iteration. So the condition is only evaluated only on the last iteration.

---

<div class="post-metadata">

**Author:** ![Neil\_Young](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@Neil\_Young](https://forum.ansible.com/u/Neil_Young)\
**Post date:** [November 3, 2021, 10:57am UTC](https://forum.ansible.com/t/find-line-in-file/35955/4 "2021-11-03T10:57:02Z")

</div>

No, the reason was the wrong “{{ item.item }}” reference.

Real world example:

- name: TEST - Check /tmp/out.log for some lines expected to be there  
tags: test  
lineinfile:  
path: /tmp/out.log  
regexp: “{{ item.item }}”  
state: absent  
check\_mode: yes  
changed\_when: false  
register: checklog  
with\_items:
- { item: “^v=1”}
- { item: “^s=Kurento Media Server” }
- { item: “^IceCandidateFound” }
- { item: “^IceGatheringDone” }  
failed\_when: not checklog.found

The failing line is “v=1”, because it is “v=0”. The rest of the lines except the last is there. Result:

TASK [TEST - Check /tmp/out.log for some lines expected to be there] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
failed: [ubuntuVM] (item={‘item’: ‘^v=1’}) =\> {“ansible\_loop\_var”: “item”, “backup”: “”, “changed”: false, “failed\_when\_result”: true, “found”: 0, “item”: {“item”: “^v=1”}, “msg”: “”}  
ok: [ubuntuVM] =\> (item={‘item’: ‘^s=Kurento Media Server’}) =\> {“ansible\_loop\_var”: “item”, “backup”: “”, “changed”: false, “failed\_when\_result”: false, “found”: 1, “item”: {“item”: “^s=Kurento Media Server”}, “msg”: “1 line(s) removed”}  
ok: [ubuntuVM] =\> (item={‘item’: ‘^IceCandidateFound’}) =\> {“ansible\_loop\_var”: “item”, “backup”: “”, “changed”: false, “failed\_when\_result”: false, “found”: 12, “item”: {“item”: “^IceCandidateFound”}, “msg”: “12 line(s) removed”}  
failed: [ubuntuVM] (item={‘item’: ‘^IceGatheringDone’}) =\> {“ansible\_loop\_var”: “item”, “backup”: “”, “changed”: false, “failed\_when\_result”: true, “found”: 0, “item”: {“item”: “^IceGatheringDone”}, “msg”: “”}

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ubuntuVM : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

So it works as expected

Solved.
