YAML: Difference between revisions

From Omnia
Jump to navigation Jump to search
Line 62: Line 62:
== Dictionary ==
== Dictionary ==


Python note: This is equivalent to Python Dictionary: martin = {'name': 'Martin', 'job': 'Dev': 'skill': 'Elite'}
YAML Dictionary:
 
Dictionary:
<pre>
<pre>
# An employee record
# An employee record
Line 73: Line 71:
</pre>
</pre>


Compact Abbreviated list / dictonary:
Compact YAML Dictonary:
<pre>
<pre>
martin: {name: Martin D'vloper, job: Developer, skill: Elite}
martin: {name: Martin D'vloper, job: Developer, skill: Elite}
fruits: ['Apple', 'Orange', 'Strawberry', 'Mango']
</pre>
 
---
 
JSON for Comparision:
<pre>
{
  "martin": {
    "name": "Martin D'vloper",
    "job": "Developer",
    "skill": "Elite"
  }
}
</pre>
 
Python Dictionary for Comparision:
<pre>
martin = {
    "name": "Martin D'vloper",
    "job": "Developer",
    "skill": "Elite"
}
</pre>
</pre>



Revision as of 23:24, 25 February 2024

YAML

YAML Syntax — Ansible Documentation - https://docs.ansible.com/ansible/2.5/reference_appendices/YAMLSyntax.html#yaml-syntax

Comment

# this is a comment

---

All YAML files (regardless of their association with Ansible or not) can optionally begin with --- and end with ... This is part of the YAML format and indicates the start and end of a document.

# file test.yaml - yamllint doesn't care if there is a comment before ---
---
test:
  name: big test
# yamllint will check for --- by default
# yamllint likes an empty new line at end of file, ??most of the time??, even after ...
# yamllint will NOT check for ... by default
...

List

All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space):

YAML List:

# A list of tasty fruits
fruits:
    - Apple
    - Orange
    - Strawberry
    - Mango

Compact YAML List:

fruits: [Apple, Orange, Strawberry, Mango]

---

JSON for comparision:

{
  "fruits": [
    "Apple",
    "Orange",
    "Strawberry",
    "Mango"
  ]
}

Python list for comparision:

fruits = ['Apple', 'Orange', etc]

Dictionary

YAML Dictionary:

# An employee record
martin:
    name: Martin D'vloper
    job: Developer
    skill: Elite

Compact YAML Dictonary:

martin: {name: Martin D'vloper, job: Developer, skill: Elite}

---

JSON for Comparision:

{
  "martin": {
    "name": "Martin D'vloper",
    "job": "Developer",
    "skill": "Elite"
  }
}

Python Dictionary for Comparision:

martin = {
    "name": "Martin D'vloper",
    "job": "Developer",
    "skill": "Elite"
}

List of Dictionaries

employees:
    - people:
        - name: joe
          job: manager
        - name: mike
          job: tech
    - skills:


JSON

Boolean Values

create_key: yes
needs_agent: no
knows_oop: True
likes_emacs: TRUE
uses_cvs: false

Block Text

Literal Block Scalar: (indentation ignored)

include_newlines: |
            exactly as you see
            will appear these three
            lines of poetry

Literal Block Scalar - folded to one line: (indentation ignored)

fold_newlines: >
            this is really a
            single line of text
            despite appearances

Injected Variables - NOT YAML

This is not a YAML standard, but is a common theme used by things applications like Ansible.

Injected Variables:

foo: "{{ variable }}"
foo: {{ variable }}

yamllint

See yamllint

Install:

apt install yamllint

Use:

yamllint file.yaml

Note:

  • Defaults to max lines 80 limit

Doc:

https://yamllint.readthedocs.io/en/stable/

keywords