1
votes

For test purposes I've configured a combination of vagrant/virtualbox/ansible.

Versions >> Ansible : 2.3.1.0 | Vagrant : 1.9.5.

While running :

vagrant provision

the output states :

  web1: Running provisioner: ansible...
  web1: Running ansible-playbook...
  PLAY [Install Apache] 
  **********************************************************
   skipping: no hosts matched
   PLAY RECAP 
  ****************************************************************

Below the configuration files :

Vagrantfile:

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.define "web1"
  config.vm.network "private_network", type: "dhcp"
  config.vm.network "forwarded_port", guest: 8040, host: 8090
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "256"
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provisioning/playbook.yml"
  end
  end
  end

Playbook :

  ---
  - name: Install Apache
    hosts: testclients
    become_user: root
    roles:
      - apache

Andible inventory (/etc/ansible/hosts) :

 [testclients]
 testclient3

and successful checking

  ansible -m ping testclients
  testclient3 | SUCCESS => {
  "changed": false,
  "ping": "pong"
    }

The weird thing is that if i will run the playbook itself (ansible-playbook playbook.yml) it works, in contrary inside vagrant which doesn't and return "no hosts matched". Any help would be appreciated.

1
you have your own inventory file but you do not instruct vagrant to use it, add the inventory_path properties in the ansible block with the path of the file to be usedFrederic Henri
ok ,thanks for your comment.I added those 2 lines in Vagrantfile : ansible.inventory_path = "provisioning/hosts" ansible.limit = "all" . While trying to re-run playbook ,an error occurs : TASK [Gathering Facts] ********************************************************* fatal: [testclient3]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Warning: Permanently added 'testclient3,192.168.111.111' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", "unreachable": true}VorX

1 Answers

3
votes

You have mismatch as you do not declare the inventory file to be used by Vagrant so it returns no host matched for testclients.

You have following options to fix your issue

  1. naive option: just use all hosts so everything will be ok

In your Playbook definition, change the host definition

  ---
  - name: Install Apache
    hosts: all
    become_user: root

Even if its not what you want at the end, give it a try just to make sure everything is working fine.

  1. Instruct vagrant to use your inventory file

In your Vagrantfile

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provisioning/playbook.yml"
    ansible.inventory_path = "path_to_your_file"
  end

You need to make sure you inform how to reach the instance so you need your inventory to have

testclient3 ansible_ssh_host=127.0.0.1 ansible_ssh_port=2200 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='path to ssh key'
  1. do not include your own inventory file and let vagrant generate one for you.

You will need vagrant to set the groups

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "provisioning/playbook.yml"
    ansible.groups = {
      "testclients" => ["testclient3"]
    }
  end

vagrant will generate the inventory file