Introduction to YAML!

YAML Ain’t Markup Language! It is designed with a focus on human-readable formatting. The creators of YAML wanted it to be easily readable by humans. It is portable, easily extendable, and supports one-pass processing (means, the program can go through the file line by line, processing each line in the first pass!)

YAML is extensively used in AWS cloud-formations, Kubernetes, Ansible playbooks, and a lot of other places. Until recently, I preferred JSON over YAML whenever I had a choice, but it looks like that’s not something I can continue doing. I must befriend YAML.

Syntax

  • Indentation is everything. spaces, not tabs.
  • Colon ‘:’ represents key-value pair
  • Dash ‘-‘ represents lists
  • Use Unicode, UTF-32 to be JSON compatible
  • YAML is case sensitive
  • The files should have .yaml or .yml as the extension
  • Comments start with ‘#’. To comment multiple lines, use ‘#’ in front of each line or use ‘>’ .
  • Use three dashes ‘—‘ mark the beginning of a document

Example yaml files

Simple yaml

hostname: awesomehostname.com
# this is a comment 
location:
  state: Colorado # comment may be added here 
  city: Denver
  code: "abc123"
tiers:
  - web
  - app
  - data
comments: > 
  this is another
  way of adding 
  multi-line comments

Kubernetes deployment yaml file

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pv-claim
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      containers:
      - image: wordpress:4.8-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pv-claim

Ansible Playbook

---
# This playbook deploys the whole application stack in this site.

# Apply common configuration to all hosts
- hosts: all

  roles:
  - common

# Configure and deploy database servers.
- hosts: dbservers

  roles:
  - db

# Configure and deploy the web servers. Note that we include two roles
# here, the 'base-apache' role which simply sets up Apache, and 'web'
# which includes our example web application.

- hosts: webservers

  roles:
  - base-apache
  - web

# Configure and deploy the load balancer(s).
- hosts: lbservers

  roles:
  - haproxy

# Configure and deploy the Nagios monitoring node(s).
- hosts: monitoring

  roles:
  - base-apache
  - nagios

References

  • https://yaml.org/
  • https://www.tutorialspoint.com/yaml/index.htm
  • https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
, , , ,

Post navigation