5

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?

Soviero
  • 4,366
  • 8
  • 36
  • 60

1 Answers1

11

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:

/etc/sudoers:
  file.append:
    - text: 
      - "# Administrators LDAP Group"
      - "%Administrators   ALL=(ALL)       ALL"

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

Jason Zhu
  • 821
  • 7
  • 10
  • 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](http://makina-corpus.com/blog/metier/2014/saltstack-manage-entries-in-unmanaged-files-with-file-blockreplace) has a good tutorial – sxc731 Jan 12 '16 at 15:13