Stop Vagrant from provisioning system when autostart = false?

Hi. I am using multimachine, where the client has autostart: false. Vagrant will try to provision the system that is not up. Is there a way to detect if the system is up, and not try to provision that is not up?

Snippet:

Vagrant.configure("2") do |config|
  @hosts.each do |hostname, ipaddr|
    primary = hostname == @primary
    autostart = @starts.include? hostname
    config.vm.define hostname, autostart: autostart, primary: primary do |node|
      node.vm.provision "shell" do |shell|
        shell.path = "./vagrant/provision.sh"
        shell.privileged = true
      end
  end
end

Hmmm, I wonder what version of Vagrant you are using. With 2.2.10 this seems to be working as you expect it to. That is, the provisioner is not run against a machine with autostart: false.

My Vagrantfile looks like:

Vagrant.configure("2") do |config|
  config.vm.define "test1", autostart: false, primary: true do |node|
    node.vm.box = "ubuntu/xenial64"
    node.vm.provision "shell" do |shell|
      shell.path = "./provision.sh"
      shell.privileged = true
    end
  end

  config.vm.define "test2", autostart: true, primary: true do |node|
    node.vm.box = "ubuntu/xenial64"
    node.vm.provision "shell" do |shell|
      shell.path = "./provision.sh"
      shell.privileged = true
    end
  end
end

I observe that the provisioner is only run against test2 while test1 does not start. My output looks like:

% vagrant up    
==> vagrant: You have enabled the experimental flag with all features enabled.
==> vagrant: Please use with caution, as some of the features may not be fully
==> vagrant: functional yet.
Bringing machine 'test2' up with 'virtualbox' provider...
==> test2: Importing base box 'ubuntu/xenial64'...
==> test2: Matching MAC address for NAT networking...
==> test2: Checking if box 'ubuntu/xenial64' version '20201006.2.0' is up to date...
==> test2: A newer version of the box 'ubuntu/xenial64' for provider 'virtualbox' is
==> test2: available! You currently have version '20201006.2.0'. The latest is version
==> test2: '20201008.0.0'. Run `vagrant box update` to update.
==> test2: Setting the name of the VM: vagrant_test2_1602171990964_86376
==> test2: Fixed port collision for 22 => 2222. Now on port 2207.
==> test2: Clearing any previously set network interfaces...
==> test2: Preparing network interfaces based on configuration...
    test2: Adapter 1: nat
==> test2: Forwarding ports...
    test2: 22 (guest) => 2207 (host) (adapter 1)
==> test2: Running 'pre-boot' VM customizations...
==> test2: Booting VM...
==> test2: Waiting for machine to boot. This may take a few minutes...
    test2: SSH address: 127.0.0.1:2207
    test2: SSH username: vagrant
    test2: SSH auth method: private key
    test2: 
    test2: Vagrant insecure key detected. Vagrant will automatically replace
    test2: this with a newly generated keypair for better security.
    test2: 
    test2: Inserting generated public key within guest...
    test2: Removing insecure key from the guest if it's present...
    test2: Key inserted! Disconnecting and reconnecting using new SSH key...
==> test2: Machine booted and ready!
==> test2: Checking for guest additions in VM...
    test2: The guest additions on this VM do not match the installed version of
    test2: VirtualBox! In most cases this is fine, but in rare cases it can
    test2: prevent things such as shared folders from working properly. If you see
    test2: shared folder errors, please make sure the guest additions within the
    test2: virtual machine match the version of VirtualBox you have installed on
    test2: your host and reload your VM.
    test2: 
    test2: Guest Additions Version: 5.1.38
    test2: VirtualBox Version: 6.1
==> test2: Mounting shared folders...
    test2: /vagrant => /Users/sophia/project/vagrant
==> test2: Running provisioner: shell...
    test2: Running: /var/folders/8w/p5c4ycl53fl9j19gp2y31k300000gp/T/vagrant-shell20201008-69440-1a5zi7h.sh
    test2: hello

I am running Vagrant 2.2.3. I had to install through system packages because the only way to get libvirt to work was using debian/ubuntu apt-get packages.

It does indeed try to provision a system that is not started unfortunately when I type vagrant provision, but it doesn’t try to do other operations. That is why I wanted to see if there was some Util library or something that can detect running systems, sort of reflection on its current state.

hmmm, you might be able to use the “never” provision option

    node.vm.provision "autostart", type: "shell", run: "never" do |shell|
      shell.path = "./provision.sh"
      shell.privileged = true
    end

Then, the provisioner will only run when explicitly run with vagrant provision --provision-with autostart.

I couldn’t find any utils available in Vagrant 2.2.3 that would detect if a system is running though.

1 Like