What is YAML?
YAML stands for (yet another markup language) and is a data serialization language that is often used for writing configuration files. YAML is a popular programming language because it is human-readable and easy to understand. It can also be used in conjunction with other programming languages. Because of its flexibility and accessibility, YAML is used by the Ansible automation tool to create automation processes, in the form of Ansible Playbooks.
YAML syntax
YAML has features that come from Perl, C, XML, HTML, and other programming languages. YAML is also a superset of JSON, so JSON files are valid in YAML.
YAML uses Python-style indentation to indicate nesting. Tab characters are not allowed, so whitespaces are used instead. There are no usual format symbols, such as braces, square brackets, closing tags, or quotation marks. YAML files use a .yml or .yaml extension.
The structure of a YAML file:
- map
- list.
Maps
Maps allow you to associate key-value pairs. Each key must be unique, and the order doesn’t matter. Think of a Python dictionary or a variable assignment in a Bash script. A map in YAML needs to be resolved before it can be closed, and a new map is created. A new map can be created by either increasing the indentation level or by resolving the previous map and starting an adjacent map.
List
A list includes values listed in a specific order and may contain any number of items needed. A list sequence starts with a dash (-) and a space, while indentation separates it from the parent. You can think of a sequence as a Python list or an array in Bash or Perl. A list can be embedded into a map.
YAML scalars
YAML also contains scalars, which are arbitrary data (encoded in Unicode) that can be used as values such as strings, integers, dates, numbers, or booleans.
YAML file syntax
When creating a YAML file, you’ll need to ensure that you follow these syntax rules and that your file is valid. A linter is an application that verifies the syntax of a file. The yamllint command can help to ensure you’ve created a valid YAML file before you hand it over to an application.
YAML syntax example
In the following you see a simple YAML file for an employee record :
# An employee record
name: James Oliver
job: Developer
skill: Professionell
employed: True
foods:
- meat
- Cheese
languages:
csharp: Professionell
python: Elite
javascript: Lame
education: |
BSc in mathematics
3 A-Levels
MS in computer science
What is YAML used for?
One of the most common uses for YAML is to create configuration files. It’s recommended that configuration files be written in YAML rather than JSON, even though they can be used interchangeably in most cases, because YAML has better readability and is more user-friendly.
In addition to its use in Ansible, YAML is used for Kubernetes resources and deployments.
A benefit of using YAML is that YAML files can be added to source control, such as Github, so that changes can be tracked and audited.
YAML in Ansible
Ansible Playbooks are used to orchestrate IT processes. A playbook is a YAML file containing 1 or more plays, and is used to define the desired state of a system.
Each play can run one or more tasks, and each task invokes an Ansible module. Modules are used to accomplish automation tasks in Ansible. Ansible modules can be written in any language that can return JSON, such as Ruby, Python, or bash.
An Ansible Playbook consists of maps and lists. To create a playbook, start a YAML list that names the play, and then lists tasks in a sequence. Remember that indentation is not an indication of logical inheritance. Think of each line as a YAML data type (a list or a map).
By using YAML templates, Ansible users can program repetitive tasks to happen automatically without having to learn an advanced programming language.
For more about Ansible look to the Basics in Ansible
YAML for Kubernetes
Kubernetes works based on defined state and actual state. Kubernetes objects represent the state of a cluster and tell Kubernetes what you want the workload to look like. Kubernetes resources, such as pods, objects, and deployments can be created using YAML files.
When creating a Kubernetes object, you’ll need to include specifications to define the object’s desired state. The Kubernetes API can be used to create the object. The request to the API will include the object specifications in JSON, but most often you’ll provide the required information to kubectl as a YAML file. Kubectl will convert the file into YAML for you when it makes the API request.
Once an object has been created and defined, Kubernetes works to make sure that the object always exists.
Developers or sysadmins specify the defined state using the YAML or JSON files they submit to the Kubernetes API. Kubernetes uses a controller to analyze the difference between the new defined state and the actual state in the cluster.
For more about YAML detail look to the YAML for beginner
YAML for Pipelines
In Azure DevOps, YAML Pipelines helps to setup Continuous Integration/ Continuous Deployment and to achieve this we have below two option
- Classic Editor
- YAML Pipeline.
In this Document we will discuss setup YAML Pipeline.
YAML Pipeline Structure is shown in the bellow:
Hierarchy of YAML File:
Component of YAML Pipeline
1- Stages
Stage is collection of Jobs which runs sequentially.
stages:
- stage: Build
jobs:
- job: BuildOnWindows
steps:
- Build
- job: BuildOnMac
steps:
- Build
2- Jobs
Job is Collection of Steps that runs on agents/environment.
jobs:
- job: BuildPackage
steps:
- Build
- Package
- Publish
3- Steps
Steps helps to define the set of process to setup your task or any activity which you want to perform on any specific job.
Kind of Steps:
- Task
- Script
- templatereference
steps:
- Build
- Package
- Publish
Schema of YAML Pipeline file
name: string # Define Custom Build number/Name
variables: #Define variable for Pipeline
trigger: trigger/none #it defines which branch to enable for CI
stages:[stage| templateReference ]
jobs:[job| templateReference ]
steps:[script | task | templateReference ]
More about YAML Pipeline
Conclusion
We have discussed about YAML and what is used YAML for, kind of YAML, such as YAML for Kubernetes and YAM for pipeline,