Skip to main content Logo (IEC resistor symbol)logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Ansible - pure ssh based configuration management and deployment

Published: 09-03-2013 | Author: Remy van Elst | Text only version of this article

Table of Contents

Ansible is a radically simple model-driven configuration management, deployment,and command execution framework. Other tools in this space have been toocomplicated for too long, require too much bootstrapping, and have too muchlearning curve. By comparison, Ansible is dead simple and painless to extend.Puppet and Chef have about 60k lines of code. Ansibles core is a little over2000 lines.

If you like this article, consider sponsoring me by trying out a Digital OceanVPS. With this link you'll get $100 credit for 60 days). (referral link)

How do I use Ansible

I used chef to deploy and manage all my vps servers which host the cluster onwhich is hosted. I've written a chef-bootstrap script to make sureall the packages to build and compile rvm, ruby and chef are installed, and I'vegot my entire cluster in chef cookbooks. It works well, but it is bloated. Itrequires ruby on the server nodes, and that sometimes gives problems on the vpsservers I have, since none of them have more than 128MB ram.

I discovered Ansible via reddit, and I'm sold. I've converted the scripts I usedwith chef to ansible format, and not only are they shorter, the biggestadvantage is that Ansible does not require ruby on the host. I can start with anewly installed vps with ssh installed. Nothing more.

Update 2013-03-09 I've also contributed a few pieces of code to Ansible,namely for the apt and yum modules. It is such a great project!

You can find more about Ansible on their website.

Below I'll explain how to install and setup ansible (on debian 6), and I'll givesome examples on how to use it.

Install ansible

git clone git:// ./ansiblesudo make install

You might need some python modules

sudo easy_install jinja2 sudo easy_install pyyamlsudo easy_install paramiko

Add localhost to test

source ./hacking/env-setupecho "" > ~/ansible_hostsexport ANSIBLE_HOSTS=~/ansible_hosts

Setup ssh-agent

ssh-agent bashssh-add ~/.ssh/id_rsa

Add servers/hosts

Edit the ~/ansible_hosts file:

Make sure to add the ssh-key to the host
ssh-copy-id -i ~/.ssh/ '-p 2222'    

Test if it works

 ansible all -m | success >> {"ping": "pong"} | success >> {"ping": "pong"} | success >> {"ping": "pong"} | success >> {"ping": "pong"} | success >> {"ping": "pong"} | success >> {"ping": "pong"} | FAILED => FAILED: timed out

It is working. Server vps7 is down, so it reports it correct.


Get external IP of hosts in groups "nodes" and "master"
ansible nodes:master -a "wget -qO -" -f 10 | success | rc=0 >> | success | rc=0 >>[...]
Install a package on all hosts in group "nodes" using apt
ansible nodes -m apt -a "pkg=nano state=latest" | FAILED >> {    "failed": true,     "msg": "Could not import python modules: apt, apt_pkg. Please install python-apt package."} | success >> {    "changed": false}    
Fix the above error

Install package python-apt manually using the "apt-get -y install python-apt"command. Run it via sudo (-s) as user remy (-u remy) and ask me for the sudopassword (-K)

ansible -v -a "apt-get -y install python-apt" -f 10 -u remy -s -Ksudo password: | success | rc=0 >>Reading package lists...Building dependency tree...Reading state information...The following extra packages will be installed:iso-codes python-apt-common [...]Setting up python-apt-common ( ...Setting up python-apt ( ...Setting up iso-codes (3.23-1) ...

My Playbooks

I've got my playbooks on my github account. You can see them and use them asreference, and if you want, improve them..

Playbook tips

Get the network inteface (eth0, venet0) in a value (item [0] is lo):

vars:  interface: ${ansible_interfaces[1]}

Variables with quotes:

vars:  compress: '( "application/x-javascript", "text/css", "text/html", "text/plain" )'

Result of a command in a variable:

vars:  date: $PIPE(date)  hard_command: $PIPE("this_is_a_long_command | which gets piped | and grepped")

Running on CentOS and Debian and have a yum and an apt statement with an if? Usethe pkg_mgr variable:

vars:  pkg_mgr: ${ansible_pkg_mgr}tasks:  - name: Install vim and git with ${pkg_mgr}    action: $pkg_mgr name=$item state=latest    with_items:      - vim      - git

Have special IPv6 config? Or want general if execution based on variables?Define a variable (this is in the ansible_hosts file) 

Now in a template (example for nginx):

{% if ipv6 == "True" %}  listen [::]:80 default_server; # ipv6only=on;  {% else %}  listen 80 default_server;{% endif %}

This makes sure nginx runs on non-ipv6 nodes.

Tags: ansible, chef, configuration-management, deployment, devops, featured-one, puppet, python, tutorials