giraffe
giraffe

Reputation: 811

Insert line at start of file with 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?

Upvotes: 8

Views: 12524

Answers (1)

techraf
techraf

Reputation: 68639

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

Upvotes: 15

Related Questions