Why YAML is so important in Ansible?

Table of Content

  1. What is YAML?
  2. Why Ansible needs YAML?
  3. What are rules for writing Ansible Playbook using YAMLs?
  4. Advantages of YAML in Ansible
  5. YAML vs Other Data Serialization Formats

What is YAML?

YAML is a recipe for computers that helps them understand what information means. It looks like a plan with items and steps, but it tells the computer what to do instead of making food.

Here are some points which will help you understand YAML -

  1. Let's say you want to bake a cake and need a plan to follow. The recipe tells you what you need to make the cake and how to mix them together.

  2. YAML is like a recipe for a computer. Instead of ingredients, the machine is told what to do by data and orders.

  3. YAML uses a style that is easy to read and understand by programmer. Like a regular recipe, it uses spaces and lines to split the different parts.

  4. The computer reads the YAML recipe and does what it says, just like you do when you make a cake by following the recipe.

  5. YAML is used to make sure that many different computer tools work right. It's like a special language that computers use to get things done and talk to each other.

Example of YAML?

Here is a very basic example of YAML which I think is really easy to understand -

1fruit:
2  - apple
3  - banana
4  - orange
5  - grape

Let's break down the above YAML code -

  1. What is the purpose of above example? The above-mentioned example of YAML tells you more about the list of fruits and in the list there are 4 items -

    • apple
    • banana
    • orange
    • grape
  2. Name of the list/Key of the list - In the code, the word "fruit" is called a key, and it tells the computer what the list is all about. The hyphens below the key represent the items in the list, just like a shopping list with different items.


Why Ansible needs YAML?

Ansible is a computer instructions that helps manage other computers by automating tasks, such as installing software or updating settings.

YAML is used in Ansible to create Ansible program(playbook), which are like recipes that tell Ansible what tasks to perform on each computer.

Here are some points to help explain why Ansible needs YAML:

  1. Ansible is like a robot helper that can do things on many computers at once.

  2. We need to make a script to tell Ansible what jobs to do. A plan tells Ansible what to do step by step, like a recipe.

  3. Ansible uses YAML to write these playbooks, just like we might use a special style to write down our cake recipe.

  4. YAML is a computer-friendly language that is easy for people to read and write. Because of this, it is a great tool for making playbooks.

Example of Ansible Playbook written in YAML

Here is a very basic example of Ansible playbook written in YAML -

1---
2- name: Install Apache
3  hosts: webserver
4  become: yes
5  tasks:
6    - name: Install Apache
7      apt:
8        name: apache2
9        state: present

Let's elaborate the above ansible example line by line -

  1. The first line (---) : is a special separator that tells Ansible that this is a YAML file. It's not required in all YAML files, but it's good practice to include it.

  2. The second line (- name: Install Apache) is a key-value pair that tells Ansible the name of the task we want to perform. In this case, we're installing Apache, which is a popular web server software.

  3. The third line (hosts: webserver) tells Ansible which computer to install Apache on. In this case, we're installing it on a computer named "webserver".

  4. The fourth line (become: yes) tells Ansible to run the task with administrator privileges. This is necessary for certain tasks, like installing software.

  5. The fifth line (tasks:) is a key that tells Ansible that we're going to define a list of tasks to perform.

  6. The sixth line (- name: Install Apache) is a key-value pair that describes the first task we want to perform. This task has the same name as the overall task we're performing (installing Apache).

  7. The seventh line (apt:) tells Ansible to use the "apt" module to install Apache. The "apt" module is used on Debian-based systems (like Ubuntu) to manage software packages.

  8. The eighth line (name: apache2) tells Ansible the name of the package we want to install. In this case, we want to install the "apache2" package, which is the name of the Apache web server package on Debian-based systems.

  9. The ninth line (state: present) tells Ansible that we want to make sure the package is installed and up-to-date. The "present" state means that Ansible will install the package if it's not already installed, or update it to the latest version if it is installed already.


What are rules for writing Ansible Playbook using YAMLs?

When you use YAML to write an Ansible script, there are a few important rules to remember. Here are the most important rules for writing YAML Ansible playbooks:

  1. Use uniform indentation : YAML uses indentation to organize its code, so it's important to use the same indentation throughout your plan. This will make it easy to read and understand your plan.

Here is an example of proper indentation -

 1---
 2# This playbook demonstrates the importance of indentation in YAML
 3- name: Indentation example
 4  hosts: all
 5  become: yes
 6  tasks:
 7    # This task is indented correctly
 8    - name: Task 1
 9      debug:
10        msg: "This task is indented correctly"
11    
12      # This task is indented incorrectly
13      - name: Task 2
14        debug:
15          msg: "This task is indented incorrectly"
  1. Use descriptive names for tasks and variables : Use names for tasks and variables that explain what they are for. When making tasks and variables in your plan, use names that explain what they are for. This will make it easy for other people to understand your plan and make changes as needed.

Here is an example of Tasks and Variables -

 1---
 2# This playbook demonstrates the use of descriptive names for tasks and variables
 3- name: Descriptive names example
 4  hosts: all
 5  become: yes
 6  vars:
 7    # This variable has a descriptive name
 8    package_name: apache2
 9
10  tasks:
11    # This task has a descriptive name and uses the variable
12    - name: Install Apache
13      apt:
14        name: "{{ package_name }}"
15        state: present
16      become: yes
17
18    # This task also has a descriptive name and uses the variable
19    - name: Start Apache
20      service:
21        name: "{{ package_name }}"
22        state: started
23 
  1. Separate different sections with hyphens : Use hyphens to separate different sections: Use hyphens to separate different jobs or plays in your script. This makes it easy to read and understand your plan.

