When writing Ansible playbooks, it’s useful to test them locally against your Vagrant box. The easiest way is just running “vagrant provision” after each change and then validating the results on the vagrant image. That said, this runs the entire playbook in full and while I enjoy a good cup of tea, there’s only so much I can have per day.
Often, we just want to check if the last couple changes run correctly. While you can’t do this with “vagrant provision”, you can do this with the ansible-playbook command’s –start-at-task option. Unfortunately, ansible-playbook often doesn’t want to connect directly to your vagrant box with just the IP. Luckily, the answer is in the Ansible/Vagrant docs (admittedly at the bottom of the page).
So, if we put these two things together, we have this command which’ll start running our playbook at whatever step we choose, directly against our vagrant box. All you need to do is swap out the name of the step you want to start at the very end of the command:
ansible-playbook -i inventories/dev --private-key=~/.vagrant.d/insecure_private_key \ -uvagrant my_playbook.yml --start-at-task "Do Foobar"
You can then let it run and stop it with a quick Ctrl+C. Or, if you want to check each step by hand, you can add the –step switch. Ansible will then ask you to confirm or cancel before it goes on to each of the following steps in the playbook.
If you’re looking to squeeze out that extra bit of speed, you might try disabling fact gathering but I haven’t tried it and wouldn’t recommend it.
Remember though, just because your last changes work in isolation doesn’t mean your entire playbook can’t break. Always test your playbooks in full after any major changes.