Structuring a YAML Playbook with Ansible

Hands-On Lab

 

Photo of Elle Krout

Elle Krout

Content Team Lead in Content

Length

00:30:00

Difficulty

Beginner

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

Introduction

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:

Key Value
remote_user ansible
become yes
become_method sudo
connection ssh
gather_facts yes

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):

Key Value
name Installing MariaDB
yum See below
notify startservice (This should be a list)

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

Key Value
name mariadb-server
state latest

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

Key Value
name startservice
service See below

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

Key Value
state restarted
name mariadb

The completed file should look like this:

--- # Install MariaDB
- hosts: localhost
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  tasks:
    - name: Installing MariaDB
      yum:
        name: mariadb-server
        state: latest
      notify:
        - startservice
  handlers:
    - name: startservice
      service:
        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.

Conclusion

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