YAML Essentials

Code

YAML Essentials

Anchors

Anchors are set using a prefix with a &
Anchors are referenced using a *
Good for reusing repeating values.
If you Re Define an Anchor it will take on the latest value. Just like any variable assignment

---
host: ServerX
country: &AU Australia
province: Melbourne
roles: &wordpress
  - web
  - db
---
host: ServerY
country: *AU
province: Perth
roles: *wordpress

Comments

#This is a comment

Mappings

Mapping are a key value pair. There must be a space between the : and the value

Name: ServerX

Sequences

- nmap
- wireguard

Mapping of Mappings

Computer:
  Name: ServerX
  IP: 10.10.10.10

Sequence of Mappings

- name: nmap
  version: 10
  platform: linux 

Mapping of Sequences

tasks:
  - name: Install_git
  - name: reboot

Multiple Lines

New Lines Preserved

comment: |
  Line 1
  Line 2
  Kine 3

New Lines Ignored

comment: >
  This will
  all appear
  on one single
  line.
  New lines will be converted into a space
  Multiple spaces will be converted into a single space

Multiple Directives/Documents

— Marks the start
… Marks the end

Multi Stream

---
host: ServerX
country: Australia
province: Melbourne
---
host: ServerY
country: AU
province: Perth

Stream With End

---
host: ServerX
country: Australia
province: Melbourne
...
---
host: ServerY
country: AU
province: Perth
...

Tags

Can be set in the MetaDate are of the YAML File. Above the —
The below can be referenced by tag:hostdata:mel:MEL

%TAG ! tag:hostdata:mel:
---
host: ServerX
province: !MEL Melbourne
roles:
  - web
  - db

Change Data Type

Change to string using tag

- name: nmap
  version: !!str 10
  platform: linux 
,