Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert line at start of file with Ansible

Tags:

ansible

You can insert a line at the end of a file or before/after some regex with lineinfile. But how do I insert a line at the start of a file?

like image 604
giraffe Avatar asked Feb 21 '18 01:02

giraffe


1 Answers

From the link to the module manual you included in the question:

insertbefore  Used with state=present. If specified, the line will be inserted before the last match of specified regular expression. A value is available; BOF for inserting the line at the beginning of the file. If specified regular expression has no matches, the line will be inserted at the end of the file. May not be used with backrefs.

So:

- lineinfile:
    path: path/to/file
    insertbefore: BOF
    line: my_line
like image 88
techraf Avatar answered Oct 27 '22 01:10

techraf