如何确保在 Exec 资源执行命令之前重新启动服务?

How to ensure Service is restarted before commands from Exec resource execute?

(更新) 我在原来的问题中遗漏了一行重要的代码。

我正在编写一个将创建本地存储的人偶模块。非自动化的方法是。

  1. 编辑 /etc/multipath.conf 文件
  2. 重新启动 multipathd 守护进程
  3. 执行 pvcreate $device
  4. 执行 vgcreate $volume_name $device

所以在我的 puppet 模块中,我想确保在 /etc/multipath.conf 文件更改时重新启动 multipathd 守护进程。所以我这样写了我的人偶清单。

Service['multipathd'] -> Anchor['create_storage_volume::begin']

...

file { '/etc/multipath.conf':
    ensure  => file,
    content => template( 'local_storage::hadoop/multipath.conf.erb' ),
    owner   => 'root',
    mode    => '0600',
    notify  => Service['multipathd'],
} -> 
service { 'multipathd':
    enable => true,
    ensure => running,
}

anchor { 'create_storage_volume::begin': } ->

exec { "pvcreate ${device}":
    path   => ['/usr/sbin', '/bin'],
    unless => "pvs | grep ${"volume_name},
} ->
exec { "vgcreate ${volume_name} ${device}":
    path   => ['/usr/sbin', '/bin'],
    unless => "pvs | grep ${"volume_name},
} -> # -> do nova config stuff
anchor { 'create_storage_volume::end': }

...

我的问题是,"does the above code guarantee that the multipathd daemon gets restarted before the pvcreate and vgcreate commands are executed"?我是否需要添加更多资源订单,例如...

 Service['multipathd'] -> Anchor['local_storage::begin']

?

My question is, "does the above code guarantee that the multipathd daemon gets restarted before the pvcreate and vgcreate commands are executed"?

没有。 Service['multipathd']Exec["pvcreate ${device}"] 都将在 File['/etc/multipath.conf'] 之后应用,但在您所提供的服务和执行应用程序的相对顺序中没有任何内容。

我可能会这样写,而不是:

file { '/etc/multipath.conf':
    ensure  => file,
    content => template( 'local_storage::hadoop/multipath.conf.erb' ),
    owner   => 'root',
    mode    => '0600',
} ~>
Service['multipathd'] ->
exec { "pvcreate ${device}":
    path   => ['/usr/sbin', '/bin'],
    unless => "pvs | grep ${"volume_name},
} ->
# ...

注意the notifying chain operator的使用;这是 Puppet 语言中较少使用的功能。


关于问题的更新,在应用Exec之前要刷新Service的关键要求是两者之间存在顺序关系,无论是直接还是传递的。在修改后的问题中,涉及到的资源已经有这样的关系了。

原始答案确实掩盖了这里的一个要点,我在评论中指出了这一点:Puppet 实际上并没有明确记录相对于其他任何事情发生资源刷新的时间。记录的语义对此有一些影响,但没有给出明确的规则。实际上,通过 Puppet 4,如果发生资源刷新,它将在该资源同步后立即发生——或者如果需要的话,它会在同步后立即发生。