1 00:00:06,830 --> 00:00:08,920 - Now that we've talked about the fail module, 2 00:00:08,920 --> 00:00:11,960 we should also briefly cover the assert module. 3 00:00:11,960 --> 00:00:14,390 So the assert module is a module that you can use 4 00:00:14,390 --> 00:00:17,510 to show a message on success as well as on failure. 5 00:00:17,510 --> 00:00:19,445 It's a bit like the fail module 6 00:00:19,445 --> 00:00:21,760 but it has more advanced options. 7 00:00:21,760 --> 00:00:22,593 Let me show you. 8 00:00:24,320 --> 00:00:29,297 So the first playbook I want to show you is assert size 9 00:00:30,550 --> 00:00:32,910 but the wrong version of it. 10 00:00:32,910 --> 00:00:35,360 I made it easy to recognize which one is working, 11 00:00:35,360 --> 00:00:37,910 which one is not, because sometimes I like 12 00:00:37,910 --> 00:00:40,240 including playbooks that aren't doing 13 00:00:40,240 --> 00:00:42,210 what you expect them to do. 14 00:00:42,210 --> 00:00:45,420 So what this is doing, this is using VARs prompt, 15 00:00:45,420 --> 00:00:48,380 I'm going into local host because there is no reason 16 00:00:48,380 --> 00:00:50,860 to actually go out to another host. 17 00:00:50,860 --> 00:00:55,830 And then the playbook is checking if file size is valid 18 00:00:55,830 --> 00:00:59,640 and in order to do so using assert, so assert that. 19 00:00:59,640 --> 00:01:02,620 And then you can list a number of conditions. 20 00:01:02,620 --> 00:01:06,053 Condition number one, file size smaller than 100. 21 00:01:06,053 --> 00:01:08,560 This is an integer, right? 22 00:01:08,560 --> 00:01:09,610 It's not between quotes, 23 00:01:09,610 --> 00:01:13,073 then we have file size greater than one. 24 00:01:13,073 --> 00:01:15,650 We do a fail message file size must be 25 00:01:15,650 --> 00:01:17,580 and we do a success message. 26 00:01:17,580 --> 00:01:19,920 In case of failure, it will abort, 27 00:01:19,920 --> 00:01:22,140 in case of success, it will continue. 28 00:01:22,140 --> 00:01:26,150 And if it continues, it creates a file with this size. 29 00:01:26,150 --> 00:01:30,130 Notice that it's using bytes instead of megabytes, 30 00:01:30,130 --> 00:01:31,730 who cares about the actual size. 31 00:01:31,730 --> 00:01:34,250 But what matters is the assertion. 32 00:01:34,250 --> 00:01:39,150 So let's run it, ansible-playbook on assertsize-wrong, 33 00:01:42,000 --> 00:01:44,440 and let's give it 50. 34 00:01:44,440 --> 00:01:45,533 That should be okay. 35 00:01:46,600 --> 00:01:47,750 And what are we getting? 36 00:01:47,750 --> 00:01:50,030 Oh, that is interesting, we are getting 37 00:01:50,030 --> 00:01:53,404 the conditional check file size smaller than 100 failed. 38 00:01:53,404 --> 00:01:58,404 The error was unexpected templating error 39 00:01:59,290 --> 00:02:03,110 and not supported between instances of string and integer. 40 00:02:03,110 --> 00:02:04,360 Now what is the thing? 41 00:02:04,360 --> 00:02:09,360 The thing is that if you enter a value using VARs prompt, 42 00:02:09,890 --> 00:02:11,210 we get a string. 43 00:02:11,210 --> 00:02:13,780 If you want to do greater than smaller than operations 44 00:02:13,780 --> 00:02:16,550 on a variable, it needs to be an integer. 45 00:02:16,550 --> 00:02:21,550 And that is not the case here, the variable type is wrong 46 00:02:21,900 --> 00:02:25,360 and that is why we need to do something in addition. 47 00:02:25,360 --> 00:02:28,350 So let me show you the contents of assert size 48 00:02:28,350 --> 00:02:31,550 so that we can investigate what is different in there. 49 00:02:31,550 --> 00:02:32,609 Now what do we see? 50 00:02:32,609 --> 00:02:35,350 We see that the variable is defined 51 00:02:35,350 --> 00:02:38,130 but the variable is defined by int, 52 00:02:38,130 --> 00:02:41,180 this is passing through the integer filter. 53 00:02:41,180 --> 00:02:44,360 So this will make sure that the file size variable 54 00:02:44,360 --> 00:02:46,510 is interpreted as an integer. 55 00:02:46,510 --> 00:02:48,500 Notice that this filter is not changing 56 00:02:48,500 --> 00:02:49,790 the contents of the variable, 57 00:02:49,790 --> 00:02:52,580 it's just changing the interpretation. 58 00:02:52,580 --> 00:02:55,670 And we do the same for the other file size 59 00:02:55,670 --> 00:03:00,670 and now we can try again and that should be more successful. 60 00:03:00,740 --> 00:03:05,740 And we also include this private, no, 61 00:03:05,820 --> 00:03:09,230 no need to hide the actual results. 62 00:03:09,230 --> 00:03:11,863 So there we go, I'm going for the 50. 63 00:03:12,960 --> 00:03:15,800 And in the 50 we see file size is good, 64 00:03:15,800 --> 00:03:19,490 let's continue, create a file and change localhost 65 00:03:19,490 --> 00:03:23,440 and now I'm going for 5,000 which will be wrong 66 00:03:23,440 --> 00:03:27,188 and there you go, the assertion is having it fail. 67 00:03:27,188 --> 00:03:30,910 So that is what is really nice about assert. 68 00:03:30,910 --> 00:03:34,560 Assert, a little bit like block and rescue and always 69 00:03:34,560 --> 00:03:36,920 is just another way of implementing something 70 00:03:36,920 --> 00:03:39,620 that looks like an if then else statement. 71 00:03:39,620 --> 00:03:42,000 And you're going to need them in Ansible as well 72 00:03:42,000 --> 00:03:44,893 and that is what makes assert really cool.