Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate ansible provisioning playbooks in vagrant

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.

like image 525
mhkeller Avatar asked Aug 27 '15 05:08

mhkeller


People also ask

How do I run a vagrant provision?

On the first vagrant up that creates the environment, provisioning is run. If the environment was already created and the up is just resuming a machine or booting it up, they will not run unless the --provision flag is explicitly provided. When vagrant provision is used on a running environment.

How do I use vagrant with Ansible?

There are a lot of Ansible options you can configure in your Vagrantfile . Visit the Ansible Provisioner documentation for more information. This will start the VM, and run the provisioning playbook (on the first VM startup). This will re-run the playbook against the existing VM.

Is vagrant like Ansible?

Ansible, Terraform and Vagrant each perform automation in some way, but their functionality is decidedly different. Vagrant can incorporate other automation tools, like Ansible, Puppet or Chef, to perform specific VM configuration tasks.

What is Ansible Provisioner?

The Ansible/F5 provisioner is an opensource provisioning tool. It is a collection of Ansible playbooks packaged to build and tear down F5 and application infrastructure. It can also be used to scale the infrastructure as needed by the user.


1 Answers

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.

like image 156
mhkeller Avatar answered Nov 15 '22 11:11

mhkeller