I'm new to YAML and ansible and I'm confused by which line is the key and which one is a list. For example in the playbook below it seems that "-" represents a list item.
---
- hosts: all
gather_facts: no
sudo: yes
tasks:
- name: Ensure NTP is installed.
yum: name=ntp state=present
- name: ensure ntp is running
service: name=ntpd state=started enabled=yes
So host
is list key and all
is its value, but is gather_facts: no
, sudo:
yes
and task: ...
also the dictionaries of the list with key as - hosts
?
When running the above playbook into yamllint.com I get the following output:
---
-
gather_facts: false
hosts: all
sudo: true
tasks:
-
name: "Ensure NTP is installed."
yum: "name=ntp state=present"
-
name: "ensure ntp is running"
service: "name=ntpd state=started enabled=yes"
Each element in the list is represented in YAML as a new line with the same indentation, starting with - followed by a space.
Ansible uses the jinja2 template. the {{ }} are used to evaluate the expression inside them from the context passed. So {{ '{{' }} evaluates to the string {{ And the while expression {{ docroot }} is written to a template, where docroot could be another template variable.
In YAML, Array represents a single key mapped to multiple values. Each value starts with a hyphen - symbol followed by space. In a single line, write the same thing above using 'square brackets syntax.
Playbook execution. A playbook runs in order from top to bottom. Within each play, tasks also run in order from top to bottom.
The -
represents a list item. At the top level of a playbook then the list item represents a "play".
Each play is represented by a dictionary which has a number of parameters/keys that have values (some of these values, in turn, may also be lists or dictionaries).
So to answer your question more specifically: hosts
, gather_facts
, tasks
are all keys of the first, and only, play in the playbook. As such they should be indented to the same level.
The value of tasks is, in turn, a list of task
s which are represented by a dictionary.
As for the automatically "fixed" YAML generated by yamllint.com, I haven't a clue if Ansible will parse that properly as it looks odd to me but technically it should. I'd still stick with your non "fixed" YAML though as that formatting looks particularly bizarre.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With