lineinfile

The lineinfile module allows you to modify a file by adding or removing lines.

Arguments

  • path: a string that contains the path to the file to be modified.
  • line: a string that contains the line to be added or removed.
  • state: a string that contains the state of the line. Can be “present” or “absent”. (default is “present”)
  • pattern: a string that contains the pattern (regular expression) to search for in the file. (optional)
  • insert_after: a string that contains the line to insert the new line after. (optional)
  • insert_before: a string that contains the line to insert the new line before. (optional)
  • create: a boolean that indicates whether to create the file if it does not exist. (default is false)
  • backup: a boolean that indicates whether to create a backup of the file before modifying it. (default is false)

Usage Example

local task = {
    name = "Add a line in a file",
    komandan.modules.lineinfile({
        path = "/tmp/target_file.txt",
        line = "This is a new line",
        state = "present",
        create = true,
        backup = true,
    }),
}