Table of contents
1.
Introduction
2.
Defining YAML 
2.1.
Rule One: Indentation 
2.2.
Rule Two: Colons  
2.3.
Rule Three: Dashes 
3.
YAML: What is it used for?
3.1.
YAML in Ansible 
3.2.
YAML in Kubernetes
4.
Frequently Asked Questions
4.1.
How to create YAML Files?
4.2.
Does Ansible use YAML or JSON files?
4.3.
IS JSON and YAML identical or different?
4.4.
What are ansible Tasks? 
4.5.
What is a YAML file, and how does Ansible use it? 
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Brief About YAML

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

YAML is a digestible data serialisation language widely used to build configuration files with any computer language.

Introduction image

Designed for human interaction, YAML is a tight superset of JSON, another data serialisation language. But because it is a rigorous superset, it is able to do all of JSON's capabilities and more. One significant distinction between JSON and YAML is that JSON employs brackets and braces, whereas YAML truly understands what newlines and indentation represent.

Defining YAML 

There are only three straightforward guidelines to keep in mind when producing YAML for SLS files, despite the syntax initially appearing intimidating and terse. 

Defining YAML

Rule One: Indentation 

Relationships between data layers are represented in YAML using a fixed indentation style. Each level's indentation must include precisely two spaces, according to Salt. Avoid using tabs. 

Rule Two: Colons  

Dictionary objects in Python are just key-value pairs. Users of other languages may recognize this data type as hashes or associative arrays.

In YAML, dictionary keys are represented as strings with a following colon at the end. Following the colon and separated by a space, values are represented by either a string or a number:

myKey: myValue

 

In Python, the above maps to:

{"myKey": "myValue"}

 

An indentation can be used as an alternative to correlate a value with a key.

myKey:
  myValue


Note: Although the syntax above is acceptable for YAML, it is uncommon in SLS files since, most frequently, the value for a key is a list of values rather than a unique value.

In Python, the above maps to

{"myKey": "myValue"}


Dictionaries can be nested: 

first_level_key:
  second_level_key: value_of_second_level_key

 

And in python 

{"first_level_key": {"second_level_key": "value_in_second_level_key"}}

Rule Three: Dashes 

Using a single dash and a space, lists of items are represented. Due to their having the same level of indentation, multiple items are included in the same list.

- listValueOne
- listValueTwo
- listValueThree

 

A key-value pair's value may be a list. In Salt, this is quite typical:
 

my_dictionary:
  - listValueOne
  - listValueTwo
  - listValueThree

 

In Python, the above maps to

{"my_dictionary": ["listValueOne", "listValueTwo", "listValueThree"]}

YAML: What is it used for?

Constructing configuration files is one of YAML's most popular applications. Although they can usually be used interchangeably, configuration files should be written in YAML rather than JSON because it is easier to understand and more user-friendly.
YAML is utilized for Kubernetes resources and deployments in addition to Ansible. One advantage of using YAML is that YAML files may be put to source control, like Github, allowing for the tracking and auditing of modifications.

YAML in Ansible 

Ansible IT processes are orchestrated using playbooks. A playbook, which consists of one or more plays in a YAML file, is used to specify the ideal state of a system.

Each play can run more tasks, each of which calls an Ansible module. In Ansible, jobs that require automation are carried out using modules. Any language, including Ruby, Python, and bash, that can return JSON can be used to create Ansible modules.

Maps and lists can be found in an Ansible Playbook. Start a YAML list by naming the play and listing the tasks to create a playbook. Keep in mind that indentation does not necessarily indicate logical inheritance. Consider every line to be a YAML data type. Users of Ansible can automate repetitive tasks without learning a complex programming language using YAML templates.

YAML in Kubernetes

Kubernetes operates on a defined state and an actual state. The state of a cluster is represented by Kubernetes objects, which also specify the desired appearance of the workload. YAML files can create Kubernetes resources such as pods, objects, and deployments.

You must include specifications when creating a Kubernetes object to specify the desired state of the object. The thing can be made using the Kubernetes API. The object specifications will be included in the API request as JSON, but you'll typically give kubectl the necessary data in the form of a YAML file. When Kubectl makes the API request, it will convert the file for you into YAML.

Kubernetes ensures that an object always exists after it has been defined and created. The YAML or JSON files that developers or system administrators submit to the Kubernetes API determine the defined state. To examine the discrepancy between the newly defined shape and the cluster's actual state, Kubernetes employs a controller.


Check out this problem - Redundant Braces

Frequently Asked Questions

How to create YAML Files?

A YAML file can be created using a straightforward notepad tool or a popular IDES like Visual Studio, Sublime, or ATOM. A file with the suffix ".yaml" contains material like text or JSON. Although they are not common, some extensions can also use the.yml extension.

Does Ansible use YAML or JSON files?

By default, Ansible uses YAML to create playbooks. Since YAML is a human-readable data format that is simple to read and write, Ansible does not support the JSON format. The process is played and automated using the YAML code in Ansible playbooks.

IS JSON and YAML identical or different?

YAML and JSON are used as data format serialization languages to configure environment specifics. JSON is a superset of YAML, which has additional functionality.

What are ansible Tasks? 

One of Ansible's unit actions is the task. A configuration policy should be broken up into smaller files or coding segments. These blocks can be used to automate a process.

What is a YAML file, and how does Ansible use it? 

Like JSON or XML, YAML files are just formatted text files with a few rules. Because it is easier to read than other formats, Ansible uses this syntax for playbooks. 

Conclusion

In this article, we discussed the introduction of the YAML, the rules for defining the YAML, and why YAML is used.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; look at the Top 150 Interview Puzzles interview experiences, and interview bundle for placement preparations. Read our blogs on aptitudecompetitive programminginterview questionsIT certifications, and data structures and algorithms for the best practice.

Live masterclass