For lamp site Vagrant boxes, can I speed up a reprovision if provision fails and I made a change?

  • Page Owner: Not Set
  • Last Reviewed: 2018-08-30

First time vagrant box provisions can take a while. If my provision is almost done and I need to re-run it, do I have to run the whole thing?


Answer

For a lot of Vagrant boxes, provision just runs 'ansible-playbook' with a few parameters after booting up the vm, and you may be able to run it manually if you had to make a small change to the ansible config, and speed things up in the process.

One helpful parameter is --start-at, which restarts the provision from a task by name. If you changed a task in your playbook, try choosing the task right before it, then running:

ansible-playbook -i path/to/inventory/file yourplaybook --start-at="name of last successful task"

on a lot of Vagrant boxes, this will look like this, say to try downloading solr again:

cd devenv/ansible && ansible-playbook -i inventories/dev/hosts main.yml --start-at="solr5-core: Download Solr"

I've even added bash_aliases to make these runs easier to type:

alias andi='if [[ -d devenv ]]; then cd devenv/ansible; fi ; ansible-playbook -i inventories/dev/hosts '
alias andis='if [[ -d devenv ]]; then cd devenv/ansible; fi ; ansible-playbook -i inventories/dev/hosts --start-at '

this makes it easy to run ANsible-playbook on the Dev-Inventory Starting at task x.

andis "solr5-core: Download Solr" main.yml

Note that ansible tasks also take tags as arbitrary options. So say you just added a task to install nginx to your playbook and don't need to run everything:

tasks:
  - command: "huge command you don't want to run again"
  - name: apt command you just added
    apt: name=nginx state=latest
    tags: [ apt-install ]
  - name: another task you don't want to run again
    service: name=apache2 state=started
    tags: [ apache2 ]

You can just run ansible-playbook main.yml -t apt-install to only run your tagged task. --start-at and -t can also be combined.