Hi friends, I have a question regarding YAML syntax from the Ansible for Beginne . . .

Ryan McKenna US:
Hi friends, I have a question regarding YAML syntax from the Ansible for Beginners lab.

In the training slide a Key Value / Dictionary is defined with this syntax:

Fruits:
    -    Banana:
             Calories: 105
             Fat: 0.4g
             Carbs: 27g

This will create a dictionary named Fruits, with the first item being named Banana, with the attributes of Calories, Fat, and Carbs

Where I’m a little confused is in the Lab portion when we convert a Dictionary to a List of Dictionaries, the solution indicates we should end up with this as the List of Dictionaries:

employees:
  - name: john
    gender: male
    age: 24
  - name: sarah
    gender: female
    age: 28

This will create a dictionary named employees, with the first item being a Key-Value pair. Is that allowed, can YAML have a KVP as a list item?

If that’s allowed, how would we reference to this item? Would that be employees[0] or similar?

Al West:
Yes, in YAML, you can have a list of dictionaries (key-value pairs). When you want to reference the items you would use a zero based index, for example in Jinja2:
'{{ employees[0].name }}'

Ryan McKenna US:
got it, so for the first example could you reference it by the name Banana? Such as:
'{{ Fruits[Banana].Calories }}'

Al West:
No fruits, is a list. So you would access Calories like so:
'{{ Fruits[0].Banana.Calories }}'

You could have a dictionary of dictionaries such as:

Fruits:
  Banana:
    Calories: 105
    Fat: 0.4g
    Carbs: 27g
  Apple:
    Calories: 95
    Fat: 0.3g
    Carbs: 25g

And then you could use:
'{{ Fruits.Banana.Calories }}'

Ryan McKenna US:
In the first example, it sounds like it is referring to the same object twice, once by the position in the list Fruits ([0]) and then by the name of the dictionary at that position (Banana). Do I understand that correctly?

Ryan McKenna US:
ok, so in that case it seems possible to have multiple items at position [0], so would this be a valid syntax?

Fruits:
-  Banana:
    Calories: 105
    Fat: 0.4g
    Carbs: 27g
  Apple:
    Calories: 95
    Fat: 0.3g
    Carbs: 25g

'{{ Fruits[0].Apple.Calories }}'