Table of contents
1.
Introduction🧑‍🏫
2.
About Ansible
3.
About Ansible YAML Basics
4.
Key-Value Pair
5.
Representing List
6.
List Inside Dictionaries 
7.
Boolean
8.
Frequently Asked Questions
8.1.
Why does Ansible use YAML?
8.2.
What is YAML used for?
8.3.
What is an Ansible configuration file?
8.4.
Is it easy to learn YAML?
8.5.
Is coding necessary for Ansible?
9.
Conclusion
Last Updated: Mar 27, 2024

Ansible YAML Basics

Author Ayushi Goyal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction🧑‍🏫

Learning advances by starting with basics, so are you wondering what Ansible YAML basics will be? Here in this blog, we will discuss the key-value pair, how to represent lists of dictionaries, and lists inside dictionaries. Let’s get started..🏌️

introduction

Before discussing Ansible YAML basics, firstly, let us know about Ansible.   

About Ansible

Ansible is an open-source IT engine that automates the deployment of many IT tools, including intra-service orchestration, cloud provisioning, and application deployment. It has no agents or custom security infrastructures, making deployment simple.

about ansible

Ansible uses playbooks for describing automation jobs and YAML(Yet Another Markup Language), a simple language for humans to write, understand and read. YAML is a simple human-readable data serialization language used in configuration files, but it can be used in various other applications where data is stored. An IT infrastructure support person can also understand, read, and debug these playbooks as necessary.

Ansible is designed for multi-level deployment. Ansible changes IT infrastructure by describing how all the systems are interrelated rather than managing one system simultaneously. Because Ansible is entirely agentless, it connects your nodes using SSH(by default). However, Ansible allows you to use the option of using another connection method, such as Kerberos.

Now we will learn about Ansible YAML Basics. 

About Ansible YAML Basics

YAML can be understood as a human-readable data serialization language generally used for writing configuration files. It is abbreviated to highlight that it is a markup language, not a document, depending on who you ask. It stands for Yet Another Markup Language or YAML ain’t markup language. It can be used to store data.

about yaml

YAML is preferred over other commonly used data formats like XML or JSON because it is easy for people to read and write. Additionally, most programming languages include libraries that can be used with YAML.

YAML has one more small quirk: all the YAML files may optionally start with “....” and end with “....”. This indicates a document’s start and end and is considered the preferred YAML format.

learn more about yaml

That’s all about the introduction to YAML. Let us now learn about key-value pairs, which are an essential part of Ansible YAML basics.

Key-Value Pair

key value pair

To represent the data in the form of dictionaries, YAML uses a key-value pair. The syntax for the dictionary is = key: value (a space must follow the colon):

Example: A Student record

…#Optional YAML start syntax 
Ninja: 
  Name: Coding Ninja
  Roll Number: 21
  Contact: XXXXXXXXXX 
… #Optional YAML end syntax

 

This complete record can also be abbreviated in a single line as:

Ninja: {name: coding ninja, roll number: 21, contact: XXXXXXXXXX}

Representing List

Almost all YAML files for Ansible start with a list. The items in the list are all lists of key/value pairs, often called hashes or dictionaries. As a result, we must understand how to write lists and dictionaries in YAML. A list’s members are on the new line starting with a “-” (a dash and a space), with the same level of indentation:

…
# A list of courses on coding ninjas
course:
     - Data Structures and Algorithms
     - Machine Learning
     - MERN Stack
     - Competitive Programming
…
You can also try this code with Online C Compiler
Run Code

Like key-value pair, a list can also be abbreviated as 

Course: [‘Data Structures and Algorithms, ‘Machine Learning, ‘MERN Stack’, ‘Competitive Programming’]

List Inside Dictionaries 

Now that we know lists and dictionaries let us write List inside Dictionaries where the value of a key is the list.

list inside dictionaries

 

…  
Ninja: 
   Name: Coding Ninja 
   Roll Number: 21
   Contact: XXXXXXXXXX  
   Course:
        - Data Structures and Algorithms
        - Machine Learning
        - MERN Stack
       - Competitive Programming
… 
You can also try this code with Online C Compiler
Run Code

List of Dictionaries

We can also make a list of dictionaries by repeating the syntax of ‘course’ in the above code.

…  
Ninja: 
   Name: Coding Ninja 
   Roll Number: 21
   Contact: XXXXXXXXXX  
 
   Course:
        - Data Structures and Algorithms
        - Machine Learning
        - MERN Stack
        - Competitive Programming
   
   Test Series: 
        - Amazon
        - Microsoft
        - Google
        - Uber
…
You can also try this code with Online C Compiler
Run Code

Boolean

YAML also supports boolean values, i.e., True or False, which are not case sensitive.

boolean values 

…  
Ninja: 
   Name: Coding Ninja 
   Roll Number: 21
   Contact: XXXXXXXXXX 
   Qualified: True
…
You can also try this code with Online C Compiler
Run Code

Now it’s time for the questions. Let us now move to FAQs.

Frequently Asked Questions

Why does Ansible use YAML?

Ansible uses YAML as it is very easy for humans to understand, write, and read as compared to other data formats like JSON and XML. Every YAML file optionally starts with “...” and ends with “...”.

What is YAML used for?

YAML is a human-understandable data serialization language which is used for writing configuration files. YAML stands for YAML ain’t markup language (a recursive acronym) or yet another markup language.

What is an Ansible configuration file?

A file that governs the behavior of all interactions made by the control node, which is the brain and the heart. The default configuration file for Ansible is located in the /etc/ansible/ansible.cfg.

Is it easy to learn YAML?

YAML is an easy, expressive, data-oriented language that distinguishes itself from document markup languages. YAML Ain't a Markup Language (YAML), and as configuration formats go, it's easy on the eyes.

Is coding necessary for Ansible?

Ansible is an open-source, free application that is simple to set up and utilize. Playbooks from Ansible don't need any specialized coding knowledge. Without the necessity for configuration files, Ansible may be used to perform basic tasks like rebooting from the command line or ensuring that a service is operating.

Conclusion

In this article, we discussed the introduction to Ansible and Ansible YAML basics and some basic things like key-value pairs, lists representation, and lists inside dictionaries. You can also visit our other suggested articles to learn more about Ansible, apart from Ansible YAML basics, if you are eager to enhance your knowledge regarding Ansible.

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Also check out - Data Dictionary In Software Engineering

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass