链接由文件更改触发的执行程序

Chaining execs to be triggered by changes in a file

我的要求是这样的:

  1. 在文件中查找更改 /tmp/file
  2. 如果有变化,请按以下顺序执行:
    • 运行 命令3
    • 运行 命令2
    • 运行 命令 1
  3. 如果文件 /tmp/file 没有变化,什么也不做。

我的代码是这样的:

exec { 'exec3':
  command => 'command3',
  require => File['file'],
}

exec { 'exec2':
  command => 'command2',
  require => Exec['exec3'],
}

exec { 'exec1':
  command     => 'command1',
  require     => Exec['exec2'],
  subscribe   => File['file'],
  refreshonly => true,
}

但是,不管/tmp/file有没有变化,command3和command2总是运行s。我该如何预防?当 /tmp/file.

没有变化时,我不希望 "require" 在 exec1 中成为 运行

您需要:首先,让所有的执行者订阅文件资源;其次,对于每个人来说,还需要他们之前的 exec 资源;第三,将每个 exec 设置为 refreshonly.

下面是一些代码:

file { 'file':
  ensure  => file,
  path    => '/tmp/file',
  content => "some content\n",
}

exec { 'exec1':
  command   => 'command1', 
  subscribe => File['file'],
  refreshonly => true,
}

exec { 'exec2':
  command   => 'command2',
  subscribe => File['file'],
  require   => Exec['exec1'],
  refreshonly => true,
} 

exec { 'exec3':
  command   => 'command3',
  subscribe => File['file'],
  require   => Exec['exec2'],
  refreshonly => true,
}

这是如何工作的:

  • 使用exec的refreshonly机制,exec1仅在刷新事件时触发,当且仅当file1的内容发生变化时才发送刷新事件。

  • 所有的exec事件都需要类似的由文件内容的变化触发,因此他们都订阅了文件。

  • 但是exec需要以特定的方式排序,因此exec2需要exec1,exec3需要exec2。

另请参阅reasons为什么需要谨慎使用 refreshonly。