In this article, we are going to learn Ansible YAML Basics. We use YAML to write Ansible Playbooks. Playbooks contain the set of instructions that Ansible is going to execute on the target nodes.
Table of Contents
YAML Basics
YAML (YAML Ain’t Markup Language) is a human-readable data serialization format. It is often used for configuration files, data exchange between languages with different data structures, and applications where data is being stored or transmitted.
Indentation and Structure in YAML
YAML uses indentation to represent the structure of data. Spaces are preferred over tabs, and indentation should be consistent. It uses colons and dashes to denote key-value pairs and lists.
Key-Value Pairs in YAML
Key-value pairs are used to represent data. The key and value are separated by a colon, with a space after the colon. For example:
name: John Doe
age: 30
Lists in YAML
Lists or arrays are represented using a hyphen followed by a space, and items in the list are indented. For example:
- item 1
- item 2
- item 3
Nested Structures in YAML
You can nest data structures within YAML, including dictionaries within dictionaries and lists within dictionaries. Maintain consistent indentation for nested structures.
person:
name: John Doe
age: 30
hobbies:
- Reading
- Swimming
Comments in YAML
YAML supports comments. Comments begin with the # symbol, and everything to the right of it is treated as a comment.
# This is a comment
key: value
Multiline Strings in YAML
You can represent multiline strings using the | character for a literal block style. This preserves line breaks and indentation.
description: |
This is a multiline
string in YAML.
Quoted Strings in Ansible YAML
If your string contains characters that could be interpreted as a YAML indicator (e.g., :, -, #), you can enclose the string in double or single quotes.
message: "This is a string with a colon: it's inside quotes."
Conclusion
In this tutorial, we learned about Ansible Yaml. We are not ready to move on to the next lesson.
Youtube Tutorial Link
You can learn Ansible through the Ansible Tutorial available on our YouTube playlist here.