Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confusion on list or keys in YAML syntax ansible playbook

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"
like image 637
sherpaurgen Avatar asked Apr 04 '16 10:04

sherpaurgen


People also ask

How do you represent a list in YAML?

Each element in the list is represented in YAML as a new line with the same indentation, starting with - followed by a space.

What does {{ }} mean in Ansible?

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.

How pass multiple values in YAML?

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.

What is the Ansible playbook execution order?

Playbook execution. A playbook runs in order from top to bottom. Within each play, tasks also run in order from top to bottom.


1 Answers

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 tasks 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.

like image 158
ydaetskcoR Avatar answered Sep 22 '22 07:09

ydaetskcoR