insert multiple lines at a particular location within a file

9 views
Skip to first unread message

Vince Skahan

unread,
Nov 10, 2014, 3:33:43 PM11/10/14
to ansible...@googlegroups.com
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 ?





Reply all
Reply to author
Forward
0 new messages