1 00:00:00,780 --> 00:00:03,750 In this video, we are going to learn variables. 2 00:00:04,020 --> 00:00:11,850 Variables are like containers that hold the defined value which can be used repetitively. 3 00:00:12,390 --> 00:00:12,720 All right. 4 00:00:12,720 --> 00:00:18,330 So there are a few important things to remember about variables before we move on. 5 00:00:19,210 --> 00:00:24,870 Name of a variable can include the letters, numbers and underscores. 6 00:00:24,900 --> 00:00:27,630 Name should always start with a letter. 7 00:00:28,860 --> 00:00:35,940 Variable cannot have spaces, dots or hyphen in the in its name. 8 00:00:36,630 --> 00:00:41,760 Variables can be defined inside of inventory files as well. 9 00:00:42,330 --> 00:00:42,630 All right. 10 00:00:42,630 --> 00:00:48,240 So let's take an example of how a variable would look like in a playbook. 11 00:00:48,270 --> 00:00:51,030 So here we have our first playbook. 12 00:00:51,330 --> 00:00:57,530 So let's say in a playbook, we define as a YAML file the name of the YAML file. 13 00:00:57,540 --> 00:01:00,510 Sorry, the name of the playbook is install some package. 14 00:01:00,930 --> 00:01:02,820 The host is going to install it against. 15 00:01:02,820 --> 00:01:05,550 And right now we define the variables. 16 00:01:05,820 --> 00:01:09,840 This is the name of the package, Cece package. 17 00:01:09,840 --> 00:01:12,330 This is the variable via defining. 18 00:01:12,450 --> 00:01:19,590 And the package name that we wanted to install is Cece, Key Pedley and near SSM. 19 00:01:20,160 --> 00:01:22,110 I don't know what the hell is that? 20 00:01:22,110 --> 00:01:30,990 But you see how difficult it is and very likely when you are typing it in you can make a mistake and 21 00:01:30,990 --> 00:01:31,920 misspell it. 22 00:01:32,310 --> 00:01:40,200 So when you have weird spellings or longer names in your packages or services or anything like that, 23 00:01:40,200 --> 00:01:43,630 then the best thing to do is to put that in a variable. 24 00:01:43,650 --> 00:01:50,670 So what I did is I took this whole big name and I put it in a variable SES just took the first three 25 00:01:50,670 --> 00:01:53,370 letters and put the package S'S package. 26 00:01:53,460 --> 00:01:53,820 All right. 27 00:01:53,820 --> 00:01:56,250 The task is to install the package. 28 00:01:56,280 --> 00:01:57,690 We'll use the module. 29 00:01:57,690 --> 00:02:01,620 The name for the package is not this entire thing. 30 00:02:01,620 --> 00:02:04,650 Now it just put the variable and variable. 31 00:02:04,650 --> 00:02:05,820 To define the variable. 32 00:02:05,820 --> 00:02:07,050 You have to put a double code. 33 00:02:07,080 --> 00:02:07,950 Curly braces. 34 00:02:07,950 --> 00:02:11,520 Curly braces, the package, the variable name. 35 00:02:11,520 --> 00:02:12,390 Curly braces. 36 00:02:12,390 --> 00:02:13,230 Curly braces. 37 00:02:13,230 --> 00:02:14,160 Close. 38 00:02:15,000 --> 00:02:17,270 Double coat closed state prison. 39 00:02:17,280 --> 00:02:19,410 Same thing when you want to start the service. 40 00:02:19,410 --> 00:02:25,140 The service module, the name of the service, which is of course the package name and the state. 41 00:02:25,140 --> 00:02:25,950 You want to start it. 42 00:02:25,950 --> 00:02:33,360 So if you wanted to put it in a real life example, let's put it in our all time favorite HTTP package. 43 00:02:33,510 --> 00:02:40,350 So let's say we have a package installation playbook if you want to do it on all host and be defining 44 00:02:40,350 --> 00:02:49,470 the variables always variables are defined with vars name host and variables are all have to be vertically 45 00:02:49,470 --> 00:02:50,610 aligned together. 46 00:02:51,150 --> 00:02:58,770 Then the name of the variable I picked pack as in short package you can pick any variable and the package 47 00:02:58,770 --> 00:02:59,860 name in http pd. 48 00:02:59,940 --> 00:03:07,260 So wherever I have to put it in hpdi, i'll just put pack and pack and that it knows as soon as you 49 00:03:07,260 --> 00:03:13,440 define it with this variable codebases, it will go back to variable and ask, hey, can you tell me 50 00:03:13,440 --> 00:03:14,100 the name? 51 00:03:14,100 --> 00:03:17,460 And it says, Oh, okay, the name of the DPD, let me apply here. 52 00:03:17,550 --> 00:03:18,780 So that's how it works. 53 00:03:18,780 --> 00:03:24,270 So now we will create this playbook in our Ansible control node. 54 00:03:25,020 --> 00:03:27,510 All right, here is our control node. 55 00:03:28,290 --> 00:03:29,460 Let's log in. 56 00:03:33,140 --> 00:03:39,210 I am an Etsy Ansible Playbooks directory and I am logged in as root. 57 00:03:39,330 --> 00:03:39,680 Okay. 58 00:03:39,740 --> 00:03:46,400 Now I have already created this YAML file for this variable package to save some time, so I'm going 59 00:03:46,400 --> 00:03:46,880 to do it. 60 00:03:47,210 --> 00:03:50,470 Vim http bi variables. 61 00:03:50,480 --> 00:03:55,130 This is the name I picked and pay close attention right here. 62 00:03:56,380 --> 00:04:00,340 The defined the G.M. name package installation. 63 00:04:00,670 --> 00:04:03,480 Host Where do we want to install it in the local. 64 00:04:03,490 --> 00:04:04,210 Host Right here. 65 00:04:04,240 --> 00:04:09,230 Variables pack and install http pd task install package. 66 00:04:09,250 --> 00:04:09,760 Yum. 67 00:04:09,760 --> 00:04:14,410 Is the module and the name of the package going to get it from the variable? 68 00:04:14,950 --> 00:04:17,620 Same thing to start the service name of the service. 69 00:04:17,620 --> 00:04:19,030 We're going to get the variable. 70 00:04:19,510 --> 00:04:20,080 All right. 71 00:04:20,080 --> 00:04:22,900 Take your time to write this playbook now. 72 00:04:23,720 --> 00:04:25,280 Once you're done, let's save it. 73 00:04:27,550 --> 00:04:33,670 And before we run it, let's check if we have a HTTP package installed. 74 00:04:34,570 --> 00:04:35,500 If you do. 75 00:04:35,530 --> 00:04:37,750 I'm sure you know what to do. 76 00:04:37,800 --> 00:04:42,580 You can remove a http d minus y to remove it. 77 00:04:42,700 --> 00:04:43,270 All right. 78 00:04:43,270 --> 00:04:46,330 So it is going to remove it and is removing right now. 79 00:04:46,330 --> 00:04:52,210 Once it is done, then we will run the Ansible playbook. 80 00:04:52,240 --> 00:05:01,720 Now clear the screen and simple dash playbook and playbook is http bi variable. 81 00:05:02,780 --> 00:05:03,190 Enter. 82 00:05:04,450 --> 00:05:08,110 And right here you see this package installation. 83 00:05:08,110 --> 00:05:11,110 This is the name against our local host. 84 00:05:11,800 --> 00:05:14,260 I had it all, but we will pick local host. 85 00:05:14,260 --> 00:05:15,070 That's fine. 86 00:05:15,610 --> 00:05:17,020 Installing package. 87 00:05:17,020 --> 00:05:21,910 We are up here at this point where we are going to install the package. 88 00:05:21,910 --> 00:05:22,450 All right. 89 00:05:22,450 --> 00:05:24,610 So the package is installing Yum! 90 00:05:24,610 --> 00:05:31,570 And on the local host and it's changed meaning it changed to and the change equal is two is because 91 00:05:31,570 --> 00:05:36,970 one it's the first install package and second one is to start the service. 92 00:05:37,180 --> 00:05:41,550 So let's check by running rpm key wa grep http pd. 93 00:05:41,560 --> 00:05:51,670 Yes, it is there system CTL, status, HTTP and active and services started as well. 94 00:05:51,820 --> 00:05:52,660 Excellent. 95 00:05:52,900 --> 00:05:53,440 All right. 96 00:05:53,440 --> 00:05:55,660 So now you notice one more thing. 97 00:05:55,720 --> 00:06:01,670 If you wanted to install another package, for example, you wanted to install NFS package, you wanted 98 00:06:01,670 --> 00:06:05,770 to FTP package, name D package, any package of your choice. 99 00:06:05,860 --> 00:06:09,790 All you have to do here is go to this same. 100 00:06:11,360 --> 00:06:21,260 ATP buy variable playbook and simply change the name of the package right here. 101 00:06:21,990 --> 00:06:25,420 Only you do not have the have to change the name here. 102 00:06:25,440 --> 00:06:27,290 You do not have to change the name here. 103 00:06:27,300 --> 00:06:35,190 And imagine if you had a very long playbook with repetitive entries of hpdi. 104 00:06:35,220 --> 00:06:37,530 How many times you had to do that change? 105 00:06:37,860 --> 00:06:40,620 Yes, it is quite cumbersome. 106 00:06:40,620 --> 00:06:49,260 So that's why variable are one of the greatest feature of Ansible that's going to help you define that 107 00:06:49,260 --> 00:06:52,440 initially, and then you could use it repetitively. 108 00:06:53,160 --> 00:06:54,540 All right, let's save it. 109 00:06:55,470 --> 00:07:01,740 Let's go back to our slide and go to the next examples that we have where we could use variable. 110 00:07:01,950 --> 00:07:07,890 In the second example, let's say if you wanted to copy file to a remote clients, you could put it 111 00:07:07,890 --> 00:07:12,960 in host and then define the variables right here and the variable would be source file. 112 00:07:12,960 --> 00:07:22,140 I just picked the name and the location of the source file, starting the task copying file the whole 113 00:07:22,140 --> 00:07:23,760 line you are source. 114 00:07:23,760 --> 00:07:30,030 Now you do not have to put or enter this entire absolute location. 115 00:07:30,060 --> 00:07:34,350 You just put in the variable source file and it will do it. 116 00:07:34,350 --> 00:07:35,640 The trick for you. 117 00:07:36,000 --> 00:07:36,420 All right. 118 00:07:36,420 --> 00:07:42,570 The third example, let's say if you wanted to create a file on local host, you define the variable 119 00:07:42,570 --> 00:07:44,190 with the file underscore name. 120 00:07:44,190 --> 00:07:45,960 This is the name of the variable. 121 00:07:46,380 --> 00:07:50,280 And then what's the actual name of the file? 122 00:07:50,280 --> 00:07:51,060 It's Kramer. 123 00:07:51,630 --> 00:07:52,260 All right. 124 00:07:52,260 --> 00:07:57,120 The task, the name, create file in temp directory, file state and so on. 125 00:07:57,120 --> 00:07:59,130 And the path, where do you want to put it? 126 00:07:59,130 --> 00:08:07,680 In the slash temp directory and in the temp directory, then you have to put Slash and the file name. 127 00:08:07,680 --> 00:08:14,400 By the way, you could confirm I believe it's slash temp slash the variable name, which is the file 128 00:08:14,400 --> 00:08:14,880 name. 129 00:08:14,880 --> 00:08:15,300 All right. 130 00:08:15,300 --> 00:08:16,950 So that's the third example. 131 00:08:16,950 --> 00:08:18,870 Let's go to the fourth example. 132 00:08:19,590 --> 00:08:22,200 Hello, our print Hello World. 133 00:08:22,200 --> 00:08:28,860 That's the fourth playbook on a whole host of variables we are defining and the name of the variable 134 00:08:29,160 --> 00:08:34,320 is say and it will again just the name I picked. 135 00:08:34,320 --> 00:08:36,780 What's inside of this say container? 136 00:08:36,780 --> 00:08:37,590 Hello world. 137 00:08:37,590 --> 00:08:45,930 Now imagine if you had a really long message then you would have to of course write that long message 138 00:08:46,170 --> 00:08:53,910 every time you have defined that module debug and its option with message simply you put that into variable 139 00:08:54,210 --> 00:09:00,150 and tell the debug module the message option to just put the variable. 140 00:09:00,150 --> 00:09:07,230 And that is just a say and it will say the whole world for a whole word for you. 141 00:09:07,920 --> 00:09:14,550 Okay, I'm sure now you know how variable works and how you can make a user benefit out of it.