Deploy Web Applications on IaaS with Ansible

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

https://cloudacademy.com/going-deeper-into-ansible-playbooks/
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:

  1. Copy your public key cat ~/.ssh/id_rsa.pub
  2. Log in the vagrant with `vagrant ssh`
  3. Add your key in the authorized_keys file: vi .ssh/authorized_keys
  4. Exit the VM and try to log in with ssh vagrant@10.0.0.10

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

IAAS with Ansible
The production environment is easier to setup as the server is already created by the IAAS. You need to perform these steps:

  1. Add your SSH key to your server and modify the host/prod file so Ansible can find it
  2. Update the group_vars/prod file with your prod parameters
  3. Use Ansible to provision your instance in the cloud: ansible-playbook playbook.yml -i hosts/prod
  4. 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.

Conclusion

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.

Avatar

Written by

Maxime Thoonsen

Related Posts

Avatar
Adam Hawkins
— July 9, 2019

Top 20 Open Source Tools for DevOps Success

Open source tools perform a very specific task, and the source code is openly published for use or modification free of charge. I've written about DevOps multiple times on this blog. I reiterate the point that DevOps is not about specific tools. It's a philosophy for building and improv...

Read more
  • Ansible
  • Chef
  • configuration management
  • DevOps
  • devops tools
  • Docker
  • infrastructure-as-code
  • Kubernetes
  • telemetry
Avatar
Guy Hummel
— March 4, 2019

What is Ansible?

What is Ansible? Ansible is an open-source IT automation engine, which can remove drudgery from your work life, and will also dramatically improve the scalability, consistency, and reliability of your IT environment. We'll start to explore how to automate repetitive system administratio...

Read more
  • Ansible
  • Cloud Computing
Stefano Bellasio
Stefano Bellasio
— July 7, 2016

4 New Webinars for July 2016: Ansible, AWS Lambda, A/B Testing Algorithms in the Cloud, and Office Hours

Hello! This is Stefano. It's been a while since my last post on our blog. I've been busy working with our great team at Cloud Academy, but I would like to use this article today to talk about something we've been really enjoying these last few months: doing cloud training webinars with ...

Read more
  • Ansible
  • AWS
Avatar
Chandan Patra
— October 21, 2015

Ansible and AWS: Cloud IT Automation Management

With things moving a bit more slowly through the holiday season, we’re going to re-run some of our most popular posts from 2015. Enjoy! The kinds of virtual infrastructures that define the cloud computing ecosystem demand a high level of automation. As the number of virtual servers use...

Read more
  • Ansible
  • AWS
  • Azure
Avatar
Sanket Dangi
— January 27, 2015

Building Ansible Playbooks Step-by-Step

Update 2019: We've recently developed a Learning Path, Introduction to Ansible, which will help you to get you started using Ansible to automate common IT tasks, you will learn about Configuration Management and you'll be able to practice your knowledge on Ansible through a series of ha...

Read more
  • Ansible
Avatar
David Clinton
— January 26, 2015

Cloud Technology and Security Alert News Digest – Issue #10

Update 2019: We've been busy working on some great training content around security, check out the Cloud Academy library to prepare on all-things cloud security. Welcome to the Cloud Technology and Security Alert News Digest. This week we've got word of some big platform changes ...

Read more
  • Ansible
  • Azure
  • Chef
  • Containers
  • Security
Avatar
Sanket Dangi
— January 12, 2015

Going Deeper into Ansible Playbooks

Update 2019: We've recently developed a Learning Path, Introduction to Ansible, which will help you to get you started using Ansible to automate common IT tasks, you will learn about Configuration Management and you'll be able to practice your knowledge on Ansible through a series of ha...

Read more
  • Ansible
  • Cloud Computing
Avatar
Sanket Dangi
— December 29, 2014

Get Started with Ansible on the Cloud

Update 2019: We've recently developed a Learning Path, Introduction to Ansible, which will help you to get you started using Ansible to automate common IT tasks, you will learn about Configuration Management and you'll be able to practice your knowledge on Ansible through a series of ha...

Read more
  • Ansible
  • AWS