I would like to add the following lines to all /etc/sudoers files across my environment:

# Administrators LDAP Group
%Administrators   ALL=(ALL)       ALL

However, each server has a different /etc/sudoers file, and sourcing them all from one location would be impractical.

Is there a way in Salt to ensure a single line (or group of lines) exists in a file, rather than managing that whole file?


You are probably looking for file.append.

File.append searches the whole file and if it is unable to find your text, it will append it to the end of the file. I am assuming that you don't know of any common text structures in you /etc/sudoers file so you won't be able to use something like file.sed to replace some text.

Sample Code:

    - text: 
      - "# Administrators LDAP Group"
      - "%Administrators   ALL=(ALL)       ALL"

NOTE: You should probably test the sample code before you use it.

| improve this answer | |
  • That was perfect! In my case I used a - source file rather than - text, but the principal is the same. – Soviero Jan 24 '14 at 22:43
  • For somwhat more sophisticated operations inside unmanaged files, I found file.blockreplace useful; this blog has a good tutorial – sxc731 Jan 12 '16 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.