Skip to main content

Structuring a YAML Playbook with Ansible

Hands-On Lab


Photo of Elle Krout

Elle Krout

Content Team Lead in Content





YAML, or YAML Ain't Markup Language, is a data serialization language geared toward making human-readable files. Oftentimes you see YAML used in configuration files and other places that often require human editing and use. Ansible is just one bit of software that utilizes YAML, but with a confident knowledge of the data serialization language, writing playbooks can be a simple task. No Ansible experience is needed for this activity.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Structuring a YAML Playbook with Ansible


We've been hired as systems administrators for a company that uses Ansible for its configuration management. While we have no prior Ansible experience, we are familiar with YAML and have been assured that with nothing but our YAML knowledge and some guiding, we should be able to write a playbook, which is a file that configures one aspect of a managed system.

Ansible has already been installed on a test system. Switch to the ansible user with su - ansible. The password is the same as the cloud_user's. The playbook file we create can be placed in the home directory.

Before beginning, please forward the ansible user's SSH key:

[ansible@host]$ ssh-copy-id localhost

Create a YAML Playbook

The playbook should be structured as follows and saved as mariadb.yaml in the home directory. Mark the top of the file by signaling the start of the document. Include an inline comment about the purpose of this playbook (which is to install MariaDB). It will look like this:

--- # Install MariaDB

The rest of the document should be structured as a "sequence of mappings" (or a list of key value pairs). The first mapping should be hosts: localhost. Underneath this line, include the following table of key value pairs. These should be in the same collection as hosts: localhost:


In this same collection, set a tasks mapping. This will contain a sequence of mappings, similar to the parent setup of this line. Under the tasks list, create another sequence of mappings with the following key value pairs. This should be a separate collection (i.e., indented):

nameInstalling MariaDB
yumSee below
notifystartservice (This should be a list)

The yum key should contain these two mappings (i.e., a mapping of mappings):


On the same level as the tasks mappings, create a handlers sequence of mappings for the following key value pairs:

serviceSee below

The service key should contain these two mappings (i.e., a mapping of mappings):


The completed file should look like this:

--- # Install MariaDB
- hosts: localhost
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
    - name: Installing MariaDB
        name: mariadb-server
        state: latest
        - startservice
    - name: startservice
        name: mariadb
        state: restarted

Run the Playbook to Check Your Work

When the playbook is finished, run it with:

[ansible@host]$ ansible-playbook mariadb.yaml

We can troubleshoot any errors we get, guided by the informative messages that pop up if things go wrong (typos, faulty indents, etc.) and keep running the playbook over until it works. Check the results with:

[ansible@host]$ systemctl status mariadb

If we see it running and Active, we're good to go.


It looks like our YAML playbook worked. It installed MariaDB and started it up, just like we wanted. Congratulations!