# Lineinfile: muti-line, idempotency, regexp

**URL:** <https://forum.ansible.com/t/lineinfile-muti-line-idempotency-regexp/1626>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [October 11, 2023, 11:06am UTC](https://forum.ansible.com/t/lineinfile-muti-line-idempotency-regexp/1626 "2023-10-11T11:06:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 11, 2023, 11:06am UTC](https://forum.ansible.com/t/lineinfile-muti-line-idempotency-regexp/1626/1 "2023-10-11T11:06:12Z")

</div>

I am playing with lineinfile

### target file before

```bash
     1 line 1
     2 line 2
     3 line 3
     4 line 4
     5 line 5
     6 byebye

```

### My playboook

```yaml
---
- name: insert multi-line something
  hosts: "{{ targets | default('postgres') }}"
  tasks:

   - name: Insert multi-line content into a file
     lineinfile:
       path: /home/postgres/somefile_231011
       regexp: '^line 3'
       line: |
         this is my
         multi line
         content
     become: true
     become_user: postgres

```

### target after 1st run

(as expected)

```bash
     1 line 1
     2 line 2
     3 this is my
     4 multi line
     5 content
     6 line 4
     7 line 5
     8 byebye

```

### target file after second run

(this should be identical from the above, but adds my 3 lines plus an additional empty line at the bottom)

```bash
     1 line 1
     2 line 2
     3 this is my
     4 multi line
     5 content
     6 line 4
     7 line 5
     8 byebye
     9 this is my
    10 multi line
    11 content
    12

```

what am I missing?

- I though the `regexp` would make this idempotent (but apparently doesn’t work)
- Do I need additionally `insertberfore`/`insertafter`?

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [October 11, 2023, 1:09pm UTC](https://forum.ansible.com/t/lineinfile-muti-line-idempotency-regexp/1626/2 "2023-10-11T13:09:42Z")

</div>

I think this is correct behaviour. The doc states about `regexp`:

> If the regular expression is not matched, the line will be added to the file in keeping with `insertbefore` or `insertafter` settings.

Might you want the `blockinfile` module?

---

<div class="post-metadata">

**Author:** ![mophahr](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mophahr/32/760_2.png) [@mophahr](https://forum.ansible.com/u/mophahr)\
**Post date:** [October 11, 2023, 1:09pm UTC](https://forum.ansible.com/t/lineinfile-muti-line-idempotency-regexp/1626/3 "2023-10-11T13:09:56Z")

</div>

[Blockinfile](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/blockinfile_module.html) seems to be what you’re looking for.

---

<div class="post-metadata">

**Author:** ![dulhaver](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulhaver/32/1061_2.png) [@dulhaver](https://forum.ansible.com/u/dulhaver)\
**Post date:** [October 11, 2023, 2:00pm UTC](https://forum.ansible.com/t/lineinfile-muti-line-idempotency-regexp/1626/4 "2023-10-11T14:00:28Z")

</div>

> [@jpmens](#):
>
> I think this is correct behaviour. The doc states about `regexp`:
> 
> > If the regular expression is not matched, the line will be added to the file in keeping with `insertbefore` or `insertafter` settings.
> 
> Might you want the `blockinfile` module?

maybe, yes. Would you say the blobk I am rying to sneak in `lineinfile` with …

```auto
line: |
  here are
  my lines
  of code

```

… part of the problem, or is the takeaway that `lineinfile` is simpy **not idempotent** really ?

---

<div class="post-metadata">

**Author:** ![jpmens](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jpmens/32/5543_2.png) [@jpmens](https://forum.ansible.com/u/jpmens)\
**Post date:** [October 12, 2023, 7:18am UTC](https://forum.ansible.com/t/lineinfile-muti-line-idempotency-regexp/1626/5 "2023-10-12T07:18:26Z")

</div>

I think `lineinfile` is idempotent, but as your first run has removed the line you’re using as anchor in the regex, it has no other alternative than to append to the end of the file (or the beginning) depending on the `insert*` parameter.

> [@dulhaver](#):
>
> `regexp: '^line 3'`

---

<div class="post-metadata">

**Author:** ![valkiriaaquatica](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/valkiriaaquatica/32/1308_2.png) [@valkiriaaquatica](https://forum.ansible.com/u/valkiriaaquatica)\
**Post date:** [October 24, 2023, 9:28pm UTC](https://forum.ansible.com/t/lineinfile-muti-line-idempotency-regexp/1626/6 "2023-10-24T21:28:32Z")

</div>

the problem is with the regex,. In the second excution the line that matcht with the regex does not exist because it was replace in the first execution, so linefile does not found any match for that regex and it ends adding it to the end of the file

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 23, 2023, 9:29pm UTC](https://forum.ansible.com/t/lineinfile-muti-line-idempotency-regexp/1626/7 "2023-11-23T21:29:29Z")

</div>

Closed on November 23, 2023, 9:29pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
