YAML

YAML

May 25, 2024 | seedling, permanent

tags :

What is YAML? #

Ansible ref

  • Yet Another Markup Language.
  • Super set of json
  • File begins with “—”

What is the difference between YAML and JSON? #

When to prefer one over the other

DataStructure #

List: #

  • All members of a list are lines beginning at the same indentation level starting with a “- " (a dash and a space):
    ---
    # A list of tasty fruits
    fruits:
    ​    - Apple
    ​    - Orange
    ​    - Strawberry
    ​    - Mango
    ...
    

Dictionary: #

  • A dictionary is represented in a simple key: value form (the colon must be followed by a space):
    # An employee record
    martin:
        name: Martin D'vloper
        job: Developer
        skill: Elite
    

More complicated data structures are possible, such as lists of dictionaries, dictionaries whose values are lists or a mix of both: #

# Employee records
-  martin:
    name: Martin D'vloper
    job: Developer
    skills:
      - python
      - perl
      - pascal
-  tabitha:
    name: Tabitha Bitumen
    job: Developer
    skills:
      - lisp
      - fortran
      - erlang

Dictionaries and lists can also be represented in an abbreviated form if you really want to: #

---
martin: name: Martin D'vloper, job: Developer, skill: Elite
fruits: ['Apple', 'Orange', 'Strawberry', 'Mango']

Ansible doesn’t really use these too much, but you can also specify a boolean value (true/false) in several forms: #

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

Values can span multiple lines using | or >. #

Spanning multiple lines using a | will include the newlines. Using a > will ignore newlines; it’s used to make what would otherwise be a very long line easier to read and edit. In either case the indentation will be ignored. Examples are:

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

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

Let’s combine what we learned so far in an arbitrary YAML example. This really has nothing to do with Ansible, but will give you a feel for the format: #

---
# An employee record
name: Martin D'vloper
job: Developer
skill: Elite
employed: True
foods:
    - Apple
    - Orange
    - Strawberry
    - Mango
languages:
    perl: Elite
    python: Elite
    pascal: Lame
education: |
    4 GCSEs
    3 A-Levels
    BSc in the Internet of Things    

That’s all you really need to know about YAML to start writing Ansible playbooks. #


No notes link to this note

Go to random page

Previous Next