ipaleka
ipaleka

Reputation: 3957

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

There's a multi-machine Vagrant setup (truncated here to two machines) like the following:

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

If the tests playbook passes without errors then I want that machine to be halted just after the tests.yml playbook is finished. How I can do that from Vagrantfile or by creating another Ansible task?

Upvotes: 1

Views: 105

Answers (1)

Zeitounator
Zeitounator

Reputation: 44760

You can issue a shutdown command at the end of your test playbook. It will only be played if the rest of the tasks were successful.

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

See shutdown --help to adapt the command to your specific need (e.g. using halt instead of poweroff)

Upvotes: 1

Related Questions