Ask Your Question
0

File_line keeps appending and appending and appending to file

asked 2018-08-10 14:28:25 -0600

wsanders1 gravatar image

updated 2018-08-10 14:46:45 -0600

I have this snippet in my class:

   file_line { 'addfoobar':
    path => '/opt/splunkforwarder/etc/system/local/server.conf',
    line => "\n[httpServer]\ndisableDefaultPort = true",
    subscribe => Package['splunkforwarder'],
    notify => Service['splunkforwarder'],
    }

Each time puppet runs, it appends the two lines to the file, and keeps appending and appending and appending.....

[httpServer]
disableDefaultPort = true

[httpServer]
disableDefaultPort = true

[httpServer]
disableDefaultPort = true

[httpServer]
disableDefaultPort = true
....etc

I can work around it with ->, but why on earth would this be the default behavior?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-08-17 23:54:17 -0600

fastpuppet gravatar image

puppetlabs stdlib does use the line as the default match. I think the issue above is the multiple lines which really does not work well with it. The usually suggested way to handle multi-line is to use erb templates. This isn't always the best route because then puppet owns the entire file which is not always desired.

The workaround I use most frequently is to chain together multiple file_line resources:

file_line {
"Add puppet notice to /home/${user}/${file}":
  path => "/home/${user}/${file}",
  line => '## UMASK Value in this file is controlled by Puppet. Please do NOT edit this value directly. ##';

"Configure umask for ${user}":
  path     => "/home/${user}/${file}",
  line     => "umask ${umask}",
  match    => '^umask.*',
  after    => '^## UMASK Value in this file is controlled by Puppet',
  require  => File_line["Add puppet notice to /home/${user}/${file}"];

}

In that example, I am writing the default umask in a users bash profile. It ensures that the puppet notice is always on the line above the umask, and any line starting with umask is replaced with whatever is in puppet. It is not the most elegant way to do this and others probably have better ways, but it works for me.

edit flag offensive delete link more
0

answered 2018-08-15 12:05:04 -0600

MichaelSmith gravatar image

The way to avoid this, according to the file_line docs, is to include a match regex. Why it doesn't use the line value as a default match, I don't know.

edit flag offensive delete link more

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

Stats

Asked: 2018-08-10 14:28:25 -0600

Seen: 302 times

Last updated: Aug 17