What is YAML?
YAML stands for YAMl Ain't Markup Language, which is a lightweight data structuring human-readable format. It is normally contrasted with JSON and XML but is superior in terms of simplicity and readability. YAML has extensive usage in DevOps, Kubernetes, Docker, and general application configurations.
Properties of YAML:
- Human-readable: YAML has indentation for the structure, thus making it easy to read in comparison to JSON or XML.
- Handles complex types: Lists, dictionaries, and scalars are representable using YAML.
- Used extensively in DevOps: YAML is a favorite option for Kubernetes, Ansible, and CI/CD pipeline configuration files.
- No need for quotes or brackets: Brackets and quotes are not needed in YAML, unlike in JSON, making it less complicated.
YAML vs YML: What's the Difference?
File Extension:
There is only one variation between YAML and YML, and that is the file extension. YAML files can be saved as .yaml or .yml. Both extensions work the same way and can be used interchangeably.
Why Are There Two Extensions?
In the beginning, when YAML was first released, there were some operating systems (particularly older Windows) with file extension length limitations. As YAML is a four-letter term, some programmers abbreviated it to.yml to meet the three-letter file extension requirement. But nowadays, operating systems and applications fully support.yaml.
Industry Preference:
Even though both the extensions are good,.yaml is the accepted extension according to official YAML documentation and industry standard practices. Several tools and frameworks have shifted their focus towards adopting.yaml as the default file extension.
Usage of YAML in Different Fields
1. Kubernetes Configurations
Kubernetes, a popular container orchestration platform, heavily relies on YAML files for defining deployments, services, and configurations.
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: nginx-container
image: nginx:latest
2. Docker Compose Files
Docker Compose uses YAML files to define multi-container applications.
version: '3'
services:
web:
image: nginx
ports:
- "80:80
3. CI/CD Pipelines
Continuous Integration and Continuous Deployment (CI/CD) tools like GitHub Actions and GitLab CI/CD use YAML for workflow definitions.
name: CI Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2
4. Ansible Playbooks
Ansible, a configuration management tool, relies on YAML files to define automation scripts.
- hosts: webservers
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
Best Practices for YAML Files
- Use .yaml instead of .yml for greater compatibility and consistency.
- Proper indentation as YAML is indentation-sensitive.
- Don't use tabs; use spaces instead.
- Validate YAML files using online tools or tools like yamllint.
- Use comments (#) to make it more readable and maintainable.
Conclusion
Although there is no practical difference between .yaml and .yml file extensions, .yaml is the new default standard. YAML's simplicity and readability have made it a common format for configuration files in various fields such as DevOps, Kubernetes, Docker, and CI/CD pipelines. Developers can keep YAML files readable, maintainable, and usable effectively by adopting best practices.