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|
ansible.vm.box = “centos7”
ansible.vm.provision “shell”, path: “provision.sh”
end

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

end

2. The content of provision.sh

#!/usr/bin/env bash

echo “Add extra packages for pip”
sudo rpm -iUvh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
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: 192.168.33.11 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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s