1 00:00:07,170 --> 00:00:09,910 - In this video, I want to tell you about Register. 2 00:00:09,910 --> 00:00:12,940 Register is a nice way to automatically define 3 00:00:12,940 --> 00:00:15,073 your variables based on command result. 4 00:00:16,030 --> 00:00:18,130 So the thing of Register is that you can use it 5 00:00:18,130 --> 00:00:21,110 to store the result of a task, in a variable. 6 00:00:21,110 --> 00:00:24,200 It provides a very nice way to perform specific tasks 7 00:00:24,200 --> 00:00:27,960 only upon success or failure of another task. 8 00:00:27,960 --> 00:00:30,960 That means that it'll become really useful 9 00:00:30,960 --> 00:00:32,970 once we have talked about conditionals. 10 00:00:32,970 --> 00:00:35,910 But, as Register is about a way to create variables, 11 00:00:35,910 --> 00:00:38,060 I'd like to have a look at a quick example. 12 00:00:41,230 --> 00:00:44,520 So here is register.yaml and what do we have? 13 00:00:44,520 --> 00:00:46,340 Well, a very simple playbook. 14 00:00:46,340 --> 00:00:47,610 In this playbook we are using 15 00:00:47,610 --> 00:00:50,553 the shell command to run a command. 16 00:00:52,070 --> 00:00:54,640 Notice that there's no real reason to use shell here, 17 00:00:54,640 --> 00:00:56,620 could have used command as well. 18 00:00:56,620 --> 00:00:58,440 What it's all about, is register. 19 00:00:58,440 --> 00:01:02,500 Register stores the result of the shell module 20 00:01:02,500 --> 00:01:05,650 in a variable with an passwd_contents 21 00:01:05,650 --> 00:01:08,760 And next in debug, we are going to check 22 00:01:09,640 --> 00:01:12,343 this passwd_contents value. 23 00:01:15,530 --> 00:01:18,267 So ansible-playbook on register.yaml 24 00:01:19,250 --> 00:01:22,533 is doing this, it's gathering the facts. 25 00:01:24,700 --> 00:01:29,700 And there we go. Here we have the result of the register. 26 00:01:29,999 --> 00:01:33,780 So register defines its variable on every single host. 27 00:01:33,780 --> 00:01:38,110 So basically, it becomes a property of the host. 28 00:01:38,110 --> 00:01:41,600 And in this host, we can see that it contains 29 00:01:41,600 --> 00:01:44,180 detailed information about the success 30 00:01:44,180 --> 00:01:45,890 or failure of the exam. 31 00:01:45,890 --> 00:01:48,010 So we can see whether or not it changed. 32 00:01:48,010 --> 00:01:50,240 We can see it's, whether or not it's failed. 33 00:01:50,240 --> 00:01:52,390 We can see rc for the return code, 34 00:01:52,390 --> 00:01:54,780 which is the result of the command. 35 00:01:54,780 --> 00:01:56,247 We can see when it started. 36 00:01:56,247 --> 00:01:59,510 The stdout put, the stdout put in lines. 37 00:01:59,510 --> 00:02:02,860 And a nice thing is, you can address all of them 38 00:02:02,860 --> 00:02:07,860 like passwd_contents.rc is going to print the return code. 39 00:02:09,140 --> 00:02:12,073 You know what? Let's tweak this playbook a little bit. 40 00:02:14,580 --> 00:02:19,580 Now let me show you debug, msg, 41 00:02:19,800 --> 00:02:24,800 the status of the command is {{ passwd_contents.rc }} 42 00:02:33,550 --> 00:02:36,020 Notice, by the way, that there's no real reason 43 00:02:36,020 --> 00:02:38,290 why double quotes I used here. 44 00:02:38,290 --> 00:02:41,793 You might as well omit them, then it'll also work. 45 00:02:43,830 --> 00:02:48,020 So there we go, the status of the command is zero. 46 00:02:48,020 --> 00:02:51,610 Note by the way, that I'm using passwd_contents.rc 47 00:02:51,610 --> 00:02:54,017 and in case you are thinking, "Hey, (indistinct), 48 00:02:54,017 --> 00:02:55,950 "is that the right way of doing it?" 49 00:02:55,950 --> 00:02:58,647 Then I would say, "Good point!" 50 00:02:59,940 --> 00:03:01,710 This is how you should be doing it. 51 00:03:01,710 --> 00:03:04,100 Square bracket, single quote notation. 52 00:03:04,100 --> 00:03:06,730 But the doted notation is so common 53 00:03:06,730 --> 00:03:09,823 that you will find it still, every now and then.