Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

If you wish to run Ansible provisioning with Vagrant and pass arguments to Ansible, you can use example below.


Vagrantfile


# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
...
config.vm.provision "ansible" do |ansible|
...
ansible.playbook = "provisioning/site.yml"
ansible.raw_arguments = Shellwords.shellsplit(ENV["ANSIBLE_ARGS"]) if ENV["ANSIBLE_ARGS"]
...
end
...
end

Examples


# If running first time, runs all tasks within provisioning otherwise just starts the VM and ignores all tasks
$ vagrant up

# Runs all tasks within provisioning without restarting the VM
$ vagrant provision

# Does not run provisioning but restarts the VM
$ vagrant reload

# Runs all tasks within provisioning with restarting the VM
$ vagrant reload --provision

# If running first time, runs tasks for given tags within provisioning otherwise just starts the VM and ignores all tasks.
$ ANSIBLE_ARGS='--tags "tag_1,tag_2,..."' vagrant up

# Runs tasks for given tags within provisioning without restarting the VM
$ ANSIBLE_ARGS='--tags "tag_1,tag_2,..."' vagrant provision

# Runs tasks for given tags within provisioning with restarting the VM
$ ANSIBLE_ARGS='--tags "tag_1,tag_2,..."' vagrant reload --provision

# You can also use other tags like below
$ ANSIBLE_ARGS='--skip-tags "tag_1,tag_2,..."' vagrant ...

If a task execution fails for any given reason you can use --limit flag and it should retry execution of all tasks against failed hosts. The exact command will be printed right under error message. e.g. --limit @/Users/inanzzz/test_app/provisioning/site.retry