1 00:00:00,690 --> 00:00:06,420 In this video, we are going to learn the conditions that can be applied in Ansible playbooks. 2 00:00:06,450 --> 00:00:07,830 Condition execution. 3 00:00:07,830 --> 00:00:13,800 Allow Ansible to take actions on its own based on certain conditions. 4 00:00:13,800 --> 00:00:22,890 So this becomes a the playbook becomes a lot more logical and smarter when we apply conditions inside 5 00:00:22,890 --> 00:00:23,340 of it. 6 00:00:23,520 --> 00:00:29,010 Under condition, certain values must be met before executing a task. 7 00:00:29,460 --> 00:00:36,900 We can use the vendor statement to make ansible automation more smart for our conditions. 8 00:00:37,140 --> 00:00:44,310 So for those of you who have worked with programming languages like Shell scripting or any other programming 9 00:00:44,310 --> 00:00:51,960 languages, I'm sure you're aware for conditions of use if then statement meaning if this happens, 10 00:00:51,960 --> 00:00:53,820 then do this. 11 00:00:54,270 --> 00:01:01,290 Similarly, we are going to use one statement within Ansible for our conditions. 12 00:01:01,500 --> 00:01:07,470 Let's take a look at our example where we could have then statement applied. 13 00:01:07,470 --> 00:01:09,540 So here is an example playbook. 14 00:01:09,720 --> 00:01:11,460 The name is just a playbook. 15 00:01:11,460 --> 00:01:19,110 Description host equal the local host and the task is going to start a service, any service for that 16 00:01:19,110 --> 00:01:19,590 matter. 17 00:01:19,590 --> 00:01:24,660 It doesn't really I don't really care, but it's just an example now. 18 00:01:25,520 --> 00:01:27,390 Here is the condition. 19 00:01:27,420 --> 00:01:36,620 Start a service only if when A equals equal to B, if it is, then service the name of service and the 20 00:01:36,620 --> 00:01:37,760 state of the service. 21 00:01:38,330 --> 00:01:40,700 See, that's where the condition comes in. 22 00:01:40,730 --> 00:01:50,420 If the condition is not met or it's not suffice the requirement, then the task will not run. 23 00:01:51,050 --> 00:01:51,590 All right. 24 00:01:51,590 --> 00:01:55,130 Let's take a look at the real life example. 25 00:01:55,400 --> 00:01:58,010 Here we have a playbook. 26 00:01:58,740 --> 00:02:04,800 The name of the playbook is to install Apache Server, which is one of our favorite application to install 27 00:02:04,800 --> 00:02:05,700 and work with. 28 00:02:05,910 --> 00:02:09,630 And the host we wanted to do it is against our local host. 29 00:02:09,990 --> 00:02:17,970 All right, then we have a tasks and the name of the first task is to install Apache on a Bantu server. 30 00:02:18,750 --> 00:02:19,520 All right. 31 00:02:19,530 --> 00:02:27,100 And the, and the Ansible module we will use for a Bantu is apt or its apt dash kit. 32 00:02:27,690 --> 00:02:34,830 Once we have that module enabled for it to install will install Apache to package in a addition to our 33 00:02:34,830 --> 00:02:35,460 Debian. 34 00:02:35,460 --> 00:02:38,610 It's not a HTTP feed, it's Apache two. 35 00:02:38,850 --> 00:02:46,650 Then it will take the same option state and we'll put it present and then comes in the condition. 36 00:02:46,650 --> 00:02:55,560 The condition is you should install this Apache only if or when the ansible underscore ohs underscore 37 00:02:55,590 --> 00:02:58,440 family equal equal a bun to. 38 00:02:58,500 --> 00:03:04,950 Now you're probably wondering what is underscore what is ansible underscore os underscore family is 39 00:03:04,950 --> 00:03:08,340 it's actually ansible built in variable. 40 00:03:09,600 --> 00:03:09,990 All right. 41 00:03:09,990 --> 00:03:14,550 So then the next task is install Apache on Centaur server. 42 00:03:15,000 --> 00:03:15,390 All right. 43 00:03:15,390 --> 00:03:24,390 For that, you have to use a Yum module and the name is http dx and the state is present. 44 00:03:24,420 --> 00:03:30,270 Now again we are going to apply the condition and for this one the condition has to be as low as family 45 00:03:30,270 --> 00:03:31,770 equals to be red hat. 46 00:03:32,070 --> 00:03:34,980 And right here, look at this. 47 00:03:34,980 --> 00:03:42,030 The when or the condition has to be vertically aligned with name and the module, and that's the only 48 00:03:42,030 --> 00:03:42,750 way we'll run. 49 00:03:42,750 --> 00:03:44,820 Otherwise it will give you an error message. 50 00:03:45,390 --> 00:03:48,030 Now you're probably thinking, hold on, hold on. 51 00:03:48,510 --> 00:03:53,000 Question how to get a list of all Ansible built in variables. 52 00:03:53,010 --> 00:03:59,070 See, I have provided you the ansible ohs underscore OS underscore family. 53 00:03:59,070 --> 00:04:04,350 But what if you wanted to use something else or different conditions? 54 00:04:04,710 --> 00:04:06,450 Well, that's a very good question. 55 00:04:06,540 --> 00:04:14,280 All the variables are gathered from the facts, and in order to get the list of the facts of a machine, 56 00:04:14,280 --> 00:04:24,090 you have to get through a command line by running Ansible and the name of the host minus M is the module 57 00:04:24,090 --> 00:04:26,760 option and the setup is the actual module. 58 00:04:26,760 --> 00:04:33,450 This will give you the list of all the facts, and then facts and variables are gathered from the facts. 59 00:04:33,990 --> 00:04:42,030 All right, let's go to our Ansible control node right here and we will go to let's log in. 60 00:04:44,860 --> 00:04:45,190 All right. 61 00:04:45,190 --> 00:04:47,710 We are an at sea as well playbook. 62 00:04:47,740 --> 00:04:48,670 Who am I? 63 00:04:48,700 --> 00:04:50,230 I am logged in as route. 64 00:04:50,500 --> 00:04:57,670 All right, so I already created this playbook in my playbook directory. 65 00:04:57,670 --> 00:04:59,260 So that's myself here. 66 00:04:59,260 --> 00:05:02,610 And right here is HTTP bi condition. 67 00:05:02,620 --> 00:05:09,220 That's the playbook that I will be using or running by. 68 00:05:09,980 --> 00:05:10,910 Condition. 69 00:05:11,030 --> 00:05:12,590 All right, so same thing. 70 00:05:12,590 --> 00:05:17,390 Whatever I have listed here in this playbook, it's exactly the same. 71 00:05:17,930 --> 00:05:21,710 Except this part I have put in Debian and Debian. 72 00:05:21,710 --> 00:05:24,890 You could change a bun too if you have a bunch to anyway. 73 00:05:25,580 --> 00:05:28,310 Pay attention on. 74 00:05:28,400 --> 00:05:35,660 Right here is the Apache two I'm sorry on the when Ansible and the score was families Debian and ansible 75 00:05:35,660 --> 00:05:41,900 OS underscore families debut now I put it in red hat at the bottom because I know this machine that 76 00:05:41,900 --> 00:05:49,280 I'm on, it's Red Hat so I want it to check this task first, this condition first, then go to the 77 00:05:49,280 --> 00:05:50,060 next one. 78 00:05:51,350 --> 00:05:51,740 All right. 79 00:05:51,740 --> 00:05:54,260 So we are doing a reinstalling HTTP. 80 00:05:54,290 --> 00:06:02,540 Of course, what we have to do, we have to make sure we remove the HTTP package from the system before 81 00:06:02,540 --> 00:06:03,110 we run it. 82 00:06:03,110 --> 00:06:06,320 So we know for sure that it did run successfully. 83 00:06:07,640 --> 00:06:08,120 All right. 84 00:06:08,120 --> 00:06:09,740 So it did run. 85 00:06:09,980 --> 00:06:10,790 It did remove it. 86 00:06:10,790 --> 00:06:11,840 Let's clear it now. 87 00:06:11,840 --> 00:06:14,630 Run, ansible dash playbook. 88 00:06:15,360 --> 00:06:20,280 And the name of the playbook, which is HTTP by condition. 89 00:06:20,460 --> 00:06:21,690 And let's run it. 90 00:06:22,530 --> 00:06:22,920 All right. 91 00:06:22,920 --> 00:06:28,920 So you see right here, install Apache Web server, which is the main name of the playbook gathering 92 00:06:28,920 --> 00:06:31,470 facts, which is right here for this local host. 93 00:06:31,650 --> 00:06:37,020 Now task right here, install Apache on Debian. 94 00:06:37,020 --> 00:06:39,120 So a bun two. 95 00:06:39,150 --> 00:06:44,860 So right here says skipping because this machine is not Debian nor a Bantu. 96 00:06:44,880 --> 00:06:46,350 So that's why it's skipping. 97 00:06:46,440 --> 00:06:53,220 Its went to the next task which is to install Apache on Santos change local host and there you go it 98 00:06:53,220 --> 00:06:53,800 says change. 99 00:06:53,830 --> 00:06:58,350 So it did work, skipped it and come to the right OS family. 100 00:06:58,380 --> 00:07:02,370 Now let's verify that it did install the HTTP PD. 101 00:07:03,160 --> 00:07:04,300 And it did. 102 00:07:04,630 --> 00:07:05,650 Beautiful. 103 00:07:05,950 --> 00:07:08,140 All right, so, see, that's how condition works. 104 00:07:08,170 --> 00:07:10,870 Now, let's move to the left and you'll see. 105 00:07:10,870 --> 00:07:17,310 We'll run as we'll unload close to see what are the variables we have available out of the box that 106 00:07:17,320 --> 00:07:23,440 will run ansible local host dash, dash or space setup. 107 00:07:24,640 --> 00:07:25,060 All right. 108 00:07:25,060 --> 00:07:26,950 So this is going to take a few seconds. 109 00:07:26,950 --> 00:07:35,050 When it comes back, you will notice these are all the facts and we could use that facts for our variables 110 00:07:35,050 --> 00:07:38,680 to define for our conditions. 111 00:07:38,680 --> 00:07:41,410 So you see Ansible underscore virtualization. 112 00:07:41,410 --> 00:07:48,940 This will text underscore host ansible underscore virtualization is a type equal virtual machine. 113 00:07:48,940 --> 00:07:50,950 Same thing if you go up. 114 00:07:51,900 --> 00:07:53,550 These are all the variables. 115 00:07:54,300 --> 00:07:58,200 Ansible underscore userspace bits right here. 116 00:07:58,200 --> 00:08:02,280 You'll notice this is ansible underscore real underscore user. 117 00:08:03,220 --> 00:08:03,850 All right. 118 00:08:03,850 --> 00:08:05,680 These are all the variables. 119 00:08:05,710 --> 00:08:09,010 Let's go more up and you'll see right here. 120 00:08:09,010 --> 00:08:12,600 It says Ansible, underscore OS, underscore family is red hat. 121 00:08:12,610 --> 00:08:17,050 This is that variable that I got it from this list. 122 00:08:17,380 --> 00:08:24,550 You could also get the whole bunch of facts that could be used for variables and eventually for our 123 00:08:24,550 --> 00:08:25,420 conditions. 124 00:08:25,810 --> 00:08:26,110 All right. 125 00:08:26,110 --> 00:08:30,100 Spend some time to go through them and you will know exactly how to use them.