Ask Your Question

Remove lines from a file?

asked 2016-10-15 15:46:37 -0500

Cos gravatar image

updated 2016-10-31 12:24:48 -0500

Is there any way to ensure that a file must not contain any lines matching a particular substring or regex?

For example, say I want to ensure that file /etc/something.conf does not have any lines that begin with "riskyparam ..." I don't know what value riskyparam is set to, and I don't know how many times it appears in the file, but I want to remove all of them if any are there.

file_line with a match and ensure=>absent seemed at first like it might be a way to do this, but it turns out it doesn't work. You must have a line=> in the file_line, and that line is some specific string (even if it's expanded from variables or functions), not a regex or substring match. With ensure=>absent, file_line would remove anything matching that line completely. But when I don't know in advance what the offending line might contain, and want to remove all variations of it, that won't work.

Is there any way to do this?

Edit: Of course one can do pretty much anything with an exec, since you can write a script to do it. I'm looking for a way to avoid having an exec run a shell every single time the puppet agent runs.

edit retag flag offensive close merge delete



This sounds like a good use-case for the augeas resource type. I have not used it myself yet and you may need to write your own lens if your somthing.conf does not follow conventions.

pascal gravatar imagepascal ( 2016-10-18 10:10:07 -0500 )edit

I agree with pascal, your options are either an exec (with an onlyif/unless condition for idempotence) or an augeas lens.

DarylW gravatar imageDarylW ( 2016-11-02 08:50:45 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2016-10-17 12:00:54 -0500

How about an Exec resource to do an inline sed and replace the string with a space only if that string is present in the file?

edit flag offensive delete link more

answered 2016-10-17 07:35:31 -0500

puser gravatar image

If you ensure the state and content of something.conf then whenever a change is made to the file the md5 will not match the file that is saved in your configuration. The conf file will then be replaced with the something.conf with the correct content.

edit flag offensive delete link more


That doesn't allow for the rest of the file to be modified independently of this puppet, which is the whole point of what I'm trying to do.

Cos gravatar imageCos ( 2016-10-17 10:44:50 -0500 )edit

well the point of puppet is to not do what you are trying to do.

puser gravatar imagepuser ( 2016-10-18 06:15:44 -0500 )edit

answered 2016-10-18 23:07:54 -0500

rohithmn03 gravatar image
file { '/etc/something.cfg':
    ensure => 'present',
    notify => exec['replace_content'],
    replace => no,
    source => 

exec { 'replace_content':
    command => 'use sed command to do the job'
    path => '/usr/local/bin/:/bin/',

This may work, but may not be the puppet way of doing it. Just try it once..!

Best way is to just ensure that file is present..!

file { '/etc/something.cfg':
    ensure => 'present',
    source => 
    mode => '0644',
edit flag offensive delete link more


Thanks. I should've made it clear in my original post that I'm looking to do this *without* having to fall back to exec, which will shell out on every single agent run, whether it's needed or not. Sorry for omitting that; updated my post.

Cos gravatar imageCos ( 2016-10-31 12:25:46 -0500 )edit

you can add an onlyif or unless parameter to the exec that greps for riskyparam. onlyif => "/usr/bin/grep $riskyparam",

DarylW gravatar imageDarylW ( 2016-11-02 08:49:10 -0500 )edit

Doesn't an onlyif like that still cause puppet to run a shell every time, to run the onlyif command?

Cos gravatar imageCos ( 2016-11-07 15:04:59 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-10-15 15:46:37 -0500

Seen: 300 times

Last updated: Oct 31 '16