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?
From the link to the module manual you included in the question:
insertbefore
Used withstate=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 withbackrefs
.
So:
- lineinfile:
path: path/to/file
insertbefore: BOF
line: my_line
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With