lineinfile

Collection: builtin

Full Name: parallax.builtin.lineinfile

Aliases: ansible.builtin.lineinfile, lineinfile

Synopsis

Ensure a single line is present in, or absent from, a file

Parameters

NameTypeRequiredDefaultDescription
pathstringyesThe file to modify
statestringpresentWhether the line should be present or absent
linestringThe line to insert/replace
regexpstringRegular expression to match lines
search_stringstringLiteral string to match in lines
createboolfalseCreate file if it doesn’t exist
backupboolfalseCreate backup of original file
insertafterstringInsert line after matching regex
insertbeforestringInsert line before matching regex
firstmatchboolfalseUse first regex match
backrefsboolfalseUse regex backreferences
ownerstringFile owner
groupstringFile group
modestringFile permissions
validatestringCommand to validate file before saving

Example

- name: Disable password authentication for ssh
  lineinfile:
    path: /etc/ssh/sshd_config
    regexp: "^#?PasswordAuthentication"
    line: PasswordAuthentication no
    validate: /usr/sbin/sshd -t -f %s
    state: present