Structuring a YAML Playbook with Ansible

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create a YAML Playbook

Create a mariadb.yaml file using the playbook description in the instructions. Save the file with :wq!.

Run the Playbook to Check Your Work

Ensure your YAML is properly written by running the playbook.

Additional Resources

You've been hired as a systems administrator for a company that uses Ansible for its configuration management. While you have no prior Ansible experience, you are familiar with YAML and have been assured that with nothing but your YAML knowledge and some guiding, you should be able to write a "playbook" (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, forward the ansible user's SSH key:

ssh-copy-id localhost

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

  • 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 key-value pairs (formatted below as key:value); these should be in the same collection as hosts: localhost:

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

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

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

    • name: startservice
    • service: See below
  • The service key should contain these two mappings (i.e., a mapping of mappings):

    • name: mariadb
    • state: restarted

Please view the diagram for a visual diagram of this setup.

When finished, run the playbook with:

ansible-playbook mariadb.yaml

Glossary

  • Sequences are lists

    • Item 1
    • Item 2
  • Mappings are key-value pairs

    key1: value1 key2: value2

  • A sequence of mappings:

    • key1: value1 key2: value2
  • A mapping of mappings:

    overallkey: key1: value1 key2: value2

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Get Started
Who’s going to be learning?

How many seats do you need?

  • $499 USD per seat per year
  • Billed Annually
  • Renews in 12 months

Ready to accelerate learning?

For over 25 licenses, a member of our sales team will walk you through a custom tailored solution for your business.


$2,495.00

Checkout
Sign In
Welcome Back!
Thanks for reaching out!

You’ll hear from us shortly. In the meantime, why not check out what our customers have to say about ACG?