如何在成功配置后立即停止多机 Vagrant 设置中的机器?

How to halt a machine in multi-machine Vagrant setup immediately upon successful provisioning?

有一个多机 Vagrant 设置(此处截断为两台机器)如下所示:

Vagrant.configure(2) do |config|

  config.vm.define "xfcevm" do |xfcevm|
    xfcevm.vm.box = "generic/ubuntu1904"
    xfcevm.vm.hostname = "xfcevm"
  end

  config.vm.define "gnomevm" do |gnomevm|
    gnomevm.vm.box = "generic/fedora30"
    gnomevm.vm.hostname = "gnomevm"
  end

  config.vm.provider "virtualbox" do |vb|
    vb.gui = true
    vb.memory = "2048"
    vb.cpus = 1
    vb.customize ["modifyvm", :id, "--vram", "32"]
  end

  config.vm.provision "ansible" do |ansible|
    ansible.verbose = "v"
    ansible.compatibility_mode = "2.0"
    ansible.playbook = "setup.yml"
  end

  config.vm.provision "ansible", run: 'always' do |ansible|
    ansible.verbose = "v"
    ansible.compatibility_mode = "2.0"
    ansible.playbook = "tests.yml"
  end

  # halt here

end

如果测试剧本没有错误地通过,那么我希望在 tests.yml 剧本完成后立即停止该机器。我如何从 Vagrantfile 或通过创建另一个 Ansible 任务来做到这一点?

您可以在测试剧本末尾发出 shutdown 命令。仅当其余任务成功时才会播放。

- name: shutdown machine
  become: true
  command: shutdown -h now

请参阅 shutdown --help 以根据您的特定需要调整命令(例如,使用 halt 而不是 poweroff