Ansible

Using Ansible with CloudSigma

In a recent article I wrote about how you can use Salt to manage your CloudSigma infrastructure.  However, another tool mentioned in that article was Ansible, which is in many ways similar to Salt.

Since we’re all about choice here at CloudSigma, I thought it was only fair to also provide Ansible-users (or aspiring users) with an easy way to tap into their cloud infrastructure on CloudSigma.

Besides, if you’re on Mac OS X, installing Ansible is dead simple using the fantastic Homebrew.  In fact, all you need to do is to run:
[bash light=”true”] $ brew install ansible
[/bash]

Next, if you’re on Linux, the tool is most likely available from your distribution’s package management tool.

First, you need to have the tool installed. Next, you need to get the CloudSigma dynamic inventory model, (which in turn depends on pycloudsigma).

Next, with the module installed, you are able to easily connect to your servers using the server names from the web interface (or API rather) without having to manually look up the IP for each node.

For instance, if you want to run the ‘ping’ module against the server ‘ansible.local’ (using the username ‘cloudsigma’), all you need to do is to run the following command:

[bash light=”true”] $ ansible -u cloudsigma ansible.local -m ping
ansible.local | success >> {
“changed”: false,
“ping”: “pong”
}
[/bash]

In fact, the ping-command is  more or less the least complex thing you can do with Ansible (but it’s still very handy for checking on servers).

After that, let’s do something more interesting. For  example, let’s create a Playbook named ‘nginx.yml’ with the following content:


---
- hosts: ansible.local
remote_user: cloudsigma
sudo: yes
tasks:
- name: install-nginx
apt: pkg=nginx state=installed update_cache=true
notify:
- make-sure-nginx-is-runnning
handlers:
- name: make-sure-nginx-is-runnning
service: name=nginx state=started

Then, what this will do is to:

  • Connect to the host ‘ansible.local’ with the username ‘cloudsigma’.
  • Use ‘sudo’ whenever needed.
  • Install Nginx.
  • Make sure Nginx is running.

Also, it is assumed that ‘ansible.local’ exists in the database, and that it is running Ubuntu (or Debian).

In addition, with the file ‘nginx.yml’ created, we can now run the following command:

[bash light=”true”] $ ansible-playbook nginx.yml

PLAY [ansible.local] **********************************************************

GATHERING FACTS ***************************************************************
ok: [ansible.local]

TASK: [install-nginx] *********************************************************
changed: [ansible.local]

NOTIFIED: [make-sure-nginx-is-runnning] ***************************************
changed: [ansible.local]

PLAY RECAP ********************************************************************
ansible.local : ok=3 changed=2 unreachable=0 failed=0
[/bash]

Clearly, as you can see above, we installed Nginx and ensured that it is was running. The target was just a single server. However,  it could well have been hundreds of servers. The process would have been the same.

Finally, if you want to learn more about the tool, take a look at their online documentation to learn more about the various components.

In conclusion, please note that only running servers will be placed in the inventory list, you bring a new server online, you will need to run the script with the ‘–refreshdb’ flag.

About Viktor Petersson

Former VP of Business Development at CloudSigma. Currently CEO at WireLoad and busy making a dent in the Digital Signage industry with Screenly. Viktor is a proud geek and loves playing with the latest technologies.