This article explains how to easily deploy a web application on IaaS platforms using Ansible. We’ll see the big picture and then study the case of deploying a Symfony application.
What is Ansible?
Ansible is an automation framework written in Python. An Ansible script is basically a list of tasks written in YAML files. They are grouped in directories called “roles.” Each role has a purpose such as “install and configure MySQL.” The classic way of using Ansible is to run your Ansible’s script on your machine and Ansible will remotely execute each task through an SSH connection on the server you are targeting.
If you don’t know how Ansible works, you should have a look at this previous post.
Development and production environment
For every DevOps project, you need a development and a production environment that meets the following conditions:
- The development environment is as similar to the production environment as possible.
- The development and the production environment can be recreated automatically.
- The application code can also be deployed in an automated manner (continuous delivery/deployment).
The development environment
In order to create an ISO prod environment, we’ll use VirtualBox, Vagrant, and Ansible. VirtualBox is virtualization software that allows us to launch VMs on our machine. VirtualBox is a bit complicated so I suggest using Vagrant to start, which offers a simple interface to manage our VMs.
The first step is creating a VM on our development machine. To do this, describe the VM configuration in a Vagrantfile. Choose which OS you want to use, the IP of the VM and the path of the folder containing the code of the application you need to synchronize between the VM and our development machine. Here is an example of a Vagrantfile.yml:
$HOSTNAME = "myapp.dev" $BOX = "ubuntu/trusty64" $IP = "10.0.0.10" $MEMORY = ENV.has_key?('VM_MEMORY') ? ENV['VM_MEMORY'] : "2048" $CPUS = ENV.has_key?('VM_CPUS') ? ENV['VM_CPUS'] : "2" $EXEC_CAP = ENV.has_key?('VM_EXEC_CAP') ? ENV['VM_EXEC_CAP'] : "100" Vagrant.configure("2") do |config| config.vm.hostname = $HOSTNAME config.vm.box = $BOX config.vm.network :private_network, ip: $IP config.ssh.forward_agent = true config.vm.synced_folder "./myapp", "/var/www/myapp/current", type: "nfs"1 config.vm.provider "virtualbox" do |v| v.name = "myapp_vagrant" v.customize ["modifyvm", :id, "--cpuexecutioncap", $EXEC_CAP] v.customize ["modifyvm", :id, "--memory", $MEMORY] v.customize ["modifyvm", :id, "--cpus", $CPUS] end end
The command to create the VM using vagrant is: vagrant up
Your VM should now be created on your machine, the next step is to provision it with your Ansible playbook.
Provision the VM with Ansible
To create your own Ansible playbook, you should have a look at Ansible Galaxy: there are many Ansible roles available. You often don’t need to rewrite a role from scratch. You can use generators such as this one. To have an idea of what can be the best practices to write a playbook for small web applications I wrote an article about it. In the end, your Ansible directory should be something like:
├── group_vars │ ├─ prod │ ├─ staging │ └─ vagrant ├── hosts │ ├─ prod │ ├─ staging │ └─ vagrant ├── roles │ ├─ composer │ ├─ ubuntu-apt │ ├─ ubuntu-mysql │ ├─ ubuntu-php │ └─ ubuntu-symfony-nginx ├── vars │ └─ main.yml └─ playbook.yml
You have to configure your playbook for your vagrant.
First, in the hosts/vagrant file, you have to specify the IP of your vagrant:
[vagrant] 10.0.0.10 ansible_ssh_user=vagrant
If you want to choose vars that will only be used by your vagrant, you have to put them in the group_vars/vagrant file. Basically, all your database password should be stored in this file as you want different passwords for each server:
#Example of group_vars file # List of databases to be created postgresql_databases: - name: myapp uuid_ossp: yes # List of users to be created postgresql_users: - name: myapp password: myapp postgresql_user_privileges: - name: myapp db: myapp priv: "ALL" role_attr_flags: "SUPERUSER" postgresql_listen_addresses: - "*" dev_env: true
If you have some variables that should be applied on all your servers, they should be placed in the vars/main.yml file:
#Example of vars/main.yml file timezone: Europe/Paris port: 80 php_date_timezone: "UTC" php_packages: - php5 - php5-fpm - php5-mcrypt - php5-cli - php5-common - php5-curl - php5-dev - php5-gd - php5-ldap - php-apc - php5-apcu - php5-pgsql - php5-intl - php5-mysql - php5-mongo
Finally, your playbook.yml should call all the roles you need to make your VM able to run your application:
#Example of a playbook for a Symfony application - name: Provisioning myapp hosts: all become: true vars_files: - vars/main.yml roles: - ubuntu-apt - create-www-data-user - ssh-keys # need create-www-data-user - ubuntu-php - composer - ubuntu-symfony-nginx - ubuntu-postgresql - blackfire - newrelic-php - nodejs
Ansible needs to have SSH access to the Vagrant so you need to add your SSH key in the vagrant:
- Copy your public key cat ~/.ssh/id_rsa.pub
- Log in the vagrant with `vagrant ssh`
- Add your key in the authorized_keys file: vi .ssh/authorized_keys
- Exit the VM and try to log in with ssh firstname.lastname@example.org
If it’s OK, you can now provision the VM using your ansible playbook: ansible-playbook playbook.yml -i hosts/vagrant. If your playbook has no errors, you will see your project on your browser at the IP of the Virtual Machine. If you see it, congratulations, your development environment is ready.
The production environment
The production environment is easier to setup as the server is already created by the IAAS. You need to perform these steps:
- Add your SSH key to your server and modify the host/prod file so Ansible can find it
- Update the group_vars/prod file with your prod parameters
- Use Ansible to provision your instance in the cloud: ansible-playbook playbook.yml -i hosts/prod
- Then you should use a deployment tool like Capistrano to deploy the application’s files to the host.
Your application has been now fully deployed in production.
Ansible is a great tool for provisioning. Its main advantage is simplicity. Ansible playbooks are easy to understand while remaining powerful. There are a number of articles on Ansible and Ansible playbooks and I have tried to add something valuable to the discussion by focusing on the deployment of web applications on IAAS with Ansible in a step-by-step manner.
If you have any comment or feedback you can start or join a discussion below. You can also find me on Twitter.