- In this video we are going to explore
multi-valued variables.
So what is going on?
Well, there are two ways to store variables
with multiple values, the dictionary and the array.
And you need to understand the difference.
Dictionaries and arrays and Ansible are based
on Python dictionaries and arrays.
An array which is also known as a list
is an ordered list of values where each value
can be addressed individually.
It looks like list is, and then
between square brackets and every single value
between double quotes, you have the values.
And if you use a command
in Python like print on the variable list,
value zero then you print array index zero
which will return to value one.
A dictionary or a hash is an unordered collection of values,
which is stored as a key-value pair.
You would write it as dictionary equals one, colon,
text one, two colon, text two, three colon,
text three, and you can use print Dict
to print the actual dictionary value.
Notice that the dictionary itself to make it even
a little bit more confusing can be presented in a list.
We'll talk about that later.
Before we continue, you should also notice that an array
is the same as a list
and a dictionary is the same as a hash.
Now let's try to understand array and dictionaries.
So multi-valued variables can be used in playbooks.
And when using a multi-valued variable,
it can be written as an array or a list or as a dictionary.
And each of these has their own specific use cases.
Dictionaries are the default to use in Ansible facts
and arrays are common for multi-valued variables.
And the big benefit is that they easily support loops.
You are going to learn about loops in the next lesson
which is about using conditionals,
but already I need to tell you
that loops are not supported on dictionaries directly,
only by using the dict2items filter.
We'll talk about filters later in lesson 10.
Now let's focus on dictionary.
Dictionaries can be written in two ways,
so you can write them in the yamal notation,
which should be the preferred way
because the indentation is nicely indicating
what you are doing.
So we have users, two users, Linda and Lisa
and every user has their own property.
And you can also write it this way.
And if you use a dictionary, there are two notations.
There is a notation that we have already seen
in a multi-valued variable, which is variable name
in between square brackets.
You use the specific value that you want to address
as in users, Linda shell,
which will print the shell for you. So Linda.
You can also use the dotted format
which we have talked about before.
This is basically what we've already seen
when we were talking about facts.
So dictionaries are used in facts.
Arrays are used in conditionals.
And again, notice that dictionaries can be a part of a list
which is used in Ansible facts.
Now let's talk about arrays.
An array provides a list of item
where each item can be addressed separately.
You can easily recognize the array because in an array
every item starts with a hyphen.
So we have username, Linda, username, Lisa
and the array has different properties for these users.
The nice thing about an array is that individual items
in the array can be addressed
by using var_ name, square bracket, zero, for instance
where zero is the array offset
and print the first value in the array.
And you can use arrays for looping
and looping as said before is not what you should do
on dictionaries and to access all of the variables
in an array you can use with items or loop.
This also is something that we are going to discover
in the next lesson about conditionals.
Now, how do we recognize list and dictionaries?
If you're an experienced Phython programmer
that might be easy if you're not, it's not that obvious.
Now the thing is in output like facts,
a list is always written between square brackets
and dictionaries are written between curly brackets.
And if the output is just showing double quotes
then it's a string.
A nice way to show that is by having a look
at ansible_mounts in the facts, because in ansible_mounts
you will actually see a list of dictionaries.
Let's go check this out.
And also let's have a look at a couple of examples.
So first I would like to talk about recognizing this stuff.
We've created the Ubuntu fax textel earlier
and I would like to show you in this fax textel
what we can see.
So what already starts here,
Ansible all IP four addresses.
We can see the square brackets.
So the square brackets, that means that this is an array
an array that has one specific value only.
Then we can see a dictionary like here in
Ansible commands line.
So Ansible commands line is a dictionary.
And in this dictionary
we have these multiple key value pairs
and notice that Ansible effects itself
is a dictionary, a dictionary containing dictionaries
which may contain arrays.
Here we have examples of just strings.
So if you just see the double quotes, then this is a string.
Now how about the Ansible mounts?
Well, if you have a look at that,
there you can see Ansible mounts
starting with a square bracket
which means that the contents really is an array.
And in the array we have dictionaries.
So it's an array of dictionaries and yes
that is how you can mix the two of them together.
Now, at this point, that might not make total sense,
but by the time we've talked about conditionals,
you will try, you will start to understand.
Let's have a look at some quick examples,
which are in the sub-directory,
arrays in the basics directory and of course get repository.
So we have variables
and I'm going to show you vars users dictionary.
And there we go, we can see the dictionary.
Now, how do you recognize this as a dictionary?
You recognize it as a dictionary
mainly because there are no hyphens.
If you define an array
every item in the array starts with a hyphen.
If you compare that to users list the other example,
there you can see the clear difference
between the dictionary and the array.
Now, how are we going to use them?
Well, there is multi-list dot YAML
and in multi-list dot YAML.
What do we see?
We see that we are including the variable fell
and in this variable fell, we are addressing the items
in the array by looping over users.
Now we haven't really seen loop yet.
Loop is something that you can use
on an array and loop will work on every single item
that is encountered on the array.
So we have item, dot, username
and item dot home there, and item dot shell.
Now, how does that work?
If you get back to the user's list
the item is the thing that you can see right here
starting with a hyphen.
So item dot username is going
for the first item for the second item for the third item.
So every item is individually explored.
And if you run it, ansible-playbook on multi list,
then this is what you get.
So one line for every single item in the array.
Now how about the dictionary?
What can we do with the dictionary?
Well, dictionaries are honestly not as spectacular,
but in a dictionary you can address
the dictionary value specifically.
So users dot Linda dot username
and users dot Linda dot homedir is what you can use
but there is no way to easily loop over the values
in a dictionary.
So let's have a look and there, we can see the result.
So two different ways of using multi-valued variables.
And that is really something that you need to be aware of.
So that you use the right approach when encountering
the right variable type.
Most important the ansible facts by nature
is a huge collection of dictionaries
and arrays that is what you can use to loop over.
We'll see more of this later throughout the scores.