# lineinfile with\_items only updates last line, despite four changed messages

**URL:** <https://forum.ansible.com/t/lineinfile-with-items-only-updates-last-line-despite-four-changed-messages/14796>\
**Category:** Ansible Project\
**Created:** [March 17, 2014, 6:51pm UTC](https://forum.ansible.com/t/lineinfile-with-items-only-updates-last-line-despite-four-changed-messages/14796 "2014-03-17T18:51:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Conlin](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@Mark\_Conlin](https://forum.ansible.com/u/Mark_Conlin)\
**Post date:** [March 17, 2014, 6:51pm UTC](https://forum.ansible.com/t/lineinfile-with-items-only-updates-last-line-despite-four-changed-messages/14796/1 "2014-03-17T18:51:44Z")

</div>

I would like to add a few lines to the end of a config file using lineinfile and with\_items.

When I run this yaml, I see changed log messages for all four entries (see below). However when I look at the file, only the very last line has been added.  
Your thoughts?

Thanks,  
Mark

`  
vars:  
limits\_lines:

- { key: ‘’, value: ‘root soft nofile 65535’}
- { key: ‘’, value: ‘root hard nofile 65535’}
- { key: ‘’, value: ‘root soft memlock unlimited’}
- { key: ‘’, value: ‘root hard memlock unlimited’}

tasks:

- name: append string  
lineinfile: dest=/etc/security/limits.conf  
regexp={{item.key}}  
insertafter=EOF  
line=“{{item.value}}”  
with\_items: limits\_lines  
`

Output:

`<127.0.0.1> REMOTE_MODULE lineinfile dest=/etc/security/limits.conf regexp= insertafter=EOF line="root soft nofile 65535" changed: [machine] => (item={'key': '', 'value': 'root soft nofile 65535'}) => {"backup": "", "changed": true, "item": {"key": "", "value": "root soft nofile 65535"}, "msg": "line replaced"} <127.0.0.1> REMOTE_MODULE lineinfile dest=/etc/security/limits.conf regexp= insertafter=EOF line="root hard nofile 65535" changed: [machine] => (item={'key': '', 'value': 'root hard nofile 65535'}) => {"backup": "", "changed": true, "item": {"key": "", "value": "root hard nofile 65535"}, "msg": "line replaced"} <127.0.0.1> REMOTE_MODULE lineinfile dest=/etc/security/limits.conf regexp= insertafter=EOF line="root soft memlock unlimited" changed: [machine] => (item={'key': '', 'value': 'root soft memlock unlimited'}) => {"backup": "", "changed": true, "item": {"key": "", "value": "root soft memlock unlimited"}, "msg": "line replaced"} <127.0.0.1> REMOTE_MODULE lineinfile dest=/etc/security/limits.conf regexp= insertafter=EOF line="root hard memlock unlimited" changed: [machine] => (item={'key': '', 'value': 'root hard memlock unlimited'}) => {"backup": "", "changed": true, "item": {"key": "", "value": "root hard memlock unlimited"}, "msg": "line replaced"}`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [March 21, 2014, 12:20pm UTC](https://forum.ansible.com/t/lineinfile-with-items-only-updates-last-line-despite-four-changed-messages/14796/2 "2014-03-21T12:20:25Z")

</div>

I believe what you are doing is constantly overwriting the last line.

I think, based on the examples and my testing, you would simply want to do:

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [March 21, 2014, 1:10pm UTC](https://forum.ansible.com/t/lineinfile-with-items-only-updates-last-line-despite-four-changed-messages/14796/3 "2014-03-21T13:10:47Z")

</div>

The regexp needs to match the line being added.
