YAML: Difference between revisions
(→YAML) |
|||
(12 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
YAML Syntax — Ansible Documentation - https://docs.ansible.com/ansible/2.5/reference_appendices/YAMLSyntax.html#yaml-syntax | YAML Syntax — Ansible Documentation - https://docs.ansible.com/ansible/2.5/reference_appendices/YAMLSyntax.html#yaml-syntax | ||
Comment | == Comment == | ||
<pre> | <pre> | ||
# this is a comment | # this is a comment | ||
</pre> | </pre> | ||
== --- == | |||
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. | 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. | ||
Line 20: | Line 23: | ||
</pre> | </pre> | ||
== Multiple Documents == | |||
You can combine multiple documents by seperating them with "---": | |||
<pre> | |||
document: this is document 1 | |||
--- | |||
document: this is document 2 | |||
</pre> | |||
== List == | |||
All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space): | All members of a list are lines beginning at the same indentation level starting with a "- " (a dash and a space): | ||
YAML List: | |||
<pre> | <pre> | ||
# A list of tasty fruits | # A list of tasty fruits | ||
fruits: | fruits: | ||
Line 30: | Line 46: | ||
- Strawberry | - Strawberry | ||
- Mango | - Mango | ||
</pre> | </pre> | ||
Dictionary: | Compact YAML List: | ||
<pre> | |||
fruits: [Apple, Orange, Strawberry, Mango] | |||
</pre> | |||
--- | |||
JSON for comparision: | |||
<pre> | |||
{ | |||
"fruits": [ | |||
"Apple", | |||
"Orange", | |||
"Strawberry", | |||
"Mango" | |||
] | |||
} | |||
</pre> | |||
Python list for comparision: | |||
fruits = ['Apple', 'Orange', etc] | |||
== Dictionary == | |||
YAML Dictionary: | |||
<pre> | <pre> | ||
# An employee record | # An employee record | ||
Line 42: | Line 81: | ||
</pre> | </pre> | ||
Compact YAML Dictonary: | |||
<pre> | <pre> | ||
martin: {name: Martin D'vloper, job: Developer, skill: Elite} | martin: {name: Martin D'vloper, job: Developer, skill: Elite} | ||
</pre> | </pre> | ||
Boolean | --- | ||
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> | |||
== List of Dictionaries == | |||
"Collection" is a list of dictionaries, with a nested list of dictionaries of "people", intermixed with a nested list of "skills", just to make it interseting. | |||
<pre> | |||
collection: | |||
- people: | |||
- name: ken | |||
job: grunt | |||
- name: mike | |||
job: manager | |||
- skills: | |||
- computers | |||
- science | |||
</pre> | |||
JSON for comparison: | |||
<pre> | |||
{ | |||
"collection": [ | |||
{ | |||
"people": [ | |||
{ | |||
"name": "ken", | |||
"job": "grunt" | |||
}, | |||
{ | |||
"name": "mike", | |||
"job": "manager" | |||
} | |||
] | |||
}, | |||
{ | |||
"skills": [ | |||
"computers", | |||
"science" | |||
] | |||
} | |||
] | |||
} | |||
</pre> | |||
== Boolean Values == | |||
<pre> | <pre> | ||
create_key: yes | create_key: yes | ||
Line 56: | Line 158: | ||
uses_cvs: false | uses_cvs: false | ||
</pre> | </pre> | ||
== Block Text == | |||
=== Multi-Line === | |||
Literal Block Scalar: (indentation ignored) | Literal Block Scalar: (indentation ignored) | ||
Line 63: | Line 169: | ||
will appear these three | will appear these three | ||
lines of poetry | lines of poetry | ||
</pre> | |||
Literal Block Scalar - folded to one line: (indentation ignored) | |||
<pre> | |||
fold_newlines: > | fold_newlines: > | ||
this is really a | this is really a | ||
Line 70: | Line 179: | ||
</pre> | </pre> | ||
Variables: | == Injected Variables - NOT YAML == | ||
This is not a YAML standard, but is a common theme used by things applications like Ansible. | |||
Injected Variables: | |||
<pre> | <pre> | ||
foo: "{{ variable }}" | foo: "{{ variable }}" | ||
foo: {{ variable }} | |||
</pre> | </pre> | ||
Line 90: | Line 204: | ||
Doc: | Doc: | ||
https://yamllint.readthedocs.io/en/stable/ | https://yamllint.readthedocs.io/en/stable/ | ||
== YAML to JSON == | |||
See [[yq]] | |||
yq eval file.yaml -o json | |||
yq e file.yaml -o j | |||
== keywords == | == keywords == |
Latest revision as of 16:27, 20 May 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 ...
Multiple Documents
You can combine multiple documents by seperating them with "---":
document: this is document 1 --- document: this is document 2
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
"Collection" is a list of dictionaries, with a nested list of dictionaries of "people", intermixed with a nested list of "skills", just to make it interseting.
collection: - people: - name: ken job: grunt - name: mike job: manager - skills: - computers - science
JSON for comparison:
{ "collection": [ { "people": [ { "name": "ken", "job": "grunt" }, { "name": "mike", "job": "manager" } ] }, { "skills": [ "computers", "science" ] } ] }
Boolean Values
create_key: yes needs_agent: no knows_oop: True likes_emacs: TRUE uses_cvs: false
Block Text
Multi-Line
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/
YAML to JSON
See yq
yq eval file.yaml -o json yq e file.yaml -o j