Automated server configuration with Ansible

In this series of articles, we’ll be automating server (VPS) configuration with the help of Ansible, an open source IT automation platform. Interested in not having to install and set up Apache/Nginx, PHP and MySQL with WordPress on your VPS servers manually? Then read more!

But first, let me tell you a short story of how I got here.

How I got into using Ansible

It all started when I was working on a Django powered web application. Every cheaper hosting provider supported only web apps written in PHP back then. I decided to buy a small Ubuntu VPS server and set it up myself, learning new skills on the way. Nothing wrong with gaining new knowledge. Three days later, my server was (somewhat) ready to rock: Python, Django, MySQL, Apache, mod_wsgi and all the stuff installed.

[Un]fortunately, a while later, I got another job — replicate the server configuration to a new one. “Easy enough, isn’t it”, I told myself. However, I had to do it manually again. It was fun and enlightening the first time. This time, I was bored as hell. Nothing new to learn here. I think you know the feeling.

I’m a programmer, I like being lazy and let the machine do the job. Couldn’t be this work automated? My initial idea was to create a bash script which would automate it for me. As I started coding the script, it was too lengthy and verbose. If only there was a tool for the task. And that’s how I came across Ansible.

What is Ansible

Ansible is a command-line tool which connects to your servers through SSH and executes specified commands. For example, to echo a text in your server, you would only need to run

$ ansible my-server -m shell -a 'echo hello'

from your local computer’s command-line. These are called “Ad-Hoc Commands“.

Ansible comes with a better way of storing and running commands (= tasks): playbooks.

Ansible Playbooks

Playbook is a file written in the YAML syntax, in order to be readable and easy to work with. It contains tasks which will be executed on the remote server when you run ansible-playbook command (installed alongside Ansible). If you want to group similar tasks together (e.g. add PPA repository of Nginx and then install it), you can use Ansible Roles. Roles can be then specified instead of tasks in the playbook for better project structure.

Example of Ansible Task:

- name: Install Nginx
  apt: name=nginx state=present force=yes

Example of Ansible Role:

- name: Add Nginx PPA
  apt_repository: repo="ppa:nginx/stable" update_cache=yes

- name: Install Nginx
  apt: name=nginx state=present force=yes

- name: Install nginx.conf
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf

- name: Disable default server
  file: path=/etc/nginx/sites-enabled/default state=absent

- name: Create Nginx Sites
  template: src="{{ item }}.conf.j2"
            dest="/etc/nginx/sites-available/{{ item }}.conf"
  with_items: nginx_sites

- name: Enable Ngix Sites
  file: src="/etc/nginx/sites-available/{{ item }}.conf"
        dest="/etc/nginx/sites-enabled/{{ item }}.conf"
  with_items: nginx_sites
  notify: reload nginx

Example of Ansible Playbook:

- name: "Web Dev server with Nginx, HHVM, MariaDB, Composer, NodeJS ready for WordPress development"
  hosts: web-dev
  sudo: yes
  remote_user: lamosty

  - common
  - hhvm
  - composer
  - mariadb
  - nginx
  - nodejs
  - web-dev

YAML is pretty much legible and the “code” shown above quite self-explanatory.

Installing Ansible

You can install Ansible in many ways: from source (git), with apt-get (Ubuntu), brew on OS X, etc.

Getting started

It is best to read Ansible official docs. I find them easy to read and understandable.

What’s next

I’ll walk you through creating a complete Ansible playbook for running WordPress on your VPS server. The playbook will include roles such as installing Nginx, HHVM (or PHP-FPM), MySQL (or MariaDB), latest WordPress and WP-CLI. I’ll try to optimize the configuration of each aforementioned software to be as performing as possible (with caching, monitoring and basic security). You can track my progress at wordpress-ansible GitHub repository.

Leave a comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: