Using Ansible with CloudSigma

In a recent article I wrote about how you can use Salt to manage your CloudSigma infrastructure. 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 Ansible-users) with an easy way to tap into their cloud infrastructure on CloudSigma.

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

If you’re on Linux, Ansible is most likely available from your distribution’s package management tool.

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

With the module installed, you are now 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"

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

Now let’s do something more interesting. Let’s create a Playbook named ‘nginx.yml’ with the following content:

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

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.

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

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

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

If you want to learn more about Ansible, take a look at their online documentation to learn more about the various components.

Also, 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.

Share this Post

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.