大网数据广告活动
服务器运维问答

How to prevent Puppet's file_line resource from always triggering an event?

· 52CE 我爱测

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-ser…

问题原文

原帖链接 · pedroapero · CC BY-SA 3.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

已采纳解答

原帖链接 · pedroapero · CC BY-SA 3.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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。