How to test ansible playbooks with vagrant

Playbook directory hosts file

[server]
server ansible_ssh_port=22 ansible_ssh_host=192.168.0.1 ansible_ssh_user=vagrant

[agent]
agent1 ansible_ssh_port=22 ansible_ssh_host=192.168.0.2 ansible_ssh_user=vagrant
agent2 ansible_ssh_port=22 ansible_ssh_host=192.168.0.3 ansible_ssh_user=vagrant

Ansible config file

/etc/ansible/ansible.cfg

[defaults]
hostfile = hosts
remote_user = vagrant
host_key_checking = False
private_key_file = ~/.vagrant.d/insecure_private_key

Vagrantfile

Vagrant.configure("2") do |config|
...
# Use the same key for each machine
config.ssh.insert_key = false
...
end

Run playbook

$ ansible-playbook -i hosts playbookname.yml

Old Method

/etc/ansible/ansible.cfg

Maybe unnecessary.

[defaults]
host_key_checking = False

[ssh_connection]
ssh_args = -o UserKnownHostsFile=/dev/null

/etc/ansible/hosts

This file anything is okay.

[all]
[all:vars]

cd Vagrantfile exist path

vim .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory

# Generated by Vagrant
default ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='.vagrant/machines/default/virtualbox/private_key'

when multiple hosts

master ansible_ssh_host=127.0.0.1 ansible_ssh_port=1234 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='.vagrant/machines/master/virtualbox/private_key'
node1 ansible_ssh_host=127.0.0.1 ansible_ssh_port=1235 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='.vagrant/machines/node1/virtualbox/private_key'
node2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=1236 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='.vagrant/machines/node2/virtualbox/private_key'

Or

[all:children]
k1
k2
k3

[k1]
master nodetype=master ansible_ssh_host=127.0.0.1 ansible_ssh_port=1234 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='.vagrant/machines/master/virtualbox/private_key'

[k2]
node1 nodetype=node ansible_ssh_host=127.0.0.1 ansible_ssh_port=1235 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='.vagrant/machines/node1/virtualbox/private_key'

[k3]
node2 nodetype=node ansible_ssh_host=127.0.0.1 ansible_ssh_port=1236 ansible_ssh_user='vagrant' ansible_ssh_private_key_file='.vagrant/machines/node2/virtualbox/private_key'

Vagrantfile

Vagrant.configure("2") do |config| 
 config.vm.box = "bento/centos-7.3" 
 
 config.vm.provider :virtualbox do |v| 
 v.memory = 1024 
 v.cpus = 2 
 end 
 
 # Master Server 
 config.vm.define "master", primary: true do |master| 
 master.vm.hostname = 'master' 
 master.vm.network :private_network, ip: "192.168.33.200" 
 master.vm.network :forwarded_port, guest: 22, host: 1234, id: 'ssh' 
 end 
 
 # Node1 Server 
 config.vm.define "node1", autostart: false do |node1| 
 node1.vm.hostname = 'node1' 
 node1.vm.network :private_network, ip: "192.168.33.201" 
 node1.vm.network :forwarded_port, guest: 22, host: 1235, id: 'ssh' 
 end 
 
 # Node2 Server 
 config.vm.define "node2", autostart: false do |node2| 
 node2.vm.hostname = 'node2' 
 node2.vm.network :private_network, ip: "192.168.33.202" 
 node2.vm.network :forwarded_port, guest: 22, host: 1236, id: 'ssh' 
 end 
end

run ad-hoc and ansible-playbook command

$ ansible all -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory -m ping
Special host:
$ ansible default[master|node1|node2] -i .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory -a "free -m"
$ ansible-playbook playbookname.yml -i .vagrant/provisioners/ansible/inventory/vagrant_a
nsible_inventory

Resources

https://github.com/geerlingguy/ansible-role-test-vms/blob/master/Vagrantfile