Example -

 1---
 2# This playbook demonstrates the use of hyphens to separate different sections
 3- name: Separate sections example
 4  hosts: all
 5  become: yes
 6  vars:
 7    # This variable has a descriptive name
 8    package_name: apache2
 9
10  tasks:
11    # This task installs Apache
12    - name: Install Apache
13      apt:
14        name: "{{ package_name }}"
15        state: present
16      become: yes
17
18    # This task starts Apache
19    - name: Start Apache
20      service:
21        name: "{{ package_name }}"
22        state: started
23
24  handlers:
25    # This handler restarts Apache
26    - name: Restart Apache
27      service:
28        name: "{{ package_name }}"
29        state: restarted
30 
  1. Use colons to separate keys and values : Split keys and values with colons. In YAML, a colon is used to split keys and values. This makes it easier for people to read and understand your code.

Example -

 1# This playbook demonstrates the use of colons to separate keys and values
 2- name: Configure web server
 3  hosts: webservers
 4  become: yes
 5  tasks:
 6    # This task installs Apache and PHP
 7    - name: Install Apache and PHP
 8      apt:
 9        name:
10          - apache2  # Name of the Apache package to install
11          - php  # Name of the PHP package to install
12          - libapache2-mod-php  # Name of the Apache module required for PHP
13        state: present  # Ensure that the packages are present 
  1. Use YAML anchors and aliases to reduce duplication: Use YAML anchors and aliases to cut down on repetition. If you have similar parts in your playbook, you can use YAML anchors and aliases to cut down on duplication and make your playbook work better.

Example -

 1---
 2# This playbook demonstrates the use of YAML anchors and aliases
 3- name: Anchors and aliases example
 4  hosts: all
 5  become: yes
 6  vars:
 7    # Use an anchor to define the package list
 8    package_list: &package_list
 9      - apache2
10      - php
11      - libapache2-mod-php
12
13  tasks:
14    # Use an alias to reference the package list
15    - name: Install Apache and PHP
16      apt:
17        name: *package_list
18        state: present
19      become: yes
  1. Use quotes around strings : Use quotes around strings: It's best practice to use quotes around strings in your script. This helps avoid processing mistakes and makes your code easier to understand.

Example -

1---
2# Example of using quotes around a string with a colon
3- name: Configure web server
4  hosts: webservers
5  become: yes
6  vars:
7    # Use quotes around the string to ensure that the colon is parsed correctly
8    website_url: "https://www.example.com"
9 
  1. Use comments to explain your playbook: Use comments to explain your playbook: Comments are a great way to explain what your playbook does and why you made certain decisions. Use comments to help other people find your guide.

Example -

 1---
 2# This playbook configures a web server
 3# It installs Apache and PHP, copies a virtual host configuration file, and restarts Apache if necessary
 4
 5# Define the name of the playbook and the target hosts
 6- name: Configure web server
 7  hosts: webservers
 8
 9  # Use become to run the playbook with elevated privileges
10  become: yes
11
12  # Define variables used in the playbook
13  vars:
14    website_url: "https://www.example.com"
15    virtual_host_config_file: "/path/to/virtual/host.conf"
16
17  # Define the tasks to be performed
18  tasks:
19    # Install Apache and PHP packages
20    - name: Install Apache and PHP
21      apt:
22        name:
23          - apache2
24          - php
25          - libapache2-mod-php
26        state: present 
  1. YAML validation tools: Check your grammar with YAML inspection tools: YAML grammar can be hard to understand, so it's a good idea to use YAML validation tools to check your spelling and make sure your plan doesn't have any mistakes.

Here are some online YAML validator tools which you can use -

  1. YAML Validator

    YAML validator tool from JSON Formatter

  2. JSON Formatter

    YAML validator tool from JSON Formatter


Advantages of YAML in Ansible

After using Ansbile for almost 5 years, I can not count the disadvantages of Ansible. Here are my list of advantages which make Ansible stand out among other tools -

  1. Easy to Read and Write - YAML is made to be easy to read and write, which makes it a great choice for setup files and data structures. The YAML format is easy to understand because it can be read by humans.

  2. Concise Syntax - Because YAML is made to be short, it is easy to write and change. The short format of YAML makes it easy to create and maintain Ansible playbooks.

  3. Flexible Data Structures - YAML can handle many different types of data formats, such as lists, sets, and single-valued items. This makes it easy to show complicated data patterns in a way that is clear and easy to read.

  4. Easy to Validate - Validating YAML files is easy, which makes it easier to find mistakes and make sure that playbooks are written properly. Tools for validating YAML can quickly find grammar mistakes and give ideas for how to fix them. Here are some validator tools which you can use - YAML Validator, JSON Formatter

  5. Integration with Other Tools - Since YAML is a widely used language for serializing data, it is easy to use with other tools and systems. This makes it easy for different systems and tools to share setup files and data structures.


YAML vs Other Data Serialization Formats

There are many different ways to serialize data, like -

Each style has its own pros and cons, and the best format to use relies on how it will be used. In the case of Ansible, YAML is the best option because it is easy to use and understand.

People often think that YAML is easier to read and understand than other forms for serializing data. Also, YAML is made to be shorter, which makes it easy to write and change. On the other hand, XML and JSON are often thought to be more wordy and hard to read.

Posts in this Series