Reputation: 455
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.
Upvotes: 2
Views: 7223
Reputation: 53713
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
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.
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'
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
Upvotes: 4