We normally use Ansible playbooks to configure servers in a cloud provider. When we develop those playbooks we need to test them locally. Thanks to Vagrant it is quite easy to start a virtual machine and run our playbooks against it.
There are two ways to achieve this:
- using Vagrant’s Ansible provisioning feature
- using the virtual machine like any other remote server
I'm going to explain the second option here.
First, let's create a virtual machine called, for example, tau
. The Vagrantfile
could be:
Vagrant.configure('2') do |config|
config.vm.define 'tau' do |debian|
debian.vm.box = 'debian/buster64'
debian.vm.network :private_network, ip: '192.168.27.2'
debian.vm.hostname = 'tau'
debian.vm.provider 'virtualbox' do |vb|
vb.memory = '2048'
vb.cpus = 2
end
end
end
We can start the server with:
vagrant up
Now we have to add it to Ansible's inventory, but we need to know the ssh key Vagrant is using when we connect using vagrant ssh
:
$ vagrant ssh-config
Host tau
HostName 127.0.0.1
User vagrant
Port 2200
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/javiervidal/test/.vagrant/machines/tau/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
Interesting, we can use /Users/javiervidal/test/.vagrant/machines/tau/virtualbox/private_key
in the inventory. We need to add a line like this:
tau ansible_host=192.168.27.2 ansible_port=22 ansible_ssh_user=vagrant ansible_ssh_private_key_file=/Users/javiervidal/test/.vagrant/machines/tau/virtualbox/private_key ansible_python_interpreter=/usr/bin/python3
And finally we can test that Ansible can connect to tau
:
$ ansible tau -m ping
tau | SUCCESS => {
"changed": false,
"ping": "pong"
}
😀
Top comments (0)