Dubby
Dubby

Reputation: 2402

"skipping: no hosts matched" issue with Vagrant and Ansible

I have installed Vagrant, VirtualBox and Ansible and trying to run provision over one host but it always returns "skipping: no hosts matched"

The head of my playbook file looks like this:

---
- hosts: webservers
  user: vagrant
  sudo: yes

and my /etc/ansible/hosts file looks like this:

[webservers]
webserver1

I tried putting the IP address there but had the same result. I have added my ssh key to the server and added webserver1 host to both .ssh/config and /etc/hosts.

I can ssh vagrant@webserver1 fine without being prompted for a password, thanks to using the ssh key.

What am I missing here?

Upvotes: 30

Views: 34416

Answers (9)

Guillaume Dedrie
Guillaume Dedrie

Reputation: 144

According to the vagrant documentation (http://docs.vagrantup.com/v2/provisioning/ansible.html), when you use Ansible to provision, vagrant will automatically create an host file named vagrant_ansible_inventory_default which will use vagrant vm.config information (like your VM IP, remote ssh port).

So just add your playbook path using the ansible.playbook = "playbook.yml"

Upvotes: 2

mestachs
mestachs

Reputation: 1889

It could help to post your Vagrantfile and your ansible inventory file.

  • Are you using the default ansible provider of vagrant?

    did you specify the inventory_path?

 config.vm.provision :ansible do |ansible|
   ansible.playbook = "provisioning/playbook.yml"
   ansible.inventory_path = "provisioning/ansible_hosts"
 end
  • Are you launching it the through the a vagrant ssh with --connection=local try a /etc/ansible/hosts
[webserver1]
127.0.0.1              ansible_connection=local
  • Are you using the ansibleLocal provider / vagrant plugin?

Upvotes: 30

Geetha
Geetha

Reputation: 19

I tried pointing the groups to default vagrant box and it worked. Refer https://gagor.pl/2015/12/ansible-on-vagrant-skipping-no-hosts-matched/

ansible.groups = {
  'webservers' => ['default']
}

Upvotes: 2

Vaseem007
Vaseem007

Reputation: 2531

You can run your playbook in verbose mode by adding -vvvv in the end. Ansible uses host file from /etc/ansible/hosts add the below

You can check what inventory file is being used by your ansible by below

ansible --version
ansible 2.0.0.2
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides

Then check your host file if it contains the exact group for host

 vim /etc/ansible/hosts
 [webserver1]
 IP of machine

check by running the below command

ansible -m ping 10.0.3.145 (IP Of machine)
10.0.3.145 | SUCCESS => {
"changed": false, 
"ping": "pong"

}

This should fix the issue.

Upvotes: 1

isheng
isheng

Reputation: 1

make sure you use this command "ansible-playbook -i yourhosts your.yml"

Upvotes: -5

Theo Kouzelis
Theo Kouzelis

Reputation: 3523

I think you can also do this without a hosts file, by assigning the Ansible groups in your Vagrant file.

If you don't have multiple machines in your Vagrant file your box will probably be called "default" and you will be able to add multiple Ansible groups with the following code.

Code:

config.vm.provision "ansible" do |ansible|
    ansible.groups = {
        "webservers" => ["default"],
        "dev_enviroment" => ["default"]
    }

    ansible.playbook = "provisioning/playbook.yml"
end

Upvotes: 21

Ricardo Mayerhofer
Ricardo Mayerhofer

Reputation: 2309

Make sure you're running ansible-playbook command instead of pure ansible command.

Upvotes: 27

Theo Kouzelis
Theo Kouzelis

Reputation: 3523

Changing hosts to "all" worked for me.

---
- hosts: all
  user: vagrant
  sudo: yes

Upvotes: 9

Lorin Hochstein
Lorin Hochstein

Reputation: 59202

The first thing that jumps out at me is that the syntax on the head of your playbook file is incorrect, (it has extra dashes where it shouldn't). It should look like this instead:

---
- hosts: webservers
  user: vagrant
  sudo: yes

Upvotes: 3

Related Questions