Skip to main content Logo

Quis custodiet ipsos custodes?
Home | About | All pages | Cluster Status | 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

❗ This post is over nine years old. It may no longer be up to date. Opinions may have changed.

Table of Contents

  • Install ansible
  • Examples
  • My Playbooks

  • Ansible is a radically simple model-driven configuration management, deployment, and command execution framework. Other tools in this space have been too complicated for too long, require too much bootstrapping, and have too much learning 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 over 2000 lines.

    I'm developing an open source monitoring app called Leaf Node Monitoring, for windows, linux & android. Go check it out!

    Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.

    You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days.

    How do I use Ansible

    I used chef to deploy and manage all my vps servers which host the cluster on which is hosted. I've written a chef-bootstrap script to make sure all the packages to build and compile rvm, ruby and chef are installed, and I've got my entire cluster in chef cookbooks. It works well, but it is bloated. It requires ruby on the server nodes, and that sometimes gives problems on the vps servers 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 used with chef to ansible format, and not only are they shorter, the biggest advantage is that Ansible does not require ruby on the host. I can start with a newly 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 give some examples on how to use it.

    Install ansible

    git clone git://
    cd ./ansible
    sudo make install

    You might need some python modules

    sudo easy_install jinja2 
    sudo easy_install pyyaml
    sudo easy_install paramiko

    Add localhost to test

    source ./hacking/env-setup
    echo "" > ~/ansible_hosts
    export ANSIBLE_HOSTS=~/ansible_hosts

    Setup ssh-agent

    ssh-agent bash
    ssh-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 ping
     | 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 sudo password (-K)

    ansible -v -a "apt-get -y install python-apt" -f 10 -u remy -s -K
    sudo 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 as reference, and if you want, improve them..

    Playbook tips

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

      interface: ${ansible_interfaces[1]}

    Variables with quotes:

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

    Result of a command in a variable:

      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? Use the pkg_mgr variable:

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

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

    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