0

I use the file_line resource from puppetlabs/stdlib to add lines to a file with Puppet. I would like to trigger a service reload when the file_line actually changes the file. Here is what I've tried:

First try

service { 'test-service':
  ensure => 'running',
  enable => true
}
file_line { 'test':
  path   => '/file'
  line   => 'test parameter'
  notify => Service['test-service']
}

Second try

service { 'test-service':
  ensure => 'running',
  enable => true
}
file_line { 'test':
  path   => '/file'
  line   => 'test parameter'
}~>Service['test-service']

Or similar with File_line['test']~>Service['test-service'] on the header.

Third try

file { '/file':
  path   => '/file',
  notify => Service['test-service']
}
service { 'test-service':
  ensure => 'running',
  enable => true
}
file_line { 'test':
  path   => '/file'
  line   => 'test parameter'
}

Fourth try

service { 'test-service':
  ensure    => 'running',
  enable    => true,
  subscribe => File_line['test']
}
file_line { 'test':
  path   => '/file'
  line   => 'test parameter'
}

In each case, file_line always trigger the service reload, even when nothing has changed in the file. It seems file_line is first removing all lines, and then re-adding them. Playing with the match option didn't change this behaviour.

How to make it trigger the service reload only if the file has changed?

Edit: here is the output of puppet agent --test:

Notice: /Stage[main]/Profile::testprofile/File_line[test]/ensure: created
Info: /Stage[main]/Profile::testprofile/File_line[test]: Scheduling refresh of Service[test-service]
Notice: /Stage[main]/Profile::testprofile/Service[test-service]: Triggered 'refresh' from 1 events
  • 1
    Can you add the output of puppet agent -t to your question? – Flup Sep 27 '16 at 9:39
  • I added the output, but I figured it seems to be a bug, I'll reply my own question. Thanks for comment. – pedroapero Sep 27 '16 at 10:23
0

OK I figured this had to do with the content I was writing to the file! If you try to add carriage returns using file_line, it will always trigger a refresh:

file_line { 'test':
  path => '/file',
  line => 'first param\nsecond param'
}

If I remove the \n, the problem disappear. So better call file_line twice than once with carriage returns.

  • Or you can use the double-quote around the line parameter, as double-quotes respect line-breaks within them, and single-quotes do not. – Craig Watson Sep 29 '16 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.