Skip to main content

Raymii.org 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

  • 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.

    If you like this article, consider sponsoring me by trying out a Digital Ocean VPS. 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 on which raymii.org 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://github.com/ansible/ansible.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 "127.0.0.1" > ~/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:

    [spcs]
    vps1.sparklingclouds.nl:2222
    vps3.sparklingclouds.nl:2222
    vps5.sparklingclouds.nl:2222
    
    [raymii]
    raymii.nl:7777
    ssh.raymii.org:7777
    
    [hostedpiwik]
    vps7.sparklingclouds.nl:2233
    vps17.sparklingclouds.nl:2222
    
    [z1s.org]
    vps21.sparklingclouds.nl:2222
    
    Make sure to add the ssh-key to the host
    ssh-copy-id -i ~/.ssh/id_rsa.pub '-p 2222 vps3.sparklingclouds.nl'    
    

    Test if it works

     ansible all -m ping
    
    
    vps1.sparklingclouds.nl | success >> {
    "ping": "pong"
    }
    
    vps.raymii.org | success >> {
    "ping": "pong"
    }
    
    vps3.sparklingclouds.nl | success >> {
    "ping": "pong"
    }
    
    vps5.sparklingclouds.nl | success >> {
    "ping": "pong"
    }
    
    vps6.sparklingclouds.nl | success >> {
    "ping": "pong"
    }
    
    vps17.sparklingclouds.nl | success >> {
    "ping": "pong"
    }
    
    vps7.sparklingclouds.nl | FAILED => FAILED: timed out
    

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

    Examples

    Get external IP of hosts in groups "nodes" and "master"
    ansible nodes:master -a "wget -qO - http://ifconfig.me/ip" -f 10     
    
    vps8.sparklingclouds.nl | success | rc=0 >>
    84.200.77.167
    
    vps11.sparklingclouds.nl | success | rc=0 >>
    192.71.245.64
    
    [...]
    
    Install a package on all hosts in group "nodes" using apt
    ansible nodes -m apt -a "pkg=nano state=latest"
    
    vps11.sparklingclouds.nl | FAILED >> {
        "failed": true, 
        "msg": "Could not import python modules: apt, apt_pkg. Please install python-apt package."
    }
    
    vps1.sparklingclouds.nl | 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 vps11.sparklingclouds.nl -a "apt-get -y install python-apt" -f 10 -u remy -s -K
    
    sudo password: 
    vps11.sparklingclouds.nl | 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 (0.7.100.1+squeeze1) ...
    Setting up python-apt (0.7.100.1+squeeze1) ...
    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):

    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? Use the 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)

    vps5.sparklingclouds.nl:444 ipv6=True
    vps6.sparklingclouds.nl:444 ipv6=True
    vps12.sparklingclouds.nl:444 
    vps13.sparklingclouds.nl:444 
    

    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