elegant way to hash a line in a file, if present

96 views
Skip to first unread message

Cam Mac

unread,
Jun 29, 2015, 10:42:18 AM6/29/15
to puppet...@googlegroups.com
Hi,

I wanted to create a recipe for hashing a line in a file (I'd rather hash it than delete it), if the file is present and that particular line is present. I came up with a less than elegant solution and wondered whether
someone knows of a better way.

The file in question: /usr/share/X11/xorg.conf.d/glamor.conf
The line in question (which needs to be commented):  Load  "glamoregl"

  exec { 'hash_glamor':
    path    => '/bin:/usr/sbin:/usr/bin',
    command => 'sed -i \'s/\(^\s*Load\s*\"glamoregl\"$\)/\#\1/\' /usr/share/X11/xorg.conf.d/glamor.conf',
    onlyif => 'grep -E "^\s*Load\s*\"glamoregl\"$" /usr/share/X11/xorg.conf.d/glamor.conf',
  }

I tried using the 'file_line' module:

  file_line { 'hash_glamor':
    path  => '/usr/share/X11/xorg.conf.d/glamor.conf',
    line  => '#      Load  "glamoregl"',
    match => 'Load  "glamoregl"',
  }

but this returns an error if the file doesn't exist, and I couldn't find a way of adding a conditional that will check whether the file exists in the 'file_line' module before it executes.

Augeas could be a possibility, but I couldn't see a way of doing it in a simple way.

Any suggestions welcome.

Thanks,

Cam

Tom Limoncelli

unread,
Jun 30, 2015, 9:41:54 AM6/30/15
to puppet...@googlegroups.com
You'll want to create the file using file{} and then add require =>
File['/path/to/file'] so that the file is created before Puppet tries
to modify it.

You can do that with less typing by using the -> operator. For example:

file { '/usr/share/X11/xorg.conf.d/glamor.conf':
ensure => file,
mode => '0644', # adjust as needed
owner => 'root', # adjust as needed
group => 'root', # adjust as needed
}->
file_line { 'hash_glamor':
path => '/usr/share/X11/xorg.conf.d/glamor.conf',
line => '# Load "glamoregl"',
match => 'Load "glamoregl"',
}

Tom
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/f0b10c43-3868-46f7-9257-fd1e795dc360%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Email: t...@whatexit.org Work: tlimo...@StackOverflow.com
Skype: YesThatTom
Blog: http://EverythingSysadmin.com

Cam Mac

unread,
Jul 7, 2015, 5:37:56 AM7/7/15
to puppet...@googlegroups.com
Hi Tom,

Thanks for the reply. I hadn't used the 'file' method as I didn't want to create the file if it wasn't there already, only modify it. Sorry I should have said that in my original post. It was this requirement that made it less than straightforward.

Cheers,

Cam
Reply all
Reply to author
Forward
0 new messages