Pages

Monday, February 1, 2016

Ansible with Vagrant


Provisioning is one important aspect of DevOps. Provisioning is something like providing. In Infrastructure, a provisioning is some thing like preparing the Server or the Physical machine with all necessary configuration and installations before passing it to the Users.

In this article we will how we can use Ansible in provisioning a Vagrant Box. You can check my other blogs for more details on Vagrant.

Provisioning a Vagrant box with Ansible goes in 2 steps,

1) Configure a Vagrant file for configuring the vagrant machine. For the article purpose we will use ubuntu box.

Below is the Vagrantfile that we use for configuring the Vagrant Box,

[root@vx111a vagrant-ansible]# cat Vagrantfile
Vagrant.configure(2) do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.host_name = "ubuntu.foohost.vm"
  config.vm.network "private_network", ip: "172.16.202.97"
  config.vm.network "forwarded_port",guest: 80, host: 18080

 config.vm.provider :virtualbox do |vb|
     vb.name = "ubuntuFoohost"
 end

 config.vm.provision :ansible do |ansible|
   ansible.playbook = "playbook.yml"
 end

end

In the above Snippet, we have used Virtaul box as “ubuntu”, host name for the configured box as “ubuntu.foohost.vm”, IP address as "172.16.202.97" and Forwarded port for 80 to 18080.

The main Important thing is the provisioning part as,

config.vm.provision :ansible do |ansible|
   ansible.playbook = "playbook.yml"
 end

The line is the provisioning Ansible playbook location and telling the Vagrant to use Ansible as the provision tool and the the playbook to execute on the box once configured.

2) Create the playbook.

Once the VagrantFile is created,we then need to create a playbook. Below is the playbook for the article purpose.

[root@vx111a vagrant-ansible]# cat playbook.yml
---
- hosts: all
  sudo: true
  tasks:
    - name: install apache
      apt: name=apache2 state=present
    - name: install mysql
      apt: name=mysql-server state=present
    - name: install php
      apt: name=php5 state=present
    - copy: src="/work/vagrant-ansible/info.php" dest="/var/www/html/"

In the above snippet, we are trying to install Apache, My Sql and then PHP. Once the play book is ran we also copy a PHP index file to the /var/www/html location and test the configured Vagrant box.

We have a test PHP file as,
[root@vx111a vagrant-ansible]# cat info.php
<?php phpinfo();

Keep all the 3 files in the same location and run the “vagrant up” command. We can see the following output as

[root@vx111a vagrant-ansible]# vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'ubuntu/trusty64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'ubuntu/trusty64'
    default: URL: https://atlas.hashicorp.com/ubuntu/trusty64
==> default: Adding box 'ubuntu/trusty64' (v20160122.0.0) for provider: virtualbox
    default: Downloading: https://atlas.hashicorp.com/ubuntu/boxes/trusty64/versions/20160122.0.0/providers/virtualbox.box
==> default: Successfully added box 'ubuntu/trusty64' (v20160122.0.0) for 'virtualbox'!
==> default: Importing base box 'ubuntu/trusty64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'ubuntu/trusty64' is up to date...
==> default: Setting the name of the VM: ubuntuFoohost
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
    default: Adapter 2: hostonly
==> default: Forwarding ports...
    default: 80 (guest) => 18080 (host) (adapter 1)
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default:
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default:
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default:
    default: Guest Additions Version: 4.3.34
    default: VirtualBox Version: 5.0
==> default: Setting hostname...
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
    default: /vagrant => /work/vagrant-ansible
==> default: Running provisioner: ansible...
    default: Running ansible-playbook...

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [default]

TASK: [install apache] ********************************************************
changed: [default]

TASK: [install mysql] *********************************************************
changed: [default]

TASK: [install php] ***********************************************************
changed: [default]

TASK: [copy src="/work/vagrant-ansible/info.php" dest="/var/www/html/"] *******
changed: [default]

PLAY RECAP ********************************************************************
default                    : ok=5    changed=4    unreachable=0    failed=0  

Once this is successful,we can test the PHP installation by accessing http://localhost:18080/info.php

Thus we have seen how we can use Ansible in provisioning the Vagrant Box. More to Come in the next articles.

1 comment :

  1. Bonjour, hello..;

    can I ask you some questions ?
    puis-je vous possez quelque questions que je ne comprend pas.

    ReplyDelete