How to continue Vagrant/Ansible provision script from error?
Asked Answered
N

4

9

After I provision my Vagrant... I may get errors during provision... how do I restart from error, instead of doing everything from scratch ?

vagrant destroy -f && vagrant up

And I may get an error...

PLAY RECAP ******************************************************************** 
to retry, use: --limit @/path/to/playbook.retry

And I want to just resume from where it failed... it seems it can be done by the message... use --limit.... but when I use it in the vagrant context it doesn't work..

Northeastward answered 28/4, 2015 at 20:50 Comment(3)
Why do you care about this? Ansible plays should be idempotent, so running the whole playbook again should be very fast.Louislouisa
@StrahinjaKustudic "idempotent" does not mean "instant". You can save a lot of time rerunning only failing tasks.Flooring
Fair enough. Ansible doesn't have the feature currently, but I'm sure they would love a pull request with it :)Louislouisa
Y
7

You can edit the Vagrantfile and include the ansible.start_at_task variable.

Then you can re-run the provision with $ vagrant reload --provision

Vagrant Reload docs

However, because Ansible plays are idempotent you don't really need to do the start_at_task. You can just re-run the provision with the reload command above.

Yerga answered 28/4, 2015 at 21:2 Comment(0)
N
1

You could do an ansible run with ansible-playbook on your vagrant box. The trick is to use the inventory file ansible has created. It is located in your .vagrant foler.

ansible-playbook playbook.yml -i vagrant/.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory --start-at-task='MY_TASK'

Vagrantfile: Assign your roles to a vagrant machines.

      config.vm.provision "ansible" do |ansible|
        ansible.verbose = "v"
        ansible.playbook = "../playbook.yml"
        ansible.groups = {
        "web"                 => ['vm01app'],
        "db"                  => ['vm01db'],
        }
      end
Nepean answered 9/8, 2017 at 10:51 Comment(0)
K
0

To start provisioning at a particular task:

vagrant ssh
provision --list-tasks
provision --start-at-task {task}

If there's anything that needs to be fixed, you can either do it while SSHed to the server, or you can make the changes in the host machine, then sync those changes to the Vagrant host.

https://developer.hashicorp.com/vagrant/docs/synced-folders

So, for instance, if you're using rsync as the sync type, you can sync the changes by running

vagrant rsync
Kovach answered 27/10, 2022 at 4:0 Comment(0)
L
-1

If you used shell as provisioner instead of Ansible (for this you would need to have Ansible installed in the VM itself) you could run a command like this:

test -f "/path/to/playbook.retry" && (ansible-playbook site.yml --limit @/path/to/playbook.retry; rm -f "/path/to/playbook.retry") || ansible-playbook site.yml

This would basically check if a retry file exists, if it does it would run the playbook with that limit and delete the retry file after, if not it would run the whole playbook. Of course you would need to run these commands with sudo if you are using Vagrant, but that is easy to add.

Louislouisa answered 28/4, 2015 at 21:24 Comment(2)
The shell provisioner runs commands inside the vagrant VM, whereas the Ansible provision runs commands outside the vagrant VM. The Ansible retry files are stored outside the VM, as well, and so are not accessible to the shell provisioner inside the guest machine.Flooring
You can make them visible if you put them in the vagrant shared directory. That is how we use Ansible with Vagrant because that way you can spin up a vagrant box from any guest OS (like Windows) which doesn't even have Ansible installed, but that is a whole other topic.Louislouisa

© 2022 - 2024 — McMap. All rights reserved.