Shankar:
Hi Guys…A quick one. I’m currently on the beginners course for ansible. I saw the following example in the video that represents a list of dictionaries. In here, we would want to define the name of the dictionary in the beginning, right? If not, what would be the name of the dictionary? And only the colour
element has a -
in the beginning? What is colour
actually? Is that an element of a list or a dictionary?
If I add cars
in the beginning of the example, what is cars
? Is that the name of the list of dictionaries?
cars:
- colour: Blue
Model: ***
***
***
Máté Varga:
hey!
In this case cars is the name of a list.
The ‘-’ represent an element of the list. Very important, color is just part of the dictionary!
In this case the elements are dictionaries with the keys:
• color
• model
• transition
• price
From these model contains a dictionary too,
with the keys:
• name
• model
If it helps here is the json representation:
{
"cars": [
{
"color": "blue",
"model": { "name": "corvette", "model": 1995 },
"transition": "manual",
"price": "$20,000"
},
{
"color": "grey",
"model": { "name": "corvette", "model": 1995 },
"transition": "manual",
"price": "$22,000"
},
{
"color": "red",
"model": { "name": "corvette", "model": 1995 },
"transition": "manual",
"price": "$20,000"
}
]
}
> In here, we would want to define the name of the dictionary in the beginning, right?
No.
> If not, what would be the name of the dictionary?
It’s not a dictionary.
> And only the colour
element has a -
in the beginning?
Since we use indentation for yaml we need to check where each line starts. In this case color,model and so on are part of an object. The ‘-’ belongs to that object.
> What is colour
actually? Is that an element of a list or a dictionary?
It’s the key of the dictionary.
> If I add cars
in the beginning of the example, what is cars
? Is that the name of the list of dictionaries?
Yes, it’s the name of the list.
A bit redundant answer.
Shankar:
Thankyou @Máté Varga for replying. I have another clarifications as well,
• The Colour, Model, Transition and Price are the Keys of a dictionary and they have their respective values. Can I say Color: Blue
itself is a dictionary? If so, are the Color, Model, Transition and Price all can be said as a single dictionary?
Máté Varga:
Sorry for the late response. Yes, it can.
Máté Varga:
They all can be a separeta dictionary but it will be dependante on the context.
Shankar:
Understood @Máté Varga thank you…