First steps with Ansible


I remember having to install a new web server next to an existing one. The only requirement my boss had was it had to be "the same" as the existing one. There were two problems:

  • no one had ever documented which packages were needed to run our software
  • the existing server hadn't been upgraded in ages.

I had to use trial and error to get the new server up and running and still I wasn't confident everything was properly installed. What I needed at that time was a way to manage my server configuration.

In this blog post I will tell you about my experience with server provisioning, why I chose Ansible and I will show you how to install a web server.

Configuration management

Not so long ago installing a new server was the domain of your local SysAdmin hero. He would perform his magic to end up with a running server. Nowadays you can get a running server for free with your doppio espresso. All you need now is install your software and go.

There are a lot of tools out there to do exactly that. Popular tools are Puppet, Chef and Ansible. But there are lots more. They allow you to define the packages you need on your server and will install them for you. You can even use them to deploy your websites and applications.

I started out with Puppet a couple of years ago. It was the first tool out there I tried and it really opened my eyes about server provisioning. It is a very popular tool and there are a lot of user contributed modules you can use. However there is somewhat steep learning curve and I eventually ran into resource ordering issues which took me a lot of debugging time.

Later I learned about Ansible and it stuck from the beginning. It can do everything I need (except for pouring me a coffee). And it is a lot easier to learn and maintain. It only needs SSH to connect to your target server.

Let me show you some cool stuff! But first let's install Ansible.

Installing Ansible

Installing Ansible on Ubuntu is as simple as running:

sudo apt-get install ansible

But you probably want to use the newest version:

sudo add-apt-repository ppa:rquillo/ansible
sudo apt-get update
sudo apt-get install ansible

Ansible only depends on python on the host system. The clients need nothing else but SSH! Checkout the installation page for other installation options.

Let's get started!

I have a sandbox vagrant box running on my machine which I'm going to set up as a LAMP server. I'm going to use Ubuntu 13.04 64-bit. Check out http://www.vagrantbox.es/ for other guest OS'es.

To initialize the Vagrant box run:

$ vagrant init ansible http://cloud-images.ubuntu.com/vagrant/raring/current/raring-server-cloudimg-amd64-vagrant-disk1.box

Edit your Vagrantfile and add the following line to set the IP address of your vagrant box to 10.0.0.10:

config.vm.network :private_network, ip: "10.0.0.10"

Then run vagrant up and you're “up” and running!

Now you have to tell Ansible what servers it can reach by creating a hosts file and setting some connection parameters.

mkdir ansible
cd ansible
echo -e "10.0.0.10\tansible_ssh_user=vagrant\tansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key" > hosts

You now have a hosts file that looks like this:

10.0.0.10    ansible_ssh_user=vagrant    ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key

Now you can ping the servers using:

ansible all --inventory-file=hosts --module-name=ping

Ping

You can even execute remote commands:

ansible all --inventory-file=hosts --args='/bin/hostname'

Hostname

ansible all --inventory-file=hosts --args='/usr/bin/uptime'

Uptime

Setting up a LAMP server

Provided you have a Linux server, you need to install Apache, MariaDB and PHP. Also you want the Apache and MariaDB services to run. I will skip MariaDB for now and focus on getting a working "Hello World!" PHP script.

Let's make a todo list:

  1. Install Apache
  2. Install PHP
  3. Start Apache
  4. Show "Hello World!"

Ansible comes with an awesome set of built-in modules including apt, service, file, etc.

Let's use them to process our todo list:

1) install Apache

ansible all --inventory-file=hosts --sudo --verbose --module-name=apt --args="name=apache2 state=present"

install Apache

2) install PHP module for Apache

ansible all --inventory-file=hosts --sudo --verbose --module-name=apt --args="name=libapache2-mod-php5 state=present"

install PHP

3) start Apache

ansible all --inventory-file=hosts --sudo --verbose --module-name=service --args="name=apache2 state=running"

start Apache

4) install Hello World PHP script

echo -e "<?php\necho 'Hello World';\n" > ./index.php
ansible all --inventory-file=hosts --sudo --verbose --module-name=copy --args="src=index.php dest=/var/www/index.php mode=664"

install Hello World PHP script

Now point your web browser to http://10.0.0.10/index.php and voila! Hello World!

You have installed a web server with just a few lines of bash!

Wouldn't it be awesone to checkout your application from GitHub and run it on your server? What about two servers? Or a separate web server and database server? What about a server farm in the cloud on Amazon EC2? Why not even provision your workstation in case it crashes and you need to recover ASAP? All is possible with Ansible!

I hope you enjoyed trying out Ansible! In my next post I will discuss how to organize your provisioning scripts using playbooks and roles.

In later posts I will look into provisioning a Vagrant VM and Docker containers.