1 00:00:06,960 --> 00:00:09,880 - So we have already talked a little bit about register. 2 00:00:09,880 --> 00:00:13,060 Register is particularly useful in combination 3 00:00:13,060 --> 00:00:14,460 with when statements. 4 00:00:14,460 --> 00:00:16,320 Let's go check it out. 5 00:00:16,320 --> 00:00:19,700 So you can use register to store the output of a command 6 00:00:19,700 --> 00:00:21,490 and address it as a variable. 7 00:00:21,490 --> 00:00:23,660 And next, you can use the result of the command 8 00:00:23,660 --> 00:00:25,750 in a conditional or in a loop, 9 00:00:25,750 --> 00:00:27,860 and that's exactly what I want to show you 10 00:00:27,860 --> 00:00:29,623 in the next couple of examples. 11 00:00:31,210 --> 00:00:34,564 So the first example is command output test 12 00:00:34,564 --> 00:00:38,833 and we are going to run that on ansible1. 13 00:00:42,211 --> 00:00:43,690 So what are we going to do? 14 00:00:43,690 --> 00:00:47,670 Well, the command that we are running is vgs centos. 15 00:00:47,670 --> 00:00:51,700 So this is running vgs, that's a Linux command, 16 00:00:51,700 --> 00:00:54,240 that checks if volume groups are available 17 00:00:54,240 --> 00:00:57,430 and it is running vgs on centos. 18 00:00:57,430 --> 00:01:00,340 So this command will give you some useful information 19 00:01:00,340 --> 00:01:02,750 if the volume group exists, 20 00:01:02,750 --> 00:01:06,920 and if it doesn't exist it'll give you other information. 21 00:01:06,920 --> 00:01:08,050 In order to work with it, 22 00:01:08,050 --> 00:01:10,990 we register the complete output of the command 23 00:01:10,990 --> 00:01:14,370 in a variable, and this variable is vgout. 24 00:01:14,370 --> 00:01:17,206 I do want this playbook to continue 25 00:01:17,206 --> 00:01:19,880 even if there is no result 26 00:01:20,750 --> 00:01:24,400 and that is why you can see ignore_errors is true. 27 00:01:24,400 --> 00:01:27,440 Notice that ignore_errors can be set in a play header, 28 00:01:27,440 --> 00:01:29,540 can also be set in a task header. 29 00:01:29,540 --> 00:01:31,040 Here it has a task scope. 30 00:01:31,040 --> 00:01:34,610 So only for this task, if it has an error, we continue. 31 00:01:34,610 --> 00:01:36,240 If all the tasks have an error 32 00:01:36,240 --> 00:01:39,100 then the playbook execution will stop 33 00:01:39,100 --> 00:01:43,900 and produce a normal error behavior that we've seen before. 34 00:01:43,900 --> 00:01:46,520 Next, I'm using show variable value. 35 00:01:46,520 --> 00:01:50,020 I like using debug especially if you are using register. 36 00:01:50,020 --> 00:01:51,770 Register can give you results 37 00:01:51,770 --> 00:01:54,010 that you would never have expected before. 38 00:01:54,010 --> 00:01:56,170 So it's a wise idea to go check out 39 00:01:56,170 --> 00:01:58,510 what exactly register is giving you. 40 00:01:58,510 --> 00:02:01,030 So we have debug var vgout. 41 00:02:01,030 --> 00:02:06,030 It'll show you the values that are in the register variable. 42 00:02:06,340 --> 00:02:08,580 And then, we are going to print a message 43 00:02:08,580 --> 00:02:10,490 and we print a message. 44 00:02:10,490 --> 00:02:11,560 Again, we see debug. 45 00:02:11,560 --> 00:02:13,690 The message is vg does not exist 46 00:02:13,690 --> 00:02:17,940 when "not found" in vgout.standard error. 47 00:02:17,940 --> 00:02:20,132 So what's happening here? 48 00:02:20,132 --> 00:02:23,210 We are looking for the string text "not found" 49 00:02:23,210 --> 00:02:27,425 in the registered variable vgout.standard error. 50 00:02:27,425 --> 00:02:30,646 A notice by the way that in a when statement, 51 00:02:30,646 --> 00:02:33,150 you do not have to use the curly braces 52 00:02:33,150 --> 00:02:35,270 to refer to the variables. 53 00:02:35,270 --> 00:02:39,660 It's a little bit confusing, but it's just the rule 54 00:02:39,660 --> 00:02:42,600 when using variables in when statements. 55 00:02:42,600 --> 00:02:44,250 Let's go see how this is working. 56 00:02:52,170 --> 00:02:54,870 So here we can see the fact gathering. 57 00:02:54,870 --> 00:02:58,550 You can see that the test for the VG existence is failing. 58 00:02:58,550 --> 00:03:00,470 And then, you can see that basically 59 00:03:00,470 --> 00:03:02,270 everything that you see right here 60 00:03:02,270 --> 00:03:07,270 is in a nicely printed format in the variable vgout. 61 00:03:07,982 --> 00:03:10,390 So vgout is showing you the commands. 62 00:03:10,390 --> 00:03:13,830 It is showing you the standard out. 63 00:03:13,830 --> 00:03:15,210 Oh, there is no standard out. 64 00:03:15,210 --> 00:03:17,396 It is showing you the standard error, 65 00:03:17,396 --> 00:03:20,060 nicely formatted in lines 66 00:03:20,060 --> 00:03:22,960 and the standard error all by itself. 67 00:03:22,960 --> 00:03:27,407 And then, we get the message "vg does not exist". 68 00:03:27,407 --> 00:03:29,933 Now, let's rewrite this playbook 69 00:03:29,933 --> 00:03:33,460 to the situation where the volume group does exist. 70 00:03:33,460 --> 00:03:38,460 So quick command on ansible1 -a vgs. 71 00:03:41,830 --> 00:03:43,480 And there, we can see that the name 72 00:03:43,480 --> 00:03:44,810 of the volume group is rl, 73 00:03:44,810 --> 00:03:48,450 rl for Rocky Linux. 74 00:03:48,450 --> 00:03:53,303 So this time the command is vgs rl. 75 00:03:55,120 --> 00:03:56,870 There we go. 76 00:03:56,870 --> 00:03:58,383 We run it again. 77 00:04:01,170 --> 00:04:03,670 And, we can see this is kind of curious, 78 00:04:03,670 --> 00:04:05,440 test for VG existence. 79 00:04:05,440 --> 00:04:06,660 I'm learning a command 80 00:04:06,660 --> 00:04:09,060 and nevertheless I'm getting changed, 81 00:04:09,060 --> 00:04:10,600 that's one of the weird artifacts 82 00:04:10,600 --> 00:04:12,683 of using the command model. 83 00:04:12,683 --> 00:04:14,250 We'll talk about it later. 84 00:04:14,250 --> 00:04:15,870 There's a change to when statement 85 00:04:15,870 --> 00:04:17,950 that you can use to influence that. 86 00:04:17,950 --> 00:04:19,600 Here we can see the command. 87 00:04:19,600 --> 00:04:21,330 Here we can see the standard out 88 00:04:21,330 --> 00:04:25,410 and the standard out actually does contain rl. 89 00:04:25,410 --> 00:04:26,270 There we go. 90 00:04:26,270 --> 00:04:27,694 We're looking for the string 91 00:04:27,694 --> 00:04:30,790 or the partial string to appear in the standard out. 92 00:04:30,790 --> 00:04:33,370 So the print methods can be skipped. 93 00:04:33,370 --> 00:04:38,000 I have another example which is register_command. 94 00:04:38,834 --> 00:04:41,100 So what are we doing here? 95 00:04:41,100 --> 00:04:42,770 We are prompting for a username. 96 00:04:42,770 --> 00:04:45,084 Oh, if we are prompting for a username 97 00:04:45,084 --> 00:04:50,083 then definitely I want private to be set to no 98 00:04:50,083 --> 00:04:54,215 because by default vars_prompt will not show the results 99 00:04:54,215 --> 00:04:56,340 of what the user is typing, 100 00:04:56,340 --> 00:04:58,290 that's not very user friendly 101 00:04:58,290 --> 00:05:01,710 and there's no reason to make this username private. 102 00:05:01,710 --> 00:05:03,350 So let's show it. 103 00:05:03,350 --> 00:05:08,350 Then, we are using the shell command cat/etc/passwd 104 00:05:08,760 --> 00:05:11,410 and we are register the output of that command 105 00:05:11,410 --> 00:05:13,600 on passwd_contents. 106 00:05:13,600 --> 00:05:17,293 Next, we have the debug module again on passwd_contents. 107 00:05:18,870 --> 00:05:19,950 And then, 108 00:05:19,950 --> 00:05:23,690 we are printing "passwd contains user {{username}}". 109 00:05:23,690 --> 00:05:26,870 So that's the username that was created in vars_prompt. 110 00:05:26,870 --> 00:05:27,973 And then, we have the when statement 111 00:05:27,973 --> 00:05:30,400 and the when statement is a curious one, 112 00:05:30,400 --> 00:05:32,738 passwd_content.stdout. 113 00:05:32,738 --> 00:05:34,658 That is what we are looking for. 114 00:05:34,658 --> 00:05:39,216 And next, we are using the Python function find username. 115 00:05:39,216 --> 00:05:40,049 This is Python. 116 00:05:40,049 --> 00:05:41,602 This is a Python command 117 00:05:41,602 --> 00:05:46,435 that is used on the standard out on the register variable. 118 00:05:47,580 --> 00:05:51,710 You can see that because username between braces, 119 00:05:51,710 --> 00:05:55,690 that's one notation of variables we haven't seen before, 120 00:05:55,690 --> 00:05:57,440 but the username between braces 121 00:05:57,440 --> 00:06:00,220 that's the Python way to refer to a variable 122 00:06:00,220 --> 00:06:03,160 and we look for non equals minus one. 123 00:06:03,160 --> 00:06:04,570 Hey, what is this is minus one? 124 00:06:04,570 --> 00:06:08,370 Well, you should know that if this Python find function 125 00:06:08,370 --> 00:06:09,780 will find the username, 126 00:06:09,780 --> 00:06:11,390 it'll give a byte offset 127 00:06:11,390 --> 00:06:15,857 like 386 if the user was find starting at byte 386. 128 00:06:17,050 --> 00:06:18,920 And if user was not found, 129 00:06:18,920 --> 00:06:20,653 then it'll give a minus one. 130 00:06:21,660 --> 00:06:26,160 We want the message to print "passwd contains user" 131 00:06:26,160 --> 00:06:27,802 when we get a byte offset. 132 00:06:27,802 --> 00:06:30,640 We don't know the byte offset beforehand 133 00:06:30,640 --> 00:06:32,570 but we do know if the user was found, 134 00:06:32,570 --> 00:06:34,030 we do not see minus one 135 00:06:34,030 --> 00:06:36,643 and that is what's happening right here. 136 00:06:36,643 --> 00:06:38,113 So let's run it. 137 00:06:46,140 --> 00:06:49,930 Let's first run it on user, user ansible, 138 00:06:49,930 --> 00:06:51,713 that's a user that does exist. 139 00:06:53,300 --> 00:06:57,220 And there we can see, we get passwd contains user ansible. 140 00:06:57,220 --> 00:06:59,160 Of course it contains user ansible. 141 00:06:59,160 --> 00:07:01,070 Just for your convenience, 142 00:07:01,070 --> 00:07:02,690 we can see the standard out, 143 00:07:02,690 --> 00:07:04,190 nicely formatted in lines 144 00:07:04,190 --> 00:07:06,130 which makes it a little bit more readable. 145 00:07:06,130 --> 00:07:07,600 There we have the lines 146 00:07:07,600 --> 00:07:09,760 and we can see all the other details 147 00:07:09,760 --> 00:07:11,909 that is just interesting to check it up. 148 00:07:11,909 --> 00:07:15,250 But this debug of the entire registered variable 149 00:07:15,250 --> 00:07:17,620 obviously has no real function. 150 00:07:17,620 --> 00:07:19,870 The only function is to clarify 151 00:07:19,870 --> 00:07:22,720 to yourself what is happening. 152 00:07:22,720 --> 00:07:27,720 Now, let me use it again with a user that does not exist 153 00:07:30,070 --> 00:07:32,880 and there we get a skipping and a skipping. 154 00:07:32,880 --> 00:07:35,470 It's still printing the entire variable 155 00:07:35,470 --> 00:07:38,920 that is because we are looking at the register okay? 156 00:07:38,920 --> 00:07:43,350 The register is just printing the output of etc/passwd 157 00:07:43,350 --> 00:07:45,661 and we don't do anything conditional on that. 158 00:07:45,661 --> 00:07:50,180 But here in this last task in the playbook, 159 00:07:50,180 --> 00:07:53,839 let me show you again, in this last task in the playbook, 160 00:07:53,839 --> 00:07:56,890 the when statement is find(username) 161 00:07:56,890 --> 00:07:58,710 should not return a minus one. 162 00:07:58,710 --> 00:08:02,370 Well, Caroline doesn't exist so we do get a minus one, 163 00:08:02,370 --> 00:08:04,190 and that is why we get a skipping, 164 00:08:04,190 --> 00:08:06,350 and that is how you can use register. 165 00:08:06,350 --> 00:08:08,670 Register is an awesome command, 166 00:08:08,670 --> 00:08:11,640 and you are likely going to use it a lot 167 00:08:11,640 --> 00:08:13,250 when working with ansible.