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.