6
votes

I'm using vagrant and ansible to provision a virtual machine and that works fine. The ansible playbook clones a git repo, installs it and runs a service daemon.

I'd also like to have a vagrant command that executes a separate "update" playbook that pulls the latest from the git repo, installs and restarts the daemon.

Something like this usage would be nice.

Vagrant.configure("2") do |config|

  # Default
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
  end

  # Update
  config.vm.provision "ansible", name="update" do |ansible|
    ansible.playbook = "update.yml"
  end
end

Then I could run it with vagrant --provision-with update. Is something like this possible? I'd like to avoid having to ssh into the box to run an update like this.

1

1 Answers

14
votes

I'm not sure why the person's answer was deleted because it was correct. As of vagrant 1.7.0 you can name provisions.

The following worked:

Vagrant.configure("2") do |config|

  # Default
  config.vm.provision "main", type: "ansible" do |ansible|
    ansible.playbook = "playbook.yml"
  end

  # Update
  config.vm.provision "update", type: "ansible" do |ansible|
    ansible.playbook = "update.yml"
  end
end

You can use vagrant provision --provision-with <foo> to then execute either one.

However, if you do vagrant up, all provisioners will run, which is not desired. As a solution, I run vagrant up --no-provision then vagrant provision --provision-with main as the default, which I put in a Makefile.