Welcome to Day 74 of the #100DaysOfDevOps Challenge! Today we will see the Introduction of YAML Scripting Language
What is YAML?
YAML is one of the most popular data serialization languages. Its popularity stems from its simplicity, as well as the fact that it is human-readable and simple to understand.
In addition to being a powerful format for writing configuration files, it finds its uses in data persistence, internet messaging, cross-language data sharing, and many more places.
YAML is a lightweight, human-readable data serialization language. It is designed to make the format easy to read and write. So, anyone can understand like a non-technical person can read and write in YAML.
YAML stands for “YAML Ain’t Markup Language”.
It is like XML and JSON but has easy and mini syntax.
YAML files are created with “.yaml” or “.yml” extensions. We can use any IDE (Integrated Development Environment) or text editor to open/create YAML files.
YAML is a superset of JSON.
It is quite easy, and we can represent the complex mapping in a straightforward way. Due to this nowadays YAML is used in configuration settings.
What Does A Regular YAML File Look Like?
---
# A sample yaml file
company: SGTechnologies
domain:
- devops
- devsecops
tutorial:
- yaml:
name: "YAML Ain't Markup Language"
type: awesome
born: 2001
- json:
name: JavaScript Object Notation
type: great
born: 2001
- xml:
name: Extensible Markup Language
type: good
born: 1996
author: Gokul
published: true
Where is YAML Used?
Let us see it in action with some of the most famous tools used YAML Scripting.
Ansible
Ansible playbooks are used to automate repeated tasks that execute actions automatically.
Playbooks are expressed in YAML format and perform any action defined in plays.
Here is a simple Ansible playbook that installs Nginx, applies the specified template to replace the existing default Nginx landing page, and finally enables TCP access on port 80.
---
- hosts: all
become: yes
vars:
page_title: Spacelift
page_description: Spacelift is a sophisticated CI/CD platform for Terraform, CloudFormation, Pulumi, and Kubernetes.
tasks:
- name: Install Nginx
apt:
name: nginx
state: latest
- name: Apply Page Template
template:
src: files/spacelift-intro.j2
dest: /var/www/html/index.nginx-debian.html
- name: Allow all access to tcp port 80
ufw:
rule: allow
port: '80'
proto: tcp
Kubernetes
Kubernetes, also known as K8s, is an open-source system for automating the deployment, scaling, and management of containerized applications.
Kubernetes works based on a state model where it tries to reach the desired state from the current state in a declarative way. Kubernetes uses YAML files to define the Kubernetes object, which is applied to the cluster to create resources like pods, services, and deployments.
Here is a YAML file that describes a deployment that runs Nginx.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Security-
YAML is a User-Friendly Data Formatting Language. YAML does not contain any executable commands, which makes YAML highly secure when exchanging files with third parties.
If we require an executable command, then we need to integrate the YAML with other languages.
Conclusion:
YAML is a human-readable data-serialization language. We covered its commonly used features like Scalers, Sequences, Dictionaries, Complex keys, Anchors, and aliases. Due to its powerful language feature, YAML is used in Infrastructure as Code to write configuration files and data transfer. Docker uses YAML to create Dockerfile. In Kubernetes, we can create storage and virtual machines using YAML. Currently, YAML is widely used for pipeline development in DevOps.