# insert multiple lines at a particular location within a file

**URL:** <https://forum.ansible.com/t/insert-multiple-lines-at-a-particular-location-within-a-file/16736>\
**Category:** Ansible Project\
**Created:** [November 10, 2014, 8:33pm UTC](https://forum.ansible.com/t/insert-multiple-lines-at-a-particular-location-within-a-file/16736 "2014-11-10T20:33:43Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vince\_Skahan](https://avatars.discourse-cdn.com/v4/letter/v/5e9695/32.png) [@Vince\_Skahan](https://forum.ansible.com/u/Vince_Skahan)\
**Post date:** [November 10, 2014, 8:33pm UTC](https://forum.ansible.com/t/insert-multiple-lines-at-a-particular-location-within-a-file/16736/1 "2014-11-10T20:33:43Z")

</div>

I’m looking for a reasonable way to insert a block of lines into a certain position within a file hopefully ‘without’ needing to template the destination file.

I have a config file that I need to insert multiple lines into.

> [SectionA]  
> … many lines below here …  
> [SectionB]  
> … many more lines below here…  
> [SectionC]  
> … and so on…

What I’m trying to do is insert a block of lines just above the ‘SectionB’ line.

> [SectionA]  
> … many lines below here …

> **[[subsectionX]]**  
> **HTML\_ROOT = /path/here**  
> **[[subsectionY]]**  
> **HTML\_ROOT = /path/here**

[SectionB]  
… many more lines below here…  
[SectionC]

… and so on…

I tried a ‘with\_items’ approach ala:

> - name: insert the lines  
> lineinfile: dest=/config/file/path line=“{{ item }}” insertbefore=‘[SectionB]’  
> with\_items:
> - " [[subsectionX]]"
> - " HTML\_ROOT = /path/here"
> - " [[subsectionY]]"
> - " HTML\_ROOT = /path/here"

This struck me as a bit of a kludge, and it failed anyway as ansible only adds the first 'HTML\_ROOT =" line into the file, and does not add the second one (being a little too smart for its own good)

Is there a preferred way to do this kind of stuff ?
