1 00:00:06,690 --> 00:00:08,520 - In this video we are going to explore 2 00:00:08,520 --> 00:00:11,020 multi-valued variables. 3 00:00:11,020 --> 00:00:12,540 So what is going on? 4 00:00:12,540 --> 00:00:15,210 Well, there are two ways to store variables 5 00:00:15,210 --> 00:00:18,410 with multiple values, the dictionary and the array. 6 00:00:18,410 --> 00:00:21,050 And you need to understand the difference. 7 00:00:21,050 --> 00:00:23,310 Dictionaries and arrays and Ansible are based 8 00:00:23,310 --> 00:00:25,740 on Python dictionaries and arrays. 9 00:00:25,740 --> 00:00:28,250 An array which is also known as a list 10 00:00:28,250 --> 00:00:30,760 is an ordered list of values where each value 11 00:00:30,760 --> 00:00:33,260 can be addressed individually. 12 00:00:33,260 --> 00:00:35,150 It looks like list is, and then 13 00:00:35,150 --> 00:00:37,560 between square brackets and every single value 14 00:00:37,560 --> 00:00:40,163 between double quotes, you have the values. 15 00:00:41,380 --> 00:00:43,270 And if you use a command 16 00:00:43,270 --> 00:00:46,290 in Python like print on the variable list, 17 00:00:46,290 --> 00:00:50,730 value zero then you print array index zero 18 00:00:50,730 --> 00:00:52,533 which will return to value one. 19 00:00:53,520 --> 00:00:57,580 A dictionary or a hash is an unordered collection of values, 20 00:00:57,580 --> 00:00:59,453 which is stored as a key-value pair. 21 00:01:00,470 --> 00:01:04,660 You would write it as dictionary equals one, colon, 22 00:01:04,660 --> 00:01:08,100 text one, two colon, text two, three colon, 23 00:01:08,100 --> 00:01:11,740 text three, and you can use print Dict 24 00:01:11,740 --> 00:01:14,770 to print the actual dictionary value. 25 00:01:14,770 --> 00:01:17,160 Notice that the dictionary itself to make it even 26 00:01:17,160 --> 00:01:20,980 a little bit more confusing can be presented in a list. 27 00:01:20,980 --> 00:01:22,820 We'll talk about that later. 28 00:01:22,820 --> 00:01:25,700 Before we continue, you should also notice that an array 29 00:01:25,700 --> 00:01:26,800 is the same as a list 30 00:01:26,800 --> 00:01:29,820 and a dictionary is the same as a hash. 31 00:01:29,820 --> 00:01:33,160 Now let's try to understand array and dictionaries. 32 00:01:33,160 --> 00:01:36,380 So multi-valued variables can be used in playbooks. 33 00:01:36,380 --> 00:01:38,490 And when using a multi-valued variable, 34 00:01:38,490 --> 00:01:42,260 it can be written as an array or a list or as a dictionary. 35 00:01:42,260 --> 00:01:45,670 And each of these has their own specific use cases. 36 00:01:45,670 --> 00:01:49,520 Dictionaries are the default to use in Ansible facts 37 00:01:49,520 --> 00:01:52,470 and arrays are common for multi-valued variables. 38 00:01:52,470 --> 00:01:55,763 And the big benefit is that they easily support loops. 39 00:01:56,860 --> 00:01:59,740 You are going to learn about loops in the next lesson 40 00:01:59,740 --> 00:02:02,350 which is about using conditionals, 41 00:02:02,350 --> 00:02:03,850 but already I need to tell you 42 00:02:03,850 --> 00:02:06,600 that loops are not supported on dictionaries directly, 43 00:02:06,600 --> 00:02:09,990 only by using the dict2items filter. 44 00:02:09,990 --> 00:02:13,350 We'll talk about filters later in lesson 10. 45 00:02:13,350 --> 00:02:15,340 Now let's focus on dictionary. 46 00:02:15,340 --> 00:02:17,720 Dictionaries can be written in two ways, 47 00:02:17,720 --> 00:02:21,660 so you can write them in the yamal notation, 48 00:02:21,660 --> 00:02:23,440 which should be the preferred way 49 00:02:23,440 --> 00:02:25,890 because the indentation is nicely indicating 50 00:02:25,890 --> 00:02:27,050 what you are doing. 51 00:02:27,050 --> 00:02:30,540 So we have users, two users, Linda and Lisa 52 00:02:30,540 --> 00:02:32,850 and every user has their own property. 53 00:02:32,850 --> 00:02:35,510 And you can also write it this way. 54 00:02:35,510 --> 00:02:40,510 And if you use a dictionary, there are two notations. 55 00:02:40,630 --> 00:02:42,740 There is a notation that we have already seen 56 00:02:42,740 --> 00:02:45,980 in a multi-valued variable, which is variable name 57 00:02:45,980 --> 00:02:47,400 in between square brackets. 58 00:02:47,400 --> 00:02:52,400 You use the specific value that you want to address 59 00:02:52,520 --> 00:02:54,520 as in users, Linda shell, 60 00:02:54,520 --> 00:02:58,310 which will print the shell for you. So Linda. 61 00:02:58,310 --> 00:03:01,140 You can also use the dotted format 62 00:03:01,140 --> 00:03:02,650 which we have talked about before. 63 00:03:02,650 --> 00:03:04,840 This is basically what we've already seen 64 00:03:04,840 --> 00:03:06,563 when we were talking about facts. 65 00:03:07,640 --> 00:03:09,840 So dictionaries are used in facts. 66 00:03:09,840 --> 00:03:12,560 Arrays are used in conditionals. 67 00:03:12,560 --> 00:03:16,160 And again, notice that dictionaries can be a part of a list 68 00:03:16,160 --> 00:03:19,680 which is used in Ansible facts. 69 00:03:19,680 --> 00:03:21,640 Now let's talk about arrays. 70 00:03:21,640 --> 00:03:23,730 An array provides a list of item 71 00:03:23,730 --> 00:03:26,520 where each item can be addressed separately. 72 00:03:26,520 --> 00:03:29,870 You can easily recognize the array because in an array 73 00:03:29,870 --> 00:03:32,420 every item starts with a hyphen. 74 00:03:32,420 --> 00:03:35,530 So we have username, Linda, username, Lisa 75 00:03:35,530 --> 00:03:39,810 and the array has different properties for these users. 76 00:03:39,810 --> 00:03:42,440 The nice thing about an array is that individual items 77 00:03:42,440 --> 00:03:44,480 in the array can be addressed 78 00:03:44,480 --> 00:03:48,150 by using var_ name, square bracket, zero, for instance 79 00:03:48,150 --> 00:03:50,470 where zero is the array offset 80 00:03:50,470 --> 00:03:53,801 and print the first value in the array. 81 00:03:53,801 --> 00:03:55,480 And you can use arrays for looping 82 00:03:55,480 --> 00:03:58,270 and looping as said before is not what you should do 83 00:03:58,270 --> 00:04:01,850 on dictionaries and to access all of the variables 84 00:04:01,850 --> 00:04:04,560 in an array you can use with items or loop. 85 00:04:04,560 --> 00:04:07,230 This also is something that we are going to discover 86 00:04:07,230 --> 00:04:09,593 in the next lesson about conditionals. 87 00:04:10,650 --> 00:04:13,550 Now, how do we recognize list and dictionaries? 88 00:04:13,550 --> 00:04:15,600 If you're an experienced Phython programmer 89 00:04:15,600 --> 00:04:19,630 that might be easy if you're not, it's not that obvious. 90 00:04:19,630 --> 00:04:21,530 Now the thing is in output like facts, 91 00:04:21,530 --> 00:04:25,540 a list is always written between square brackets 92 00:04:25,540 --> 00:04:29,450 and dictionaries are written between curly brackets. 93 00:04:29,450 --> 00:04:32,910 And if the output is just showing double quotes 94 00:04:32,910 --> 00:04:34,530 then it's a string. 95 00:04:34,530 --> 00:04:37,340 A nice way to show that is by having a look 96 00:04:37,340 --> 00:04:41,220 at ansible_mounts in the facts, because in ansible_mounts 97 00:04:41,220 --> 00:04:44,173 you will actually see a list of dictionaries. 98 00:04:45,090 --> 00:04:46,350 Let's go check this out. 99 00:04:46,350 --> 00:04:49,163 And also let's have a look at a couple of examples. 100 00:04:51,080 --> 00:04:54,783 So first I would like to talk about recognizing this stuff. 101 00:04:56,440 --> 00:05:00,480 We've created the Ubuntu fax textel earlier 102 00:05:00,480 --> 00:05:04,630 and I would like to show you in this fax textel 103 00:05:04,630 --> 00:05:06,690 what we can see. 104 00:05:06,690 --> 00:05:08,640 So what already starts here, 105 00:05:08,640 --> 00:05:11,350 Ansible all IP four addresses. 106 00:05:11,350 --> 00:05:12,650 We can see the square brackets. 107 00:05:12,650 --> 00:05:16,120 So the square brackets, that means that this is an array 108 00:05:16,120 --> 00:05:19,513 an array that has one specific value only. 109 00:05:20,720 --> 00:05:23,710 Then we can see a dictionary like here in 110 00:05:23,710 --> 00:05:24,780 Ansible commands line. 111 00:05:24,780 --> 00:05:27,460 So Ansible commands line is a dictionary. 112 00:05:27,460 --> 00:05:29,530 And in this dictionary 113 00:05:29,530 --> 00:05:33,320 we have these multiple key value pairs 114 00:05:33,320 --> 00:05:35,340 and notice that Ansible effects itself 115 00:05:35,340 --> 00:05:38,480 is a dictionary, a dictionary containing dictionaries 116 00:05:38,480 --> 00:05:40,053 which may contain arrays. 117 00:05:41,000 --> 00:05:44,170 Here we have examples of just strings. 118 00:05:44,170 --> 00:05:47,600 So if you just see the double quotes, then this is a string. 119 00:05:47,600 --> 00:05:50,380 Now how about the Ansible mounts? 120 00:05:50,380 --> 00:05:51,900 Well, if you have a look at that, 121 00:05:51,900 --> 00:05:53,630 there you can see Ansible mounts 122 00:05:53,630 --> 00:05:55,330 starting with a square bracket 123 00:05:55,330 --> 00:05:59,320 which means that the contents really is an array. 124 00:05:59,320 --> 00:06:02,380 And in the array we have dictionaries. 125 00:06:02,380 --> 00:06:05,480 So it's an array of dictionaries and yes 126 00:06:05,480 --> 00:06:08,590 that is how you can mix the two of them together. 127 00:06:08,590 --> 00:06:11,960 Now, at this point, that might not make total sense, 128 00:06:11,960 --> 00:06:14,600 but by the time we've talked about conditionals, 129 00:06:14,600 --> 00:06:16,983 you will try, you will start to understand. 130 00:06:17,850 --> 00:06:20,340 Let's have a look at some quick examples, 131 00:06:20,340 --> 00:06:22,330 which are in the sub-directory, 132 00:06:22,330 --> 00:06:25,803 arrays in the basics directory and of course get repository. 133 00:06:26,720 --> 00:06:28,760 So we have variables 134 00:06:28,760 --> 00:06:33,680 and I'm going to show you vars users dictionary. 135 00:06:37,000 --> 00:06:39,810 And there we go, we can see the dictionary. 136 00:06:39,810 --> 00:06:42,240 Now, how do you recognize this as a dictionary? 137 00:06:42,240 --> 00:06:44,030 You recognize it as a dictionary 138 00:06:44,030 --> 00:06:46,290 mainly because there are no hyphens. 139 00:06:46,290 --> 00:06:47,930 If you define an array 140 00:06:47,930 --> 00:06:51,430 every item in the array starts with a hyphen. 141 00:06:51,430 --> 00:06:56,430 If you compare that to users list the other example, 142 00:06:57,290 --> 00:06:58,810 there you can see the clear difference 143 00:06:58,810 --> 00:07:01,923 between the dictionary and the array. 144 00:07:03,090 --> 00:07:05,750 Now, how are we going to use them? 145 00:07:05,750 --> 00:07:10,050 Well, there is multi-list dot YAML 146 00:07:10,050 --> 00:07:11,830 and in multi-list dot YAML. 147 00:07:11,830 --> 00:07:12,870 What do we see? 148 00:07:12,870 --> 00:07:16,650 We see that we are including the variable fell 149 00:07:16,650 --> 00:07:21,060 and in this variable fell, we are addressing the items 150 00:07:21,060 --> 00:07:23,690 in the array by looping over users. 151 00:07:23,690 --> 00:07:26,060 Now we haven't really seen loop yet. 152 00:07:26,060 --> 00:07:27,960 Loop is something that you can use 153 00:07:27,960 --> 00:07:30,820 on an array and loop will work on every single item 154 00:07:30,820 --> 00:07:32,670 that is encountered on the array. 155 00:07:32,670 --> 00:07:34,310 So we have item, dot, username 156 00:07:34,310 --> 00:07:37,040 and item dot home there, and item dot shell. 157 00:07:37,040 --> 00:07:38,120 Now, how does that work? 158 00:07:38,120 --> 00:07:40,710 If you get back to the user's list 159 00:07:40,710 --> 00:07:43,940 the item is the thing that you can see right here 160 00:07:43,940 --> 00:07:45,580 starting with a hyphen. 161 00:07:45,580 --> 00:07:47,710 So item dot username is going 162 00:07:47,710 --> 00:07:51,530 for the first item for the second item for the third item. 163 00:07:51,530 --> 00:07:55,920 So every item is individually explored. 164 00:07:55,920 --> 00:08:00,710 And if you run it, ansible-playbook on multi list, 165 00:08:00,710 --> 00:08:02,533 then this is what you get. 166 00:08:03,950 --> 00:08:08,510 So one line for every single item in the array. 167 00:08:08,510 --> 00:08:10,740 Now how about the dictionary? 168 00:08:10,740 --> 00:08:12,970 What can we do with the dictionary? 169 00:08:12,970 --> 00:08:17,760 Well, dictionaries are honestly not as spectacular, 170 00:08:17,760 --> 00:08:20,220 but in a dictionary you can address 171 00:08:20,220 --> 00:08:22,200 the dictionary value specifically. 172 00:08:22,200 --> 00:08:24,077 So users dot Linda dot username 173 00:08:24,077 --> 00:08:29,077 and users dot Linda dot homedir is what you can use 174 00:08:29,130 --> 00:08:33,380 but there is no way to easily loop over the values 175 00:08:33,380 --> 00:08:35,570 in a dictionary. 176 00:08:35,570 --> 00:08:39,820 So let's have a look and there, we can see the result. 177 00:08:39,820 --> 00:08:43,840 So two different ways of using multi-valued variables. 178 00:08:43,840 --> 00:08:46,200 And that is really something that you need to be aware of. 179 00:08:46,200 --> 00:08:49,630 So that you use the right approach when encountering 180 00:08:49,630 --> 00:08:51,240 the right variable type. 181 00:08:51,240 --> 00:08:55,110 Most important the ansible facts by nature 182 00:08:55,110 --> 00:08:58,420 is a huge collection of dictionaries 183 00:08:58,420 --> 00:09:01,820 and arrays that is what you can use to loop over. 184 00:09:01,820 --> 00:09:05,133 We'll see more of this later throughout the scores.