Except for ansible.cfg which is an .ini file, all ansible files are in yaml, so let's spent some time on YAML.
At the end of inventories chapter we showed how to use yaml to define list and maps variables.
In this chapter we'll go a bit further.
In [ ]:
cd /notebooks/exercise-08/
In [ ]:
import yaml
txt = """
{ "yaml": 'is', 'a superset': 'of json'}
"""
ret = yaml.load(txt)
print(ret)
In [ ]:
# Yoda loves dictionaries ;)
print(yaml.dump(ret))
In [ ]:
# Customized dumper
print(yaml.dump(ret, default_flow_style=False))
In [ ]:
txt = """
# Yaml comments starts with hash
you: {'can':'use', 'brace':'syntax'}
"""
ret = yaml.load(txt)
print(yaml.dump(ret))
In [ ]:
print(yaml.dump(ret, default_flow_style=False))
In [ ]:
# Yaml can describe list..
print(yaml.load("""
- tasks:
- contains
- a
- list
- of
- modules
"""))
In [ ]:
# .. and maps / dicts
print(yaml.load("""
- tasks:
- name: "this dict has two keys: name and debug"
debug: msg="Welcome to Rimini!"
"""))
In [ ]:
print(yaml.load("""
this_works: http://no-spaces-after-colon:8080
"""))
In [ ]:
print(yaml.load("""this_no: spaces: after colon"""))
In [ ]:
# Quoting is important!
print(yaml.load("""
that: "works: though"
"""))
In [ ]:
# This is fine
print(yaml.load("""
this_is: fine={{in_yaml}} but
"""))
# but with ansible you should
print(yaml.load("""
always: quote="{{moustaches}}"
"""))
In [ ]:
text = """
one_line: "Rimini is also tied with the great cinema, since it is representative of Federico Fellini's world of fantasy."
trimmed_one_line: >-
Rimini is also tied with the great cinema,
since it is representative of Federico Fellini's
world of fantasy.
always_one_line: >
Rimini is also tied with the great cinema,
since it is representative of Federico Fellini's
world of fantasy.
"""
ret = yaml.load(text)
assert ret['one_line'] == ret['trimmed_one_line'] == ret['always_one_line']
Or write a multi_line string with proper carets
In [ ]:
text = """
multi: "Rimini, or the ancient Ariminum,
is an art heritage city with over 22 centuries of history.
In 268 B.C., the Roman Senate sent six thousand settlers
who founded the city that was meant to be strategically central
and to develop to this day."
# Comments are ignored from parser.
preserves: |
Rimini, or the ancient Ariminum,
is an art heritage city with over 22 centuries of history.
In 268 B.C., the Roman Senate nsent six thousand settlers
who founded the city that was meant to be strategically central
and to develop to this day.
trims: |-
Rimini, or the ancient Ariminum,
is an art heritage city with over 22 centuries of history.
In 268 B.C., the Roman Senate nsent six thousand settlers
who founded the city that was meant to be strategically central
and to develop to this day.
"""
ret = yaml.load(text)
print(yaml.dump(ret, default_flow_style=False))
In [ ]:
# exercise
preserves = ret['preserves']
trims = ret['trims']
> and | over quote hell:with_itemswhenansible-lintMoustaches are special ansible characters: to verbatim print them, you shoud:
write the "{{" string in it
eg: {{ "{{" }} whatever {{ "}}" }}
In [ ]:
In [ ]:
In [ ]: