Fortinet SDWAN Demo
Fortinet SDWAN Demo
Expert-Led Live Training | 19th April 2025 @10 AM IST
Day
Hr
Min
Sec
Join Now
USD ($)
$
United States Dollar
India Rupee

Understanding Ansible YAML Basics

Created by Deepak Sharma in Articles 17 Mar 2025
Share
«How to Configure a Banner in Cisco ...

Ansible is one of the most popular automation tools used for configuration management, application deployment, and task automation. Its simplicity and power stem from its use of YAML (YAML Ain’t Markup Language), a human-readable data serialization format.

YAML is the backbone of Ansible playbooks, inventories, and variable files, making it essential for anyone working with Ansible to understand its syntax and structure. 

In this article, we’ll dive deep into the basics of YAML and how it is used in Ansible. We’ll cover YAML syntax, provide examples of Ansible YAML files, and explore various YAML structures such as lists, dictionaries, and nested data.

By the end of this article, you’ll have a solid understanding of how to write and interpret YAML for Ansible automation. Enrolling in Automation courses may help you learn YAML faster and gain job-ready skills.

What is YAML? 

YAML is a data serialization language designed to be both human-readable and machine-parsable. It is often used for configuration files and data exchange between systems.

YAML’s simplicity and readability make it an ideal choice for Ansible, where playbooks and configurations need to be easily understood and maintained. 

Key Features of YAML 

The following are the key features of YAML: 

● YAML uses indentation and simple syntax, making it easy to read and write. 

● YAML supports complex data structures like lists, dictionaries, and nested objects. 

● Unlike JSON or XML, YAML relies on indentation and colons to define structure. 

● YAML supports comments, which start with a # symbol. 

● YAML is case-sensitive, so key and Key are treated as different entities. 


Cisco DevNet Associate Certification TrainingJoin the online training for Cisco DevNet Certification.Explore course
custom banner static image

YAML Syntax Basics 

Before diving into Ansible-specific examples, let’s review the basic syntax of YAML. 

1. Key-Value Pairs 

YAML uses key-value pairs to represent data. The key and value are separated by a colon (:). 

name: Alice 

age: 30 

2. Indentation 

Indentation is used to define the hierarchy of data. Spaces (not tabs) are used for indentation. 

person: 

  name: Alice 

  age: 30 

3. Lists 

Lists are represented with a hyphen (-) followed by a space. 

fruits: 

  - apple 

  - banana 

  - cherry

4. Dictionaries 

Dictionaries are collections of key-value pairs. 

person: 

  name: Alice 

  age: 30 

  hobbies: 

    - reading 

    - hiking 

5. Strings 

Strings can be written without quotes unless they contain special characters. 

message: Hello, World! 

Ansible YAML File Example 

Ansible playbooks are written in YAML and typically start with three dashes (---). Let’s look at a simple example of an Ansible playbook: 

--- 

- name: Install and start Apache 

  hosts: webservers 

  become: yes 

  tasks: 

    - name: Ensure Apache is installed 

      apt: 

        name: apache2 

        state: present 

    - name: Ensure Apache is running 

      service: 

        name: apache2 

        state: started 

Breakdown of the Playbook 

1. name: Describes the purpose of the playbook. 

2. hosts: Specifies the target hosts (e.g., webservers). 

3. become: yes: Indicates that the tasks will be executed with elevated privileges (e.g., using sudo). 

4. tasks: A list of tasks to be executed. Each task is called an Ansible module. 

Ansible YAML Modules 

Ansible modules are reusable units of code that perform specific tasks. In the example above, apt and service are modules. Modules are invoked in playbooks using key-value pairs, where the key is the module name and the value is the module's parameters. 

Example of Modules 

The following are some of the examples of such modules: 

● apt: Manages packages on Debian-based systems. 

● service: Manages services on the target system. 

● copy: Copies files from the control machine to the target machine. 

● command: Executes commands on the target machine. 

YAML List of Objects 

A YAML list of objects is a collection of items, where each item is an object (a set of key-value pairs). This structure is commonly used in Ansible to define multiple tasks or hosts. 

Example 

The following are some of the examples of such objects: 

- name: Alice 

  age: 30 

- name: Bob 

  age: 25 

This represents a list of two objects, each with name and age keys. 

YAML List of Dictionaries 

A YAML list of dictionaries is like a list of objects. Each item in the list is a dictionary (key-value pairs). This structure is useful for defining complex data. 

Example 

The following are some of the examples of that list of dictionaries: 

- person: 

    name: Alice 

    age: 30 

- person: 

    name: Bob 

    age: 25 

Here, each item in the list is a dictionary under the person key. 

YAML List of Lists 

A YAML list of lists is a nested structure where each item in the main list is itself a list. This structure is useful for representing multi-dimensional data. 

