Puppet file_line: remove line with unknown number of whitespaces


I'd like to remove a line from the sudoers. All works fine as long as I give the exact line. But there might be differences in white space on some machines.
Couldn't find a workaround even with "match".

file_line { '/etc/sudoers':
          ensure => absent,
          path   => '/etc/sudoers',
          line   => 'myuser  ALL=NOPASSWD:/bin/su -', }

Any ideas?

Best Answer

I would not use ensure => absent here as it only allows for an exact match. A simple but effective workaround would be using a comment (or empty) line along with a match regex to look for to replace:

file_line { 'sudoers-myuser':
          path   => '/etc/sudoers',
          line   => '# myuser  ALL=NOPASSWD:/bin/su -', 
          match  => 'myuser.*ALL=NOPASSWD:/bin/su.*-',

This would introduce line even if the match is not present in the file, but as the content is a no-op, it should not present any serious issues. Take care to escape regex special characters in the match line.