Work smartly with YAML

YAML is a heavily used markup language in this cloudy world. There are many tricks that can help us work smartly with YAML.

Let’s get an example:

---
project: The Great Project
version: 0.0.1
services:
  service1:
    description: Get information from DB
    attributes:
      location: /opt/webapp/service1
      resource:
        cpu: 1
        memory: 1024MiB
        iops: 1000
  service2:
    description: 
    attributes:
      location: /opt/webapp/service2
      resource:
        cpu: 1
        memory: 1024MiB
        iops: 1000

In the above YAML, we have two services with the same resource block. We can write these:

---
project: The Great Project
version: 0.0.1
services:
  service1:
    description: Get information from DB
    attributes:
      location: /opt/webapp
      resource: &defaultresource
        cpu: 1
        memory: 1024MiB
        iops: 1000
  service2:
    description: Get information from DB
    attributes:
      location: /opt/webapp
      resource: *defaultresource

It is a valid YAML. This way we can solve repeated YAML sections with Anchors and Aliases.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.