Example 

The following are some of the examples of those lists: 

  - apple 

  - banana 

  - cherry 

  - dog 

  - cat 

  - bird 

This represents two lists: one of fruits and one of animals. 

YAML List of Strings 

A YAML list of strings is a simple list where each item is a string. This structure is commonly used for defining variables or parameters. 

Example 

The following are some of the examples of these list of strings: 

- apple 

- banana 

- cherry 

This represents a list of three fruits. 

YAML Dictionary Example 

A YAML dictionary is a collection of key-value pairs. Dictionaries are used to represent structured data in Ansible, such as variables or task parameters. 

Example 

The following are some of the examples of such dictionaries: 

person: 

  name: Alice 

  age: 30 

  hobbies: 

    - reading 

    - hiking

 In this example: 

● person is the top-level key. 

● name, age, and hobbies are nested keys. 

● hobbies is a list of strings. 

Advanced YAML Structures in Ansible 

Ansible often uses advanced YAML structures to define complex configurations. Let’s explore some of these structures. 

1. Nested Dictionaries 

Nested dictionaries are used to represent hierarchical data. 

webserver: 

  apache: 

    port: 80 

    docroot: /var/www/html 

  nginx: 

    port: 8080 

    docroot: /usr/share/nginx/html 

2. Combining Lists and Dictionaries 

Lists and dictionaries can be combined to represent complex data. 

servers: 

  - name: server1 

    ip: 192.168.1.1 

    services: 

      - apache 

      - mysql 

  - name: server2 

    ip: 192.168.1.2 

    services: 

      - nginx 

      - postgresql

3. Multi-Line Strings 

YAML supports multi-line strings using the | or > symbols. 

description: | 

  This is a multi-line string. 

  It preserves line breaks.

Best Practices for Writing YAML in Ansible 

The following are the best practices to be followed while writing YAML in Ansible : 

● Always use spaces (not tabs) for indentation. The standard is two spaces per level. 

● Avoid overly complex structures unless necessary. 

● Add comments to explain complex sections of your playbook. 

● Use tools like yamllint to validate your YAML files. 

● Always test your playbooks in a safe environment before deploying them to production. 

Conclusion 

YAML is the foundation of Ansible automation, and understanding its syntax and structure is crucial for creating effective playbooks and configurations. From simple key-value pairs to complex nested structures, YAML provides the flexibility and readability needed for automation tasks. 

By mastering YAML, you’ll be able to write clean, efficient, and maintainable Ansible playbooks. Whether you’re defining a list of objects, a dictionary, or a multi-dimensional structure, YAML’s simplicity and clarity make it an ideal choice for Ansible configurations. 

Start experimenting with YAML in your Ansible playbooks today, and you’ll quickly appreciate its power and flexibility. Happy automating! 

REST-Based APIs: Definition & Examples»
Deepak Sharma

He is a senior solution network architect and currently working with one of the largest financial company. He has an impressive academic and training background. He has completed his B.Tech and MBA, which makes him both technically and managerial proficient. He has also completed more than 450 online and offline training courses, both in India and ...

More... | Author`s Bog | Book a Meeting

Related Articles

#Explore latest news and articles

How to Learn Network Automation as a Fresher 6 Mar 2025

How to Learn Network Automation as a Fresher

Effective ways on how to learn network automation using Python for network automation engineers and start a journey to master this essential skill.
Comparing Artificial Intelligence and Machine Learning 19 Mar 2025

Comparing Artificial Intelligence and Machine Learning

Explore the key differences between Artificial Intelligence (AI) and Machine Learning (ML) in our article. Understand their definitions, examples, and ...
Why is Network Automation Important? 12 Nov 2024

Why is Network Automation Important?

Discover the crucial role of network automation for network engineers. Learn why automation is essential for efficiency and competition in the networking field.

FAQ

YAML is used for configuration files, data serialization, API definitions, metadata storage, and structured data exchange between programming languages.
The basic structure of a YAML file consists of key-value pairs, lists (using hyphens), and nested data structures represented through indentation.
The '<<' symbol in YAML is used for merging one mapping into another, allowing for the reuse of common configurations.
YAML and JSON have different strengths. YAML is more human-readable and versatile, while JSON is more compact and widely supported for data interchange.
Yes, YAML is used for API documentation and definitions, particularly with specifications like OpenAPI and AsyncAPI.

Comments (0)

Deepak Sharma

Deepak Sharma

Senior Instructor (Part Time) at UniNets Instructor role
★★★★★ 4.96
Faithful User
Expert Vendor
Golden Classes
King Seller
Fantastic Support
Loyal Writer
+91 8383 96 16 46

Enquire Now

Captcha
Share to your friends

Share

Share this post with others

Contact learning advisor

Captcha image