1 00:00:06,370 --> 00:00:08,680 - So let's have a look at how we can use variables 2 00:00:08,680 --> 00:00:10,100 in the a playbook. 3 00:00:10,100 --> 00:00:13,270 Variables themselves can be defined in different ways. 4 00:00:13,270 --> 00:00:15,600 You can set them in the playbook directly, 5 00:00:15,600 --> 00:00:18,490 but that is bad practice as the site-specific information, 6 00:00:18,490 --> 00:00:21,140 in that case, is not separated from the code. 7 00:00:21,140 --> 00:00:22,150 But hey, let's face it, 8 00:00:22,150 --> 00:00:25,660 in some situations, you can do it anyway. 9 00:00:25,660 --> 00:00:27,930 If you start your playbook with a list of variables 10 00:00:27,930 --> 00:00:30,100 at least you make it easy to modify 11 00:00:30,100 --> 00:00:33,173 if you're going to run it in some specific information. 12 00:00:34,020 --> 00:00:35,830 If you are referring to a variable 13 00:00:35,830 --> 00:00:38,220 inside the YAML code in your playbook 14 00:00:38,220 --> 00:00:41,630 the default rule is that you need double braces. 15 00:00:41,630 --> 00:00:45,280 As in the variable is double braces, my variable. 16 00:00:45,280 --> 00:00:48,030 And the double braces make that the value of the variable 17 00:00:48,030 --> 00:00:49,863 is interpreted the right way. 18 00:00:50,780 --> 00:00:53,140 If a value starts with a variable, 19 00:00:53,140 --> 00:00:55,550 the variable needs to be between double quotes, 20 00:00:55,550 --> 00:00:57,410 as well as double braces. 21 00:00:57,410 --> 00:01:00,860 Notice that in the line that you can see here on the slide 22 00:01:00,860 --> 00:01:03,670 the entire value is between double quotes 23 00:01:03,670 --> 00:01:05,270 not just the variable, 24 00:01:05,270 --> 00:01:08,250 but the fact that the value starts with the variable 25 00:01:08,250 --> 00:01:11,790 makes it a variable must be between double quotes. 26 00:01:11,790 --> 00:01:14,830 If you are using a variable in a when statement 27 00:01:14,830 --> 00:01:17,380 which we will talk about in the next lesson 28 00:01:17,380 --> 00:01:19,250 curly braces are omitted, 29 00:01:19,250 --> 00:01:21,440 and there's a couple of other situations 30 00:01:21,440 --> 00:01:24,260 where variables are noted in a different way. 31 00:01:24,260 --> 00:01:25,920 Can be a little bit confusing, 32 00:01:25,920 --> 00:01:28,930 but hey let's try to figure out how it works. 33 00:01:28,930 --> 00:01:30,400 Before doing anything else, 34 00:01:30,400 --> 00:01:34,273 let's have a look at the basic example of using variables. 35 00:01:35,260 --> 00:01:38,510 So this time we are looking at the basics directory. 36 00:01:38,510 --> 00:01:41,613 In the basics directory there is variables_example.yaml, 37 00:01:42,520 --> 00:01:45,070 and let's have look at this variables_example.yaml. 38 00:01:46,538 --> 00:01:50,150 As you can see it's pretty easy to understand playbook. 39 00:01:50,150 --> 00:01:52,530 It creates a user using a variable. 40 00:01:52,530 --> 00:01:53,543 On the host rocky, 41 00:01:55,000 --> 00:01:58,200 the vars are defined in the playbook itself. 42 00:01:58,200 --> 00:02:00,370 So we have a variable and in the variable 43 00:02:00,370 --> 00:02:05,253 I'm setting the variable user to the value lisa. 44 00:02:06,850 --> 00:02:09,800 So the variable, also known as the key, is user. 45 00:02:09,800 --> 00:02:13,190 And lisa is the value that I want to set it to. 46 00:02:13,190 --> 00:02:17,190 Notice that the variables integrate in the play header. 47 00:02:17,190 --> 00:02:19,420 That is the highest level in the playbook 48 00:02:19,420 --> 00:02:21,050 where you can set your variables. 49 00:02:21,050 --> 00:02:24,250 It also means that if you have a multiple play playbook 50 00:02:24,250 --> 00:02:25,580 you need a different approach 51 00:02:25,580 --> 00:02:27,483 to set your variables accordingly. 52 00:02:28,450 --> 00:02:32,520 Next, we have the tasks and in the tasks we have name, 53 00:02:32,520 --> 00:02:35,140 create a user double curly braces user 54 00:02:35,140 --> 00:02:36,390 on host ansible_hostname. 55 00:02:37,620 --> 00:02:40,030 User is a variable that we defined right here. 56 00:02:40,030 --> 00:02:44,220 So in the task description, we will see that it'll create 57 00:02:44,220 --> 00:02:46,240 the user and then it'll create this user 58 00:02:46,240 --> 00:02:47,793 on the host ansible_hostname. 59 00:02:49,380 --> 00:02:53,170 Now this ansible_hostname is also noted as a variable. 60 00:02:53,170 --> 00:02:54,820 What kind of variable is this? 61 00:02:54,820 --> 00:02:57,030 This is an Ansible fact. 62 00:02:57,030 --> 00:03:00,040 Facts are discovered variables and will talk about facts 63 00:03:00,040 --> 00:03:01,720 in more detail later. 64 00:03:01,720 --> 00:03:03,980 Next we have the user module and the user module 65 00:03:03,980 --> 00:03:07,770 is name and name is referring to the value of the variable. 66 00:03:07,770 --> 00:03:11,600 But as this time, the value is starting with the variable 67 00:03:11,600 --> 00:03:14,090 or the entire value is just the variable. 68 00:03:14,090 --> 00:03:16,920 The entire value must be between double quotes. 69 00:03:16,920 --> 00:03:19,040 So mind your syntax. 70 00:03:19,040 --> 00:03:19,873 Let's run it. 71 00:03:30,420 --> 00:03:33,710 So you can see it's starting with the fact gathering. 72 00:03:33,710 --> 00:03:35,490 It's a mandatory component, 73 00:03:35,490 --> 00:03:38,393 needs to discover what's going on on the notes. 74 00:03:39,800 --> 00:03:42,490 So there we can see fact gathering was successful 75 00:03:42,490 --> 00:03:45,890 and it created user lisa on host ansible. 76 00:03:45,890 --> 00:03:48,750 So here we have lisa which is the value of the variable 77 00:03:48,750 --> 00:03:53,750 and host ansible1 is the value of the variable as well, 78 00:03:54,550 --> 00:03:56,540 the variable ansible host name. 79 00:03:56,540 --> 00:03:59,130 Now this is kind of a primitive structure. 80 00:03:59,130 --> 00:04:01,950 It's just printing ansible1 that's the first host name 81 00:04:01,950 --> 00:04:04,390 that it will encounter. 82 00:04:04,390 --> 00:04:06,720 And then you can see it works on ansible2 83 00:04:06,720 --> 00:04:08,130 as well as ansible1. 84 00:04:08,130 --> 00:04:09,720 Don't worry about it. 85 00:04:09,720 --> 00:04:11,750 We'll talk about when statements later 86 00:04:11,750 --> 00:04:14,460 and loops that allow you to print all host names, 87 00:04:14,460 --> 00:04:15,500 if you want to. 88 00:04:15,500 --> 00:04:17,760 I don't think it's really necessary now. 89 00:04:17,760 --> 00:04:19,770 What is necessary is that we see 90 00:04:19,770 --> 00:04:22,380 the variable is substituted. 91 00:04:22,380 --> 00:04:24,210 Now there's another way of running a playbook 92 00:04:24,210 --> 00:04:25,470 with variables. 93 00:04:25,470 --> 00:04:30,470 That is ansible-playbook variables_example -e user is bob. 94 00:04:31,593 --> 00:04:33,060 What are we doing here? 95 00:04:33,060 --> 00:04:35,940 We are overwriting the variable on the commands line. 96 00:04:35,940 --> 00:04:39,290 That goes for any option that you use in Ansible. 97 00:04:39,290 --> 00:04:41,430 If you define the option on the command line, 98 00:04:41,430 --> 00:04:44,150 it has higher precedence and it'll always win. 99 00:04:44,150 --> 00:04:47,663 So in this case, user will be bob and not lisa. 100 00:04:49,210 --> 00:04:51,030 That's a golden rule in Ansible. 101 00:04:51,030 --> 00:04:53,283 The most specific setting will always win. 102 00:04:55,040 --> 00:04:58,320 As you can see, create user bob on host ansible1. 103 00:04:58,320 --> 00:05:00,500 So user bob is what is created. 104 00:05:00,500 --> 00:05:04,870 Did not exist yet, so we see it changed and it changed. 105 00:05:04,870 --> 00:05:08,593 So this is what you need to know about basic variable usage.