Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/uguy/vagrant-ansible-build-ovf

This is a simple sample on how to get started generating OVF file from a Vagrant box tuned with Ansible
https://github.com/uguy/vagrant-ansible-build-ovf

Last synced: about 1 month ago
JSON representation

This is a simple sample on how to get started generating OVF file from a Vagrant box tuned with Ansible

Awesome Lists containing this project

README

        

= vagrant-ansible-build-ovf
Hugues BRETIN
:experimental:
:toc:
:toc-placement!:

This is a simple sample on how to get started generating OVF file from a https://www.vagrantup.com/[Vagrant] box tuned with http://www.ansible.com/home[Ansible].

NOTE: you need a linux box (or an Ansible/bash capable one) to run it

toc::[]

== Forge your own cloud images

When you cant go for https://www.docker.com/[Docker] based deployment, you might want to forge deployable images in order to push them to your favourite cloud provider's catalog (ie. using glance for https://www.openstack.org/[OpenStack] ones). When you need elasticity, deploying a prepackage vm is surely faster than reconfiguring a new one from scratch !

Forging a deployable image means creating an environment from a base distro and customize it to suit your needs (install custom services/config/...)

*Vagrant* is a great and well known tool for managing local virtual machines with virtual box (but not only), so it is seems to be a natural choice to setup the base distro.

Now *Ansible* is also an "extremly pleasant" tool for box configuation (in fact your all infrastructure if you wish).
Since some cloud provider impose some packages to be present to accept custom images as valid ones, we can easily use ansible for that !

This sample shows how to build an OpenStack compliant custom http://en.wikipedia.org/wiki/Open_Virtualization_Format[OVF] files from Ubuntu/trusty64 with custom packages on it (cowsay,fortune) and configuration

== Setup environment

Install Vagrant, Ansible

[source,bash,subs="verbatim,attributes"]
----
# Vagrant
$ sudo apt-get install -y vagrant virtualbox
# Ansible
$ sudo apt-get install -y python-dev python-pip
$ sudo pip install ansible
----

== Build the image

[source,bash,subs="verbatim,attributes"]
----
$ ./build-image.sh
----

The produced OVF file should be in the relative dist folder :)

You can use your cloud provider client to upload it and make it available for instanciation. +

_But before this, make sure your box has been *secured* via ansible (users,keys,packages,...) Vagrant user should be removed and probably a lot of other things needs to be done, just improve the playbook to do so ;)_

[source,bash,subs="verbatim,attributes"]
----
$ glance image-create --progress --name "funny-ubuntu-trusty-1.0.0" --is-public=false --container-format=ovf --disk-format=vmdk --file dist/funny-ubuntu-trusty-1.0.0.ovf
----

== How this works

The build script destroy the vagrant box if it exist and recreate it.

.source: build-image.sh
[source,bash,subs="verbatim,attributes"]
----
vagrant destroy -f && vagrant up
----

The box defined is an official Ubuntu Trusty64 fetched by Vagrant from https://vagrantcloud.com/ubuntu/boxes/trusty64[Vagrantcloud]
The name of the virtual machine is fixed to be able to retrieve it from the script

.source: Vagrantfile
[source,ruby,subs="verbatim,attributes"]
----
config.vm.box = "ubuntu/trusty64"

config.vm.provider "virtualbox" do |vb|
vb.gui = false
# name is important since used to target the vm from VBoxManage tool
vb.name = "funny-ubuntu-trusty-1.0.0"
end
----

We specify ansible as our provider for the vm customization. Vagrant will run ansible/playbook.yml against the vm once started. +
Ansible rocks and is really worth having a look !

.source: Vagrantfile
[source,ruby,subs="verbatim,attributes"]
----
config.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/playbook.yml"
ansible.verbose = "v"
end
----

The last consist in exporting the vm as an OVF file. First it needs to be stopped ( vagrant halt) and then a simple call to VBoxManage will do.

.source: build-image.sh
[source,bash,subs="verbatim,attributes"]
----
$ VBoxManage export $VM_NAME -o $SCRIPTPATH/dist/$VM_NAME.ovf --ovf20 --manifest
----

That's all folks !