Three ways to YAML

Three ways to YAML

YAML, while very easy to read and extensible, can cause issues with your OpenStack deployment that aren’t readily apparent by simply looking at log output. Issues can arise when your yaml is not syntactically correct, especially where whitespace is concerned.

In the following examples, we’ll be using a typical storage-environment.yaml file, a heat template that gives us options to set up Ceph in Red Hat OpenStack. In this example template, we have Ceph enabled for RHOSP, along with disks /dev/sda to /dev/sdo. We’ll use three SSD drives to in a shared journal configuration, disks /dev/sda,b,c.

resource_registry:
  OS::TripleO::NodeUserData: /home/stack/templates/wipe-disks.yaml
parameter_defaults:
  CinderEnableIscsiBackend: false
  CinderEnableRbdBackend: true
  CinderEnableNfsBackend: false
  NovaEnableRbdBackend: true
  GlanceBackend: rbd

  ExtraConfig:
    ceph::profile::params::osds:
        '/dev/sdd':
          journal: '/dev/sda'
        '/dev/sde':
          journal: '/dev/sda'
        '/dev/sdf':
          journal: '/dev/sda'
        '/dev/sdg':
          journal: '/dev/sda'
        '/dev/sdh':
          journal: '/dev/sdb'
        '/dev/sdi':
          journal: '/dev/sdb'
        '/dev/sdj':
          journal: '/dev/sdb'
        '/dev/sdk':
          journal: '/dev/sdb'
        '/dev/sdl':
          journal: '/dev/sdc'
        '/dev/sdm':
          journal: '/dev/sdc'
        '/dev/sdn':
          journal: '/dev/sdc'
        '/dev/sdo':
          journal: '/dev/sdc'

1. Syntax Highlight

One of the easiest ways to see what you’re doing with whitespace in yaml is with vim. Vim has a very powerful built-in syntax highlighter, that will give you everything you need to see how your changes look.

First, let’s set vim up to work with yaml:

1. turn syntax highlighting on with the following command

:syntax on

2. tell vim that whitespace is your syntax

:set syntax=whitespace

2__vim_storage-environment_yaml__vim_

2. Validate

In other markup languages, validation is usually performed to be sure you’re syntactically correct. One of the most popular versions of this workflow is xmllint, for XML. There are a number of validators for yaml as well.

1. The upstream tripleo project, from which Red Hat OpenStack is derived, contains a convenient yaml validator here:

https://raw.githubusercontent.com/openstack/tripleo-heat-templates/master/tools/yaml-validate.py

Running this on our example file gives the following output:

$ curl -o https://raw.githubusercontent.com/openstack/tripleo-heat-templates/master/tools/yaml-validate.py
$ chmod 755 yaml-validate.py
$ ./yaml-validate.py ~/storage-environment.yaml
Validating /Users/ken/storage-environment.yaml
Validation successful!

2. There are many online validators as well. One of the best can be found at http://codebeautify.org/

3. Convert

Lastly, we look at a way to convert yaml to different types of output. It may be useful, for instance, to convert your yaml to json — in order to gain better insight to the output of your changes.

This can be achieved easily with python.

1. python example to convert yaml to json:

python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < file.yaml

Output of our example would look like this:

$ python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < ~/storage-environment.yaml
{
    "parameter_defaults": {
        "CinderEnableIscsiBackend": false,
        "CinderEnableNfsBackend": false,
        "NovaEnableRbdBackend": true,
        "GlanceBackend": "rbd",
        "ExtraConfig": {
            "ceph::profile::params::osds": {
                "/dev/sdn": {
                    "journal": "/dev/sdc"
                },
                "/dev/sdo": {
                    "journal": "/dev/sdc"
                },
                "/dev/sdl": {
                    "journal": "/dev/sdc"
                },
                "/dev/sdm": {
                    "journal": "/dev/sdc"
                },
                "/dev/sdj": {
                    "journal": "/dev/sdb"
                },
                "/dev/sdk": {
                    "journal": "/dev/sdb"
                },
                "/dev/sdh": {
                    "journal": "/dev/sdb"
                },
                "/dev/sdi": {
                    "journal": "/dev/sdb"
                },
                "/dev/sdf": {
                    "journal": "/dev/sda"
                },
                "/dev/sdg": {
                    "journal": "/dev/sda"
                },
                "/dev/sdd": {
                    "journal": "/dev/sda"
                },
                "/dev/sde": {
                    "journal": "/dev/sda"
                }
            }
        },
        "CinderEnableRbdBackend": true
    },
    "resource_registry": {
        "OS::TripleO::NodeUserData": "/home/stack/templates/wipe-disks.yaml"
    }
}

Leave a Reply

%d bloggers like this: