Skip to content

Instantly share code, notes, and snippets.

@jctanner
Last active December 31, 2015 15:29
Show Gist options
  • Save jctanner/8007743 to your computer and use it in GitHub Desktop.
Save jctanner/8007743 to your computer and use it in GitHub Desktop.
lineinfile guide
<Zart> let me explain how it works at el (docs are pretty unclear): first, the file is split into lines. then it searches for regexp= pattern to find a place to insert line into. if it's not found, it looks for insertbefore/insertafter markers. if regexp was found replaces matched line with line= as-is, if not inserts into insertafter marker if provided or insertbefore if provided. in absence of those append to the end of file
<Zart> all that logic works with backrefs=no
<Zart> with backrefs=yes insertafter/before are irrelevant, and the regexp=/line= becomes like sed -e s/regexp/line/ instead
<Zart> with state=absent it looks for either regexp= match or literal line= match and removes the first hit if found
<Zart> and by "file is split into lines" I mean that pattern is always matched against single lines, never multiple. so matching 'one line\nsecond line' wont work
<Zart> if there are multiple matches state=absent will remove hits one-by-one on each run, so it's not really idempotent in that case. for replacing and inserting it should do so exactly once with this processing logic
<Zart> also when inserting multiple lines with insertbefore= tasks should be in reverse of intended order. this should be obvious
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment