What Is YAML in Ansible and How It Works?

YAML in Ansible

What is YAML in Ansible?

YAML stands for Ain’t Markup Language. It is a data serialization language designed to be directly writable and readable by humans. It is commonly used in many applications where data is being stored and transmitted over the network. It is a scripting language so we can communicate with other languages using YAML.

All Ansible playbooks are written in YAML. Ansible playbooks are text files or configuration files that are written in particular formate called a YAML. If you have worked with other data structure formats like XML or JSON you should easily pick it up.

Don’t worry if you have have not worked with any of this. You should still be able to easily pick it up going through the coding exercises that come with this course.

Working with YAML

YAML is used to represent data in this case configuration data. You can see the sample YAML file in the following image.

Ansible YAML

Let’s take a closer look at YAML. If you take the data in its simplest form such as key-value pair this is how we would define it in YAML key and value separated by a colon. The keys are fruit, vegetable, liquid, and meat and the values are apple, carrot, water, and chicken. You must have a space followed by the colon to differentiating the key and value.

Ansible YAML

Let’s take a look at how an array is represented. We would like to list some Fruits and Vegetables. We would say Fruits followed by the colon, on the next line enter each item with a “-“ in the front. The “-“ indicates that its an element of an array.

Ansible YAML

Let’s take a look at the dictionary. A dictionary is a setup properties group together under an item. Here we try to represent the nutrition information of two Fruits. The Calories, Fat, and Carbs are different for each fruit. You must have an equal number of spaces before the properties of a single item so they are all aligned together.

Ansible YAML

Let’s take it to another level. You can have a list containing dictionaries containing lists. In this case, I have a list of fruits and the elements of the list are Banana and Grape. But each of these elements is further dictionaries containing nutrition information.

Ansible YAML

YAML Exercises

Exercise 1 :- A dictionary employee is given in the following table. Create a YAML file using this data.

YAML exercise

Solution :- You can see the YAML file in the following image:

YAML exercise

Exercise 2 :- A dictionary employee is given in the following table. Create a YAML file using this data.

YAML exercise

Solution :- You can see the YAML file in the following image:

YAML exercise

Exercise 3 :- I want to record information about the multiple employees named hitesh, jayesh and vyom. Convert the dictionary employees to an array of employees.

Solution :- You can see the Employees YAML file in the following image:

YAML exercise

Once you are finished, you can proceed to the next chapter.

For more information about the YAML file, visit the YAML documentation.

You should also read the following chapters:
      1. Introduction
      2. Lab Setup – Install Ansible
      3. Ansible Inventory
      5. Ansible Playbooks
      6. Ansible Modules
      7. Ansible Variables
      8. Ansible Conditionals and Loops
      9. Ansible Roles
   10. Project

About Hitesh Jethva

I am Hitesh Jethva Founder and Author at LinuxBuz.com. I felt in love with Linux when i was started to learn Linux. I am a fan of open source technology and have more than 15+ years of experience in Linux and Open Source technologies.

View all posts by Hitesh Jethva