This is a text-only version of the following page on https://raymii.org:
---
Title       :   Ansible - pure ssh based configuration management and deployment
Author      :   Remy van Elst
Date        :   09-03-2013
URL         :   https://raymii.org/s/tutorials/Ansible_Deployment_Framework.html
Format      :   Markdown/HTML
---



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.

<p class="ad"> <b>Recently I removed all Google Ads from this site due to their invasive tracking, as well as Google Analytics. Please, if you found this content useful, consider a small donation using any of the options below:</b><br><br> <a href="https://leafnode.nl">I'm developing an open source monitoring app called  Leaf Node Monitoring, for windows, linux & android. Go check it out!</a><br><br> <a href="https://github.com/sponsors/RaymiiOrg/">Consider sponsoring me on Github. It means the world to me if you show your appreciation and you'll help pay the server costs.</a><br><br> <a href="https://www.digitalocean.com/?refcode=7435ae6b8212">You can also sponsor me by getting a Digital Ocean VPS. With this referral link you'll get $100 credit for 60 days. </a><br><br> </p>


#### 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][2].

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.][3].

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

  [1]: https://www.digitalocean.com/?refcode=7435ae6b8212
  [2]: http://ansible.github.com/
  [3]: https://github.com/RaymiiOrg/ansible

---

License:
All the text on this website is free as in freedom unless stated otherwise.
This means you can use it in any way you want, you can copy it, change it
the way you like and republish it, as long as you release the (modified)
content under the same license to give others the same freedoms you've got
and place my name and a link to this site with the article as source.

This site uses Google Analytics for statistics and Google Adwords for
advertisements. You are tracked and Google knows everything about you.
Use an adblocker like ublock-origin if you don't want it.

All the code on this website is licensed under the GNU GPL v3 license
unless already licensed under a license which does not allows this form
of licensing or if another license is stated on that page / in that software:

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

Just to be clear, the information on this website is for meant for educational
purposes and you use it at your own risk. I do not take responsibility if you
screw something up. Use common sense, do not 'rm -rf /' as root for example.
If you have any questions then do not hesitate to contact me.

See https://raymii.org/s/static/About.html for details.