What is YAML?
YAML (YAML Ain’t Markup Language) is a human-readable data format used by Ansible, Kubernetes, Docker Compose, GitHub Actions, and many CI/CD tools.
name: Venkat
age: 25
city: ChennaiCore rules
- Spaces only — never tabs
- Key: value — space after the colon
- Indentation defines nesting — usually 2 spaces
# Correct
app:
name: nginx
port: 80
# Wrong — inconsistent indent / tabs
app:
name: nginxLists
packages:
- nginx
- curl
- gitDictionaries (maps)
server:
hostname: web-01
ip: 192.168.1.10Nested structures
web:
port: 80
users:
- alice
- bobList of objects
servers:
- name: web-01
ip: 10.0.1.10
- name: web-02
ip: 10.0.1.11Multiline strings
script: |
#!/bin/bash
apt update
apt install -y nginxData types
| Type | Example |
|---|---|
| String | name: nginx |
| Number | port: 80 |
| Boolean | become: true |
| List | pkgs: [git, curl] |
| Null | value: null |
YAML in Ansible
---
- name: Install Nginx
hosts: web
become: true
tasks:
- name: Install package
apt:
name: nginx
state: presentCommon errors
| Mistake | Fix |
|---|---|
| Tabs | Use spaces |
Missing space after : | key: value |
Bad indent under tasks: | Align list items consistently |
| Unquoted special chars | Quote strings when needed |
Practice
vim practice.yml
# add a small map + list, then:
python3 -c 'import yaml,sys; yaml.safe_load(open("practice.yml"))'If Ansible says “mapping values are not allowed here”, check indentation and the space after every :.