<p>YAML has two data elements that serve as building blocks for complex data structures: sequences and mappings.</p>

<h2>Sequence</h2>

<p>This is a sequence:</p>

<pre><code>
---
- Emperor
- Gentoo
- Little Blue
</code></pre>

<h2>Mapping</h2>

<p>
This is a mapping:
</p>

<pre><code>
---
Penguin: Emperor
</code></pre>

<p>
In this case, <strong>Penguin</strong> is a <em>key</em> and <strong>Emperor</strong> is a <em>value</em>. This is often called a &#34;key and value pair&#34;, but in YAML it&#39;s just called a mapping.
</p>

<h2>Combining data blocks</h2>

<p>
You can embed these data types into one another. Here is a mapping that has a sequence as its value:
</p>

<pre><code>
---
Penguin:
  - Emperor
  - Gentoo
  - Little Blue
</code></pre>

<p>
Here is a sequence of mappings:
</p>

<pre><code>
---
- Penguin: Emperor
- Penguin: Gentoo
- Penguin: Little Blue
</code></pre>

<h2>yamllint</h2>

<p>Use yamllint to detect errors in your YAML. To install:
</p>

<pre>
<code>
$ pip install yamllint
</code></pre>

<p>Run it:
</p>

<pre>
<code>
$ yamllint good.yaml
$ yamllint bad.yaml
bad.yaml
  1:1       warning  missing document start "---"  (document-start)
  4:14      error    no new line character at the end of file  (new-line-at-end-of-file)
</code></pre>

<h2>yaml2json</h2>

<p>
Sometimes it&#39;s useful to convert your YAML to JSON so you can view the data structure in a different way. There are probably dozens of YAML-to-JSON converters out there, but here&#39;s the one I use: <a href="https://gitlab.com/slackermedia/yaml2json.git" target="_blank">https://gitlab.com/slackermedia/yaml2json.git</a>
</p>

<p>
Run it:
</p>

<pre><code>
$ cat example.yaml
---
penguins:
  - Gentoo
  - Little Blue
  - Rockhopper
dragons:
  - black
  - white
  - red
$ ~/bin/yaml2json.py example.yaml
{"penguins": ["Gentoo", "Little Blue", "Rockhopper"], "dragons": ["black", "white", "red"]}
</code></pre>

<h2>YAML police</h2>

<p>There are no YAML police. As long as yamllint finds no errors, your YAML is valid and can be parsed by any one of the dozens of YAML libraries out there. However, these YAML libraries aren&#39;t magical, so you must understand the internal logic of your own YAML data. Keep that in mind when devising a scheme for the data you're recording.
</p>

<p>YAML is a great method for creating configuration files, or storing simple data structures, and it's essential for Ansible playbooks.</p>

<p>
Enjoy!
</p>

