monofone

coding.

Ansible with Composer for Vagrant LAMP

I like simplicity. Thats why I like Ansible, it`s much more simple to expand functionality then, for example in Chef. And I like to not have to use too much different tools so I can fokus on devleopment and not on the setup of environment. For a project with distributed developers Vagrant is a great solution to only have one guy figuring out how to setup the environment and let the rest just use the Vagrantbox. So this article is about all the components playing together.

In most cases I do webdevelopment with PHP and a de-facto standard is the usage of composer for dependency management. Also for the dev requirements composer is used to setup phpunit and for e.q. behat. Getting Ansible roles to provision the vagrantbox via composer, was not possible until this installerscript for composer wich will download ansible packages in the same way as composer packages.

To use one of the playbooks via composer just add them to your composer.json

This will install the ansible-mysql package directly to provisioning/roles/mysql. Now it`s possible to use the mysql and all the other roles in the playbook.yml which describes the porivisioning in vagrant.

The file vars/main.yml contains the configuration variables for the roles.

The variables define the way the roles are executed and which data the templates will be filled with. What variables are available can be found in the specific roles. As they are written in yml it´s easy to find out what can be configured and how.

Provision with Vagrant and Ansible

The Vagrantfile is straight forward written in the Version2 syntax.

Project dependent roles can be added to the provisioning directory, you only need to exclude these from the gitignore this looks like:

Make your own role available via composer

It`s as easy as adding a composer.json to your repository to make it available as ansible role. It must contain a name and the reference to the installer as well as a type, marking it as an ansible role.

This describes an ansible-role that will be installed in the provisioning/roles folder with the name mysql.

More installers ?

It would be nice to support more installer types like bower or npm, but at a first glance I didn`t find a way to get the ansible roles into a seperate directory, any ideas ?