Category Archives: ansible

Starting out with Ansible

In my previous post How to log into a Linux server without a password I generated ssh keys to allow me to log into remote Linux servers without a password. The main intention was to allow me to use ansible on a controller machine to interact with my servers.

Now let’s get started writing/running Ansible commands and playbooks.

If you want to test things on localhost we first need to edit the file /etc/ansible/hosts and add the following

localhost ansible_connection=local

We also edit the same file to add any remote hosts that we intend to connect to. We can group remote servers into named groups, so for example interacting with London based servers separately to New York servers.

To group servers within the hosts file we use syntax similar to Window ini files, i.e.

[london]
ldn_server1
ldn_server2

[new_york]
ny_server1

Playbooks and Ad-hoc commands

A playbook is simply a YAML Ansible script that we can execute against one or more servers but in some cases we just want to execute ad-hoc commands (i.e. not bother creating a playbook).

Here’s an example of and ad-hoc command which will run the df, disk free command against all our servers.

ansible all -m command -a "df"

As you can see after the ansible command (in this example) we list the servers (or group(s)) we want to run the commands against, in this case we’re running the command against all servers. The -m switch tells Ansible to use the module, in this example the module command. As this is the default module we can omit this from the entered command to instead use

ansible all -a "df"

The -a switch switch denotes the arguments that we need to send to the command module. In this instance we’re sending the Linux command df.

Once run, this will display (for each server in our hosts file) the free disk space.

Creating a playbook

So ad-hoc commands are very useful, but we can take this to another level by creating scripts (known as playbooks in Ansible) to run our commands.

Let’s create the equivalent of the ad-hoc command we just ran.

Create a directory for your playbooks and then create the file df.yml, place the following code into it

---
- hosts: all
  tasks:
  - name: Run df across all servers
    command: df
    register: out
  - debug: msg={{out.stdout}}

Now from the folder containing the yml file run ansible-playbook df.yml

The – – – can be used optionally, to denote the start of a YAML file (and … can be used optionally to end one). For some reason most examples I’ve seen have the – – – but not the …, so I’ve included it in this script, but it’s not needed.

YAML files use -hosts: to denote which servers we want this playbook to interact with, followed by the list of tasks. We can optionally name the tasks, then list the commands for each task.

In the above we create a single task to run the command df, by default Ansible will simply tell us whether the command ran, but we’ll probably want to see the output from the server calls, hence the – debug section and the register: out.

Creating an ansible dockerfile

I was trying to run the ansible docker image on Docker hub, but it kept failing, so I went through the steps listed in http://docs.ansible.com/ansible/latest/intro_installation.html inside of an Ubunto docker image

  • apt-get update
  • apt-get install software-properties-common
  • apt-add-repository ppa:ansible/ansible
  • apt-get update
  • apt-get install ansible

Then running ansible –version demonstrated everything was working.

I decided to use the same commands to create a Dockerfile and hence create my own image of ansible.

  • Create an ansible folder
  • Create the file Dockerfile in the ansible folder

Please note, this is not a complete implementation of a Docker image of ansible so much as a starting point for experimenting with ansible.

Now we’ll create a fairly simple image based on ubuntu and install all the libraries etc. as above. So the Dockerfile should have the following contents

FROM ubuntu:latest

RUN apt-get -y update
RUN apt-get -y install software-properties-common
RUN apt-add-repository ppa:ansible/ansible
RUN apt-get -y update
RUN apt-get -y install ansible

From the ansible folder run sudo docker build -t ansible ., if all goes well you’ll have an image name ansible created. Just run sudo docker images to see it listed.

Now run sudo docker run -it ansible to run the image in interactive mode and then within the container run ansible –version to see if everything worked.

Now we’re up and running there’s obviously more to do to really use ansible and hopefully I’ll cover some of those topics in subsequent posts on ansible.