This article explains how to easily deploy 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 a 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 on 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. At 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 need to have an 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 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.
Two New EC2 Instance Types Announced at AWS re:Invent 2018 – Monday Night Live
Let’s look at what benefits these two new EC2 instance types offer and how these two new instances could be of benefit to you. Both of the new instance types are built on the AWS Nitro System. The AWS Nitro System improves the performance of processing in virtualized environments by...
Google Cloud Certification: Preparation and Prerequisites
Google Cloud Platform (GCP) has evolved from being a niche player to a serious competitor to Amazon Web Services and Microsoft Azure. In 2018, research firm Gartner placed Google in the Leaders quadrant in its Magic Quadrant for Cloud Infrastructure as a Service for the first time. In t...
Understanding AWS VPC Egress Filtering Methods
Security in AWS is governed by a shared responsibility model where both vendor and subscriber have various operational responsibilities. AWS assumes responsibility for the underlying infrastructure, hardware, virtualization layer, facilities, and staff while the subscriber organization ...
S3 FTP: Build a Reliable and Inexpensive FTP Server Using Amazon’s S3
Is it possible to create an S3 FTP file backup/transfer solution, minimizing associated file storage and capacity planning administration headache?FTP (File Transfer Protocol) is a fast and convenient way to transfer large files over the Internet. You might, at some point, have conf...
Microservices Architecture: Advantages and Drawbacks
Microservices are a way of breaking large software projects into loosely coupled modules, which communicate with each other through simple Application Programming Interfaces (APIs).Microservices have become increasingly popular over the past few years. The modular architectural style,...
What Are Best Practices for Tagging AWS Resources?
There are many use cases for tags, but what are the best practices for tagging AWS resources? In order for your organization to effectively manage resources (and your monthly AWS bill), you need to implement and adopt a thoughtful tagging strategy that makes sense for your business. The...
How to Optimize Amazon S3 Performance
Amazon S3 is the most common storage options for many organizations, being object storage it is used for a wide variety of data types, from the smallest objects to huge datasets. All in all, Amazon S3 is a great service to store a wide scope of data types in a highly available and resil...
How to Optimize Cloud Costs with Spot Instances: New on Cloud Academy
One of the main promises of cloud computing is access to nearly endless capacity. However, it doesn’t come cheap. With the introduction of Spot Instances for Amazon Web Services’ Elastic Compute Cloud (AWS EC2) in 2009, spot instances have been a way for major cloud providers to sell sp...
What are the Benefits of Machine Learning in the Cloud?
A Comparison of Machine Learning Services on AWS, Azure, and Google CloudArtificial intelligence and machine learning are steadily making their way into enterprise applications in areas such as customer support, fraud detection, and business intelligence. There is every reason to beli...
How to Use AWS CLI
The AWS Command Line Interface (CLI) is for managing your AWS services from a terminal session on your own client, allowing you to control and configure multiple AWS services.So you’ve been using AWS for awhile and finally feel comfortable clicking your way through all the services....
AWS Summit Chicago: New AWS Features Announced
Thousands of cloud practitioners descended on Chicago’s McCormick Place West last week to hear the latest updates around Amazon Web Services (AWS). While a typical hot and humid summer made its presence known outside, attendees inside basked in the comfort of air conditioning to hone th...
From Monolith to Serverless – The Evolving Cloudscape of Compute
Containers can help fragment monoliths into logical, easier to use workloads. The AWS Summit New York was held on July 17 and Cloud Academy sponsored my trip to the event. As someone who covers enterprise cloud technologies and services, the recent Amazon Web Services event was an insig...