1 00:00:00,660 --> 00:00:05,160 In this video, we will be creating our very first playbook. 2 00:00:05,940 --> 00:00:12,840 There are online Yammer tools you can use to create playbooks, and that is for those who do not have 3 00:00:12,840 --> 00:00:16,890 much experience with the editor or VM editor. 4 00:00:16,890 --> 00:00:23,580 So when you log into the Linux machine, you would have to use a VI or VIM attitude to create playbooks. 5 00:00:23,580 --> 00:00:30,030 But if you do not know how to use that, then the tools that are available for you are like online yaml 6 00:00:30,030 --> 00:00:34,100 tools dot com or code beautify dot org. 7 00:00:34,110 --> 00:00:41,760 You can also download tools and windows such as notepad plus plus create your YAML file and then you 8 00:00:41,760 --> 00:00:45,630 can copy it over to the Linux system. 9 00:00:46,050 --> 00:00:53,280 All right, so before we actually get into creating our very first playbook in our lab environment, 10 00:00:53,490 --> 00:01:00,660 the best thing to do is that we take a snapshot because we have installed ANSIBLE software. 11 00:01:00,660 --> 00:01:04,890 So we have taken the snapshot of we installed Linux already. 12 00:01:04,890 --> 00:01:10,110 Now we will take a second snapshot after we have completed the Ansible software install. 13 00:01:10,320 --> 00:01:16,140 So this way, if we make a mistake, we could go back to either the point when we did the Ansible software 14 00:01:16,140 --> 00:01:18,600 install or the Linux software install. 15 00:01:19,230 --> 00:01:19,740 All right. 16 00:01:19,950 --> 00:01:22,860 So for that, we'll go to our. 17 00:01:26,040 --> 00:01:27,780 Oracle virtual manager. 18 00:01:27,810 --> 00:01:31,320 And here we'll have a Linux install point. 19 00:01:31,350 --> 00:01:31,890 Okay. 20 00:01:31,920 --> 00:01:33,600 Our machine is running already. 21 00:01:33,600 --> 00:01:34,130 That's okay. 22 00:01:34,140 --> 00:01:43,320 You can leave it running and you can click on take and type in the name and Sybil install dash point. 23 00:01:43,350 --> 00:01:49,410 Now of course, you could pick any name that will remind you where or when you took the snapshot. 24 00:01:49,650 --> 00:01:50,100 All right. 25 00:01:50,100 --> 00:01:50,460 Click. 26 00:01:50,460 --> 00:01:51,090 Okay. 27 00:01:52,930 --> 00:01:53,350 All right. 28 00:01:53,350 --> 00:01:59,170 This is going to be a lot faster, of course, but not as fast as if the machine is powered off. 29 00:01:59,200 --> 00:01:59,500 All right. 30 00:01:59,500 --> 00:02:02,350 So now you have two snapshots here. 31 00:02:02,350 --> 00:02:05,760 You could go back to any one of them when you make a mistake. 32 00:02:05,770 --> 00:02:06,040 All right. 33 00:02:06,040 --> 00:02:06,850 So that is done. 34 00:02:06,850 --> 00:02:08,440 You go go ahead and close this. 35 00:02:08,890 --> 00:02:14,620 Now, let's go and see what first playbook we are writing. 36 00:02:14,620 --> 00:02:14,920 All right. 37 00:02:14,920 --> 00:02:17,030 The first thing we have to do is become root. 38 00:02:17,050 --> 00:02:22,960 Now, you could run playbook as a root user or a normal user. 39 00:02:23,350 --> 00:02:24,700 So it doesn't really matter. 40 00:02:24,730 --> 00:02:30,400 What matters is what commands or what function that playbook is going to do. 41 00:02:30,430 --> 00:02:35,350 If Playbook is going to update your system with the Yum! 42 00:02:35,350 --> 00:02:41,410 Update, then of course you need to have the playbook being run or created as a root. 43 00:02:42,040 --> 00:02:42,520 All right. 44 00:02:43,390 --> 00:02:47,770 What we will do is we'll create a directory in ETSI Ansible and we call it playbooks. 45 00:02:47,770 --> 00:02:50,980 This is where we keep all our playbooks throughout this course. 46 00:02:51,130 --> 00:02:57,220 And then we'll see into this playbook and we'll create our first YAML file and we'll name it first yaml 47 00:02:57,850 --> 00:03:00,820 once we vim the file are added. 48 00:03:01,060 --> 00:03:08,350 Edit The file will define dash dash dash three hyphens that will signify that this is a YAML file. 49 00:03:08,380 --> 00:03:12,670 Then we'll define the name of the playbook, which is my first playbook. 50 00:03:13,600 --> 00:03:14,530 Then comes the host. 51 00:03:14,530 --> 00:03:19,090 Where do you want to run this task or this playbook you want to? 52 00:03:19,090 --> 00:03:23,560 We will start off it just local host then the task what exactly you want it to do. 53 00:03:23,560 --> 00:03:29,920 If you notice the task is right below the indentation is right below name and host. 54 00:03:29,920 --> 00:03:31,990 That is that matters a lot. 55 00:03:31,990 --> 00:03:36,790 It cannot be before a few spaces before the host or name. 56 00:03:37,210 --> 00:03:42,040 So what the task is, the name of the task is test connectivity. 57 00:03:42,040 --> 00:03:47,650 And now if you pay close attention, the name, the hyphen is right below the task. 58 00:03:47,650 --> 00:03:54,220 The T, it can move to the right if you want to, and the corresponding module's is going to be right 59 00:03:54,220 --> 00:03:54,940 below it. 60 00:03:55,600 --> 00:04:02,620 You cannot have that hyphen before the spaces of the task or the T are. 61 00:04:02,620 --> 00:04:05,620 The module that we're going to run is the paying module. 62 00:04:05,620 --> 00:04:11,140 And again, you notice that panga module is right below the name of the task. 63 00:04:11,890 --> 00:04:14,740 All right, let's go into a Linux machine. 64 00:04:15,580 --> 00:04:17,440 Right here is a Linux machine. 65 00:04:18,430 --> 00:04:23,280 And before we do anything, let's create a snapshot, which we already did, by the way. 66 00:04:23,290 --> 00:04:23,820 Sorry. 67 00:04:23,830 --> 00:04:25,690 Yes, we have taken our snapshot. 68 00:04:25,720 --> 00:04:31,450 Now go ahead and become route, which I am already route. 69 00:04:31,450 --> 00:04:31,930 Yes. 70 00:04:31,930 --> 00:04:34,410 When I do ID it shows I am root. 71 00:04:34,460 --> 00:04:34,880 Okay. 72 00:04:35,080 --> 00:04:38,710 Let's make a directory at see and symbol. 73 00:04:42,050 --> 00:04:43,060 Playbooks. 74 00:04:43,070 --> 00:04:44,150 This is the directory. 75 00:04:44,150 --> 00:04:47,090 We will create all our playbooks. 76 00:04:48,090 --> 00:04:50,100 Let's seed into the directory. 77 00:04:52,930 --> 00:04:55,450 Let's create our first YAML file. 78 00:04:57,750 --> 00:05:00,090 Define that it says yaml file. 79 00:05:00,780 --> 00:05:05,850 But the name my first playbook. 80 00:05:05,880 --> 00:05:08,400 Now you could put this double code or no codes. 81 00:05:08,400 --> 00:05:09,630 It doesn't really matter now. 82 00:05:09,870 --> 00:05:16,050 When you hit enter, you're going to notice one cool thing of using them editor that it will bring you 83 00:05:16,050 --> 00:05:18,270 right below where it should be. 84 00:05:18,270 --> 00:05:20,280 And that is under the name. 85 00:05:20,280 --> 00:05:26,700 And right here you define the host where you want to run this YAML file against and we'll run it against 86 00:05:26,700 --> 00:05:27,540 the local host. 87 00:05:27,720 --> 00:05:30,540 Now, when you hit Enter, I'll bring you back to the host. 88 00:05:30,580 --> 00:05:35,610 We could leave a space, an empty line to make it look more neat and organized. 89 00:05:35,760 --> 00:05:37,110 Come down right here. 90 00:05:37,110 --> 00:05:38,580 You define tasks. 91 00:05:38,580 --> 00:05:41,670 What exactly you want this playbook to do the task. 92 00:05:41,670 --> 00:05:48,210 Now, when you hit enter, it will bring you the it will do the indentation for you and move your cursor 93 00:05:48,210 --> 00:05:48,930 to the right. 94 00:05:49,440 --> 00:05:54,870 Now you could go back and start your indentation right here if you want to. 95 00:05:54,870 --> 00:05:55,830 That's fine. 96 00:05:55,830 --> 00:06:00,090 Or you could let the editor decide where it's going to be. 97 00:06:00,090 --> 00:06:00,960 That's fine. 98 00:06:00,960 --> 00:06:01,710 Name? 99 00:06:02,600 --> 00:06:06,120 And what is the name of this playbook? 100 00:06:06,140 --> 00:06:06,800 Sorry. 101 00:06:06,830 --> 00:06:12,560 This tasks that task will test the connectivity of the local host. 102 00:06:13,360 --> 00:06:19,570 And the module that it will use to test the connectivity will be paying module and that's pretty much 103 00:06:19,570 --> 00:06:19,870 it. 104 00:06:19,910 --> 00:06:23,320 Escape and save the file. 105 00:06:23,830 --> 00:06:25,270 Now the file is saved. 106 00:06:25,300 --> 00:06:28,750 Now let's do a few checks. 107 00:06:29,910 --> 00:06:31,860 Check syntax of playbook. 108 00:06:31,890 --> 00:06:36,160 You have to check to see if there are no syntax errors. 109 00:06:36,180 --> 00:06:40,410 You can run this command or you could do a dry run, which is ansible playbook. 110 00:06:40,440 --> 00:06:45,780 Once the check is complete, then you can run the playbook by running the simple ansible playbook command. 111 00:06:45,960 --> 00:06:51,620 All right, let's go back to our Linux machine and we'll check the syntax first. 112 00:06:51,990 --> 00:06:54,960 That would be Ansible Dash playbook. 113 00:06:54,960 --> 00:06:57,480 Dash, dash syntax. 114 00:06:59,510 --> 00:07:02,990 Dash check first yaml file. 115 00:07:04,890 --> 00:07:05,340 All right. 116 00:07:05,340 --> 00:07:06,400 It's telling it. 117 00:07:06,420 --> 00:07:08,220 The host list is empty. 118 00:07:08,250 --> 00:07:09,540 Only localhost is available. 119 00:07:09,540 --> 00:07:10,170 That is fine. 120 00:07:10,210 --> 00:07:11,910 Is only telling you that they are. 121 00:07:11,910 --> 00:07:17,550 It's not seeing any host in the in the at ansible host file. 122 00:07:17,550 --> 00:07:18,390 So that's fine. 123 00:07:18,510 --> 00:07:20,910 Now let's do the same thing. 124 00:07:21,800 --> 00:07:28,730 Instead of syntax check will do just check which will run as if it's you're running the actual file, 125 00:07:28,730 --> 00:07:30,370 but it won't do anything. 126 00:07:30,380 --> 00:07:33,080 It would just tell you the syntax looks good and perfect. 127 00:07:33,320 --> 00:07:33,890 Nice. 128 00:07:33,890 --> 00:07:36,950 Now we will run the actual playbook. 129 00:07:36,950 --> 00:07:38,540 And that is Ansible. 130 00:07:42,410 --> 00:07:43,160 Sorry. 131 00:07:43,730 --> 00:07:45,590 Ansible Playbook. 132 00:07:45,590 --> 00:07:53,960 And if you are inside of the playbooks directory where the playbook is located, then you could simply 133 00:07:53,960 --> 00:07:55,700 put in the name of the playbook. 134 00:07:55,700 --> 00:07:59,210 If you are somewhere else, then you have to put in the absolute path. 135 00:08:00,260 --> 00:08:02,240 All right, let's run it now. 136 00:08:02,240 --> 00:08:06,140 It's going to check against the local host is going to peg it and it's going to tell it. 137 00:08:06,140 --> 00:08:07,040 Everything looks good. 138 00:08:07,040 --> 00:08:09,890 Local host task is good, test connectivity is good. 139 00:08:10,190 --> 00:08:11,240 All look good. 140 00:08:11,630 --> 00:08:18,590 Now let's go back to our VI editor and go to our playbook. 141 00:08:18,590 --> 00:08:27,320 And let's say if we move this one space back accidentally, right, and then come back down here and 142 00:08:27,320 --> 00:08:30,350 now we run our Ansible playbook. 143 00:08:31,760 --> 00:08:34,310 Right here, you see, it's giving you error message. 144 00:08:34,310 --> 00:08:37,310 The error appears to be in Etsy as well. 145 00:08:37,340 --> 00:08:42,620 Playbook first yaml, file line three, column two, but maybe elsewhere in the file depending on the 146 00:08:42,620 --> 00:08:45,740 exact center C indentation matters a lot. 147 00:08:45,740 --> 00:08:52,310 So if you go back to it and you move this one step over, then it should run fine. 148 00:08:52,790 --> 00:08:57,290 When you run it, it will give you that message that it's everything looks good. 149 00:08:57,290 --> 00:08:57,980 Same way. 150 00:08:57,980 --> 00:09:04,070 If you go back to the Vim editor and you move this right here and now you run it. 151 00:09:06,380 --> 00:09:11,150 It will give you that error message again because the pen is not aligned to the name. 152 00:09:11,150 --> 00:09:15,210 So for that, you can move this three, four steps back. 153 00:09:15,230 --> 00:09:15,770 That's fine. 154 00:09:15,770 --> 00:09:19,700 But Ping has to be exactly aligned where the name is now. 155 00:09:19,700 --> 00:09:21,950 You when you run it, it should be okay. 156 00:09:23,470 --> 00:09:24,060 There you go. 157 00:09:24,070 --> 00:09:25,420 See, it is running fine. 158 00:09:25,420 --> 00:09:26,290 There are no. 159 00:09:26,290 --> 00:09:27,430 H's beautiful. 160 00:09:27,700 --> 00:09:32,320 All right, so that's what our first Ansible playbook is. 161 00:09:32,320 --> 00:09:33,280 Now, we. 162 00:09:33,280 --> 00:09:39,400 We wrote the playbook, but if you didn't want to write the playbook, if you remember, we ran the 163 00:09:39,400 --> 00:09:47,950 Peng module without a playbook, and it was just Ansible, and we ran it against local host dash m dash 164 00:09:47,950 --> 00:09:48,700 module. 165 00:09:48,940 --> 00:09:55,690 That is, if you do not want to write a playbook and you run it ad hoc command, by the way, which 166 00:09:55,690 --> 00:09:57,130 we will cover later on. 167 00:09:57,370 --> 00:09:58,000 All right. 168 00:09:58,450 --> 00:09:59,230 Moving on. 169 00:09:59,230 --> 00:10:06,520 And please note running Ansible without a playbook, you will just run the script Ansible and a running 170 00:10:06,520 --> 00:10:08,080 Ansible with the playbook. 171 00:10:08,080 --> 00:10:11,950 Then you have to define that playbook or the location of the playbook. 172 00:10:12,160 --> 00:10:16,540 For that you will run the command ansible dash playbook, sorry. 173 00:10:16,540 --> 00:10:23,470 And then SpaceX followed by the Ansible playbook location. 174 00:10:24,010 --> 00:10:24,640 All right. 175 00:10:24,640 --> 00:10:30,670 So congratulations, you created your first playbook and we will go on and create a lot more playbooks 176 00:10:30,670 --> 00:10:31,960 from this point on.