Tuesday, April 7, 2026

Ansible_5

 Indentation and Whitespace

Like Python, YAML uses space indentation to reduce the number of interpunction characters. We use two spaces as a standard. For readability, we prefer to add whitespace between each task in a playbook, and between sections in files.

Strings
In general, you don’t need to quote YAML strings. Even if there are spaces, you don’t need to quote them. For example, this is a string in YAML:

this is a lovely sentence

The JSON equivalent is as follows:
"this is a lovely sentence"

Use single quotes for literal values that should not be evaluated, like version numbers and floating point numbers, or strings with reserved characters like colons, brackets, or braces.

Booleans
YAML has a native Boolean type and provides you with a variety of values that evaluate to true or false.

These are all Boolean true values in YAML:
true, True, TRUE, yes, Yes, YES, on, On, ON
JSON only uses: true

These are all Boolean false values in YAML:
false, False, FALSE, no, No, NO, off, Off, OFF
JSON only uses:false

Lists
YAML lists are like arrays in JSON and Ruby, or lists in Python.The YAML specification calls these sequences, but we call them lists here to be consistent with the official Ansible documentation.
Indent list items and delimit them with hyphens.

shows:
- My Fair Lady
- Oklahoma
- The Pirates of Penzance

YAML also supports an inline format for lists, with comma-separated values in square brackets:
shows: [ My Fair Lady , Oklahoma , The Pirates of Penzance ]


Dictionaries
YAML dictionaries are like objects in JSON, dictionaries in Python, hashes in Ruby, or associative arrays in PHP The YAML specification calls them mappings, but we call them dictionaries here to be consistent with the Ansible documentation.

address:
street: Main Street
appt: 742
city: Logan
state: Ohio

YAML also supports an inline format for dictionaries, with comma separated tuples in braces:
address: { street: Main Street, appt: '742', city: Logan, state:Ohio}

Multiline Strings
You can format multiline strings with YAML by combining a block style indicator (| or >)

If you want one string across multiple lines:
Using |
message: |
This is line one
This is line two
This is line three

Using >
message: >
This is line one
This is line two
This is line three


output will be
This is line one This is line two This is line three


No comments:

Post a Comment