# how to add lines into a file

**URL:** <https://forum.ansible.com/t/how-to-add-lines-into-a-file/36335>\
**Category:** Ansible Project\
**Created:** [February 15, 2022, 1:37pm UTC](https://forum.ansible.com/t/how-to-add-lines-into-a-file/36335 "2022-02-15T13:37:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Praveen\_Dev](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@Praveen\_Dev](https://forum.ansible.com/u/Praveen_Dev)\
**Post date:** [February 15, 2022, 1:37pm UTC](https://forum.ansible.com/t/how-to-add-lines-into-a-file/36335/1 "2022-02-15T13:37:38Z")

</div>

HI All,

i have a requirement to add a few lines after a particular match / keyword or string in a shell script . below code is for reference when i execute this code it is adding all the bold lines at the bottom . can you please help thank you

- name: EDIT DIP Job Runner  
lineinfile:  
path: “{{ sas\_config }}/{{ sas\_config\_level }}/Web/Applications/SASWIPSchedulingServices9.4/dip/DIPJobRunner.sh”  
state: present  
insertafter: ‘kill $pid’  
line: “{{ item }}”  
with\_items:  
**- sleep 3**  
**- if [-f $PIDFILE]; then**  
**- kill -9 $pid**  
**- fi**  
become: true  
become\_user: ‘{{ install\_user.name }}’  
when: ‘“midtier” in group\_names’

---

<div class="post-metadata">

**Author:** ![Praveen\_Dev](https://avatars.discourse-cdn.com/v4/letter/p/ba9def/32.png) [@Praveen\_Dev](https://forum.ansible.com/u/Praveen_Dev)\
**Post date:** [February 23, 2022, 1:20pm UTC](https://forum.ansible.com/t/how-to-add-lines-into-a-file/36335/2 "2022-02-23T13:20:58Z")

</div>

any update please ?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [February 23, 2022, 1:57pm UTC](https://forum.ansible.com/t/how-to-add-lines-into-a-file/36335/3 "2022-02-23T13:57:53Z")

</div>

This means that the regex you provide doesn’t match:

[https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile\_module.html#parameter-insertafter](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html#parameter-insertafter)

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [February 23, 2022, 3:42pm UTC](https://forum.ansible.com/t/how-to-add-lines-into-a-file/36335/4 "2022-02-23T15:42:15Z")

</div>

For the regex to match, you’ll need to backslash-escape (with a single backslash, since you’re using single quotes) the dollar sign in your insertafter regex.

But even if you do that, it isn’t going to work. There are several problems. First, each line that gets added will be added immediately after the `insertafter` line, so the order will end up being backwards. Second, there already is a “fi” line after “kill $pid” in DIPJobRunner.sh, so your “fi” won’t be added. (Which is just as well, since they’re in the wrong order anyway.

You can probably get what you want if you drop the with\_items and change “line:” like this:  
line: “sleep 3; if [-f $PIDFILE] ; then kill -9 $pid ; fi”
