Ansible skipping localhost when using Vagrant

Vagrant Ansible provisioner skips plays that are meant to be run on localhost. There’s a simple workaround for that.

www.vagrantup.com

Heads up: this post is more than a year old, so some details may be out of date.

I have a playbook which will be run on the Ansible host.

---
- name: My play
  hosts: localhost
  connection: local
  roles:
   - list of roles

Running the play from the command line using ansible-playbook I got the result that I expected. But when I use Vagrant to provision a virtual machine using Ansible provisioner, the play that should have been executed on the Ansible host was skipped altogether.

PLAY [My play] ****
skipping: no hosts matched

From what I understand, this is caused by Vagrant Ansible provisioner always setting the --limit argument. I can reproduce the same issue by executing the playbook using ansible-playbook and setting the --limit argument explicitly.

$ ansible-playbook -i inventory/dev/hosts play.yml --limit all

Workaround

In your Vagrantfile add the following line to your Ansible provisioner configuration

ansible.limit = 'all,localhost'