Use Vagrant vm as Ansible control machine

1. Create Vagrantfile as below:

Vagrant.configure(“2”) do |config|

config.ssh.insert_key = “false”

config.vm.define “ansible” do |ansible| = “centos7”
ansible.vm.provision “shell”, path: “”

config.vm.define “vagrant1” do |vagrant1| = “ubuntu/trusty64” “private_network”, ip: “”


2. The content of

#!/usr/bin/env bash

echo “Add extra packages for pip”
sudo rpm -iUvh
echo “update packages”
sudo yum -y update
echo “Install libraries”
sudo yum -y install python-pip python-devel python-lxml openssl-devel
echo “Update pip”
sudo pip install –upgrade pip
echo “Install ansible”
sudo pip install ansible
3. The ansible machine will be used to control the vagrant1 machine

4. After vagrant up, login into ansible machine by: vagrant ssh ansible
create private/public key and upload the public key to vagrant1 (make sure you can ping it first)

5. Add an entry to your /etc/hosts file: (optional)
[target server id] [hostname]
ex: testserv

6. create an Ansible inventory file hosts with the below content:
localhost ansible_connection=local (this line is added so that Ansible can provision local machine)
testserver ansible_host=testsrv ansible_user=vagrant

(Assume ‘testsrv’ is the hostname you add in your /etc/hosts file, if you didn’t add a hostname for the target server, then use its ip instead)

7. Run the following command to make sure Ansible can talk to both local and remote machine
ansible all -i hosts -m ping