skip to content

Search

Syspirit
EN

YAML

YAML syntax for Docker Compose, Ansible, Kubernetes and CI/CD!

Languages
Published on

YAML (YAML Ain’t Markup Language) is a human-readable data serialization language, widely used for configuration files.

📝 Basic Syntax

📌 Type🧠 Syntax
📄 Stringname: "John Doe" or name: John Doe
🔢 Numberage: 25 or price: 19.99
✅ Booleanactive: true or active: false
🚫 Null valuevalue: null
💬 Comment# This is a comment

📋 Lists and Objects

📌 Action🧠 Syntax
📄 Simple list- item1
- item2
📋 Inline listports: [80, 443, 8080]
🏗️ Simple objectserver:
name: web-01
port: 80
📊 List of objectsservices:
- name: nginx
port: 80

📄 Multiline Strings

📌 Action🧠 Syntax
📝 Preserve line breaksscript: |
echo "line 1"
echo "line 2"
🔄 Single linedescription: >
Long text folded
into one line

🔥 Practical Examples

🐳 Docker Compose

services:
  web:
    image: nginx:alpine
    ports:
      - "80:80"
    environment:
      - ENV=production
 
  db:
    image: postgres:15
    environment:
      POSTGRES_DB: myapp
      POSTGRES_PASSWORD: password

🤖 Ansible

---
- name: Install nginx
  hosts: webservers
  become: yes
 
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present
 
    - name: Start nginx
      service:
        name: nginx
        state: started

🔄 GitHub Actions

name: CI Pipeline
 
on:
  push:
    branches: [main]
 
jobs:
  build:
    runs-on: ubuntu-latest
 
    steps:
    - uses: actions/checkout@v3
 
    - name: Run tests
      run: npm test
 
    - name: Build
      run: npm run build

⚠️ Important Rules


🛠️ Useful Tools

📌 Tool🧠 Usage
🔍 yamllintyamllint docker-compose.yml
🔄 yqyq '.services.web.image' docker-compose.yml
📝 EditorsVSCode, Vim with YAML plugins
🌐 Online validatorsyamllint.com, yamlchecker.com

📋 YAML Checklist

  • Indentation: 2 spaces, no tabs
  • Structure: logical and consistent
  • Validation: test before deployment
  • Comments: explain complex parts