… but you don’t need
--- at the start of a YAML file in Ansible.
What does the Ansible documentation say?
I know, I know, if you look at the official documentation on docs.ansible.com, then all of the examples start with
---. And if the official examples do it, then everyone should just blindly copy that without thinking, right?
Wrong! The Ansible documentation on YAML syntax says:
There’s another small quirk to YAML. All YAML files (regardless of their association with Ansible or not) can optionally begin with© Copyright Ansible project contributors.
---and end with
.... This is part of the YAML format and indicates the start and end of a document.
I’ve added the emphasis: optionally. They then continue with one example with
--- at the start and
... at the end. The funny thing is, that’s about the only example on the Ansible documentation site (that I could find) that ends with
.... So the end marker
... is clearly optional. What about the start marker
What does the YAML specification say?
Ansible uses version 1.2 of the YAML specification and unless you are doing something really exotic, that’s the only version you should care about. Revision 1.2.0 was published in July 2009 and revision 1.2.2 in October 2021. That last revision doesn’t make any changes to the specification, it only corrects some errors and adds clarity.
A stream can contain zero or more documents. It’s called a (character) stream because it can be something else than a file on your hard disk, for example some data that’s sent over a network connection. So your Ansible playbook file with extension
.yaml is not a YAML document, it’s a YAML stream.
A document can have several parts:
- Document prefix: optional character encoding and optional comment lines.
Seriously, it’s 2022, are you going to make life hard for yourself and use any other encoding than ASCII or UTF-8? The default encoding that every YAML processor, inclusing Ansible, must support is UTF-8. So You Ain’t Gonna Need It.
Comments can be placed anywhere, so don’t worry.
- Document directives: these are instructions to the YAML processor and aren’t part of the data structure. The only directive I’ve occasionally seen in the wild is
%YAML 1.2, to indicate the version of YAML used. That’s the default version for Ansible anyway, so You Ain’t Gonna Need It.
- Document markers: a parser needs some way to know where directives stop and document content begins. That’s the directives end marker,
---. There is also a document end marker,
..., which tells a parser to stop looking for content and start scanning for directives again. If there are no markers and the first line doesn’t start with
%(a directive), then a parser knows that everything is content. In real life you probably won’t ever have multiple documents in the same stream (file), instead you’ll organize your Ansible code in separate
.yamlfiles, with playbooks and roles and tasks etc.
- Document content: that’s the only really interesting stuff you care about.
YAML knows 3 types of documents:
- Bare documents: don’t begin with directives or marker lines. Such documents are very “clean” as they contain nothing other than the content. This is the kind of YAML documents I prefer for Ansible.
- Explicit documents: begin with an explicit directives end maker (
---) but have no directives. This is the style that many people use if they just copy/paste examples from Stack Overflow.
- Directives documents: start with some directives, followed by an explicit directives end marker. You don’t need directives for Ansible.
rules: document-start: present: false truthy: allowed-values: ['true', 'false', 'yes', 'no']
document-start makes sure that there is no
--- at the start of a file. I also have opinions on
truthy: an Ansible playbook is supposed to be readable both by machines and humans, and then it makes sense to allow the more human-readable values
Do you also have opinions that make you change the default configuration of your linters?