在“vagrant resume”后触发 NTP 重启

在“vagrant resume”后触发 NTP 重启

我使用 Vagrant 管理一些 Debian 11 服务器虚拟机。有时我需要vagrant suspend它们,我注意到在之后vagrant resume,它们的系统时间关闭了。运行systemctl status ntp显示 NTP 服务因故障而关闭。我可以通过之后手动重新启动 NTP 来解决这个问题vagrant resume,所以我想在 Vagrantfile 中添加一个触发器:

config.trigger.after :resume do |trigger|
  trigger.info = "Restart NTP"
  trigger.run = {inline: "bash -c 'systemctl start ntp'"}
end

...但是现在vagrant resume抛出一个错误:

==> xxx-vm: Running action triggers after resume ...
==> xxx-vm: Running trigger...
==> xxx-vm: Restart NTP
    xxx-vm: Running local: Inline script
    xxx-vm: bash -c 'systemctl start ntp'
    xxx-vm: System has not been booted with systemd as init system (PID 1). Can't operate.
    xxx-vm: Failed to connect to bus: Host is down
==> xxx-vm: Trigger run failed

我以为这个触发器应该在虚拟机恢复后运行,但显然运行时虚拟机仍处于关闭状态。当虚拟机恢复并运行时,是否可以自动运行脚本?

答案1

我明白了:run在主机上运行脚本,要在客户机(虚拟机)上运行它,run_remote必须使用:

config.trigger.after :resume do |trigger|
  trigger.info = "Restart NTP"
  trigger.run_remote = {inline: "bash -c 'systemctl start ntp'"}
end

相关内容