Setting Up Ansible for Configuration Management on Ubuntu: Creating Playbooks

Updated on November 19, 2020
Setting Up Ansible for Configuration Management on Ubuntu: Creating Playbooks header image

Playbooks in Ansible are what makes Ansible so handy. Playbooks allow for executing routine tasks with several modules. When should we use playbooks?

Let's take a look at a routine maintenance task: updating your servers. We would not have to use a playbook for this, given that it is just one command: apt-get update. We could just use one single command to update software on all our servers. However, for deploying something like a web or database server, more work is needed: not just a single command. Playbooks are prefect for this case. They allow you to use multiple modules, which are commands that can be executed on a server. For example, there are modules for copying files, and performing shell commands over SSH.

So how do we use playbooks? Let's get started!

Step 1: Creating a YAML (.yml) file

Ansible playbooks are written as .yml files; Ansible cannot interpret other formats. An example Ansible playbook looks like this:

---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version
    yum: pkg=httpd state=latest
  - name: write the apache config file
    template: src=/srv/httpd.j2 dest=/etc/httpd.conf
    notify:
    - restart apache
  - name: ensure apache is running (and enable it at boot)
    service: name=httpd state=started enabled=yes
  handlers:
    - name: restart apache
      service: name=httpd state=restarted

(Taken from Ansible's documentation)

Step 2: Understanding playbooks: tasks and handlers

As you can see, there are multiple sections in this playbook. First off, you will see the hosts: line. This determines on which hosts the playbook should be executed. You can also see that we have defined a couple of variables that can be accessed and used throughout the playbook. You will then see a number of tasks. These tasks could be named something like "Install Apache", for example. The second task in the example writes to the Apache config. You will then see the following:

    notify:
    - restart apache

This tells a task to execute a certain handler. In this case, it's referring to the handler restart apache, which can be found at the bottom of the playbook:

  handlers:
    - name: restart apache
      service: name=httpd state=restarted

Handlers are convenient because, taking the example, you might want to restart Apache multiple times. In this case, you do not have to repeat creating a task for every single time you need to restart Apache but instead, you can just refer to the same handler every time.

Step 3: Executing playbooks

Say you want to deploy an Apache server. Well, as you can see, we have just created a playbook for that. Executing playbooks can be done by using the ansible-playbook command. For example, if we were to execute this playbook on all servers in the webservers category (you will need to have this category in your /etc/ansible/hosts file), you'd run the following command given that the name of your playbook file is apache.yml:

ansible-playbook -i webservers apache.yml

Congratulations! You have now executed a playbook on all servers in your webservers category. You'll now just have to wait for the deployment to finish, and you'll see that Apache has successfully been installed and configured on these servers.