1 00:00:00,740 --> 00:00:01,640 At this point. 2 00:00:01,640 --> 00:00:08,270 We all know that the playbooks in Ansible are written in YAML format. 3 00:00:08,630 --> 00:00:16,910 So in this video there are a few important things to remember when we are writing our playbooks in YAML 4 00:00:16,910 --> 00:00:22,340 and the syntax that we should follow when creating YAML files. 5 00:00:23,300 --> 00:00:29,780 First thing is, all tasks in YAML are executed in sequential order. 6 00:00:30,290 --> 00:00:36,200 If you want a task to be executed first, it has to be written in the first order. 7 00:00:36,500 --> 00:00:42,980 Then each task that you want to be executed is processed one at a time. 8 00:00:42,980 --> 00:00:49,130 So just like a batch shell scripting, if you are familiar, it goes through the first task and then 9 00:00:49,130 --> 00:00:51,530 it goes to the second and third and so forth. 10 00:00:53,060 --> 00:00:59,330 Indentation in YAML file is extremely important. 11 00:00:59,870 --> 00:01:07,790 A lot of people will experience this issue that when you do not have the right indentation in the GMO 12 00:01:07,790 --> 00:01:10,450 file, it won't execute. 13 00:01:10,460 --> 00:01:18,050 So you need to know how many spaces you have to define, where you have to define those spaces. 14 00:01:18,410 --> 00:01:22,040 By the way, as we go through creating simple. 15 00:01:23,450 --> 00:01:24,910 YAML files. 16 00:01:24,920 --> 00:01:31,040 We will know exactly about the indentation then a no tabs and yaml file. 17 00:01:31,040 --> 00:01:31,250 Yes. 18 00:01:31,250 --> 00:01:33,970 Please do not use tabs. 19 00:01:33,980 --> 00:01:38,540 Then you are creating yaml file while you are using the editor. 20 00:01:38,570 --> 00:01:48,860 Now maybe you are using a different editor which allows you to create or run tabs, but then it converts 21 00:01:48,860 --> 00:01:49,850 to spaces. 22 00:01:49,850 --> 00:01:51,080 Then it's fine. 23 00:01:51,080 --> 00:01:59,210 But when you are creating YAML files using VI editor or VM editor and Linux, then do not use tabs. 24 00:02:00,080 --> 00:02:01,830 Only use spaces. 25 00:02:01,850 --> 00:02:02,210 Yes. 26 00:02:02,210 --> 00:02:03,530 It goes along with the first one. 27 00:02:03,530 --> 00:02:03,710 What? 28 00:02:03,710 --> 00:02:05,300 I said do not use tabs. 29 00:02:05,390 --> 00:02:11,390 You should always use spaces and that spaces should define our indentation. 30 00:02:12,080 --> 00:02:15,080 Then empty lines that have no value. 31 00:02:15,080 --> 00:02:20,450 So if you have one empty line to empty line, it doesn't make a difference. 32 00:02:20,630 --> 00:02:22,670 Just like a bash shell script. 33 00:02:22,670 --> 00:02:28,430 When you create empty lines, it has no value in your execution of your task. 34 00:02:29,030 --> 00:02:35,840 File extensions is usually dot by QML or dot by a HTML. 35 00:02:35,880 --> 00:02:39,880 Now, if you do not have a file extension that is still fine. 36 00:02:39,890 --> 00:02:42,860 The file or the playbook will be executed. 37 00:02:42,860 --> 00:02:52,580 But it is a good practice that you create your YAML files with extension dot html or dot by a HTML. 38 00:02:53,660 --> 00:02:54,800 All right then. 39 00:02:54,800 --> 00:03:00,320 There is no difference in double codes or no codes for a task name. 40 00:03:00,320 --> 00:03:08,090 So when we define a task name, every time we write a playbook or rewrite a play, a task name has to 41 00:03:08,090 --> 00:03:09,200 be defined. 42 00:03:09,350 --> 00:03:16,250 Now, you will notice maybe when you do a search online or you go through a certain other resources 43 00:03:16,250 --> 00:03:23,150 that certain people have double codes around a task name and certain people don't have codes. 44 00:03:23,150 --> 00:03:24,130 That is fine. 45 00:03:24,140 --> 00:03:27,470 You could use double codes or not, it will still execute. 46 00:03:27,770 --> 00:03:35,120 Then a Jammal playbook files can be placed anywhere on the file system as long as they are being executed 47 00:03:35,120 --> 00:03:36,500 with absolute path. 48 00:03:36,620 --> 00:03:37,190 Yes. 49 00:03:37,190 --> 00:03:40,730 So our yaml home directory is sorry. 50 00:03:40,730 --> 00:03:43,610 Our ansible home directory is slash root. 51 00:03:43,610 --> 00:03:44,150 Sorry. 52 00:03:44,150 --> 00:03:47,210 Slash at c slash ansible. 53 00:03:47,810 --> 00:03:53,970 So you can keep all your playbook or yaml files in that directory. 54 00:03:53,990 --> 00:04:00,770 That's a good practice, but if you wanted to keep those executable files somewhere else, that is still 55 00:04:00,770 --> 00:04:01,100 fine. 56 00:04:01,100 --> 00:04:04,550 But when you run it, you have to run it with the absolute path. 57 00:04:05,270 --> 00:04:05,600 All right. 58 00:04:05,610 --> 00:04:13,750 When a flat file is written in YAML format to execute certain tasks or place, then it is called a playbook 59 00:04:13,760 --> 00:04:15,450 inside of a playbook. 60 00:04:15,470 --> 00:04:21,860 The format that we use or the code that we use that is in YAML syntax. 61 00:04:21,860 --> 00:04:25,610 But once it's created and saved it is called a playbook. 62 00:04:26,780 --> 00:04:31,440 There is no need to modify file permission. 63 00:04:31,460 --> 00:04:32,200 Yes. 64 00:04:32,210 --> 00:04:40,310 As you notice, if you have written bash shell scripts, you will have to give it permission, executable 65 00:04:40,310 --> 00:04:47,210 permission at the owner level, group level or others level, which is the x x x. 66 00:04:47,510 --> 00:04:53,300 But in a yaml files or playbooks, you do not have to give those executable permission. 67 00:04:53,300 --> 00:04:57,290 You can leave it as a flat file or the read executable files. 68 00:04:57,290 --> 00:05:04,550 That's fine and you do not need executables, so you do not have to use the change mod command to define 69 00:05:04,550 --> 00:05:06,860 the special permission for your file. 70 00:05:07,310 --> 00:05:14,270 So anyway, these are these were the few important things that you should know and remember before you 71 00:05:14,270 --> 00:05:18,080 start creating your playbooks in YAML format. 72 00:05:19,040 --> 00:05:25,550 So the very first thing you would do when you log into your Linux system, then you use a bi or editor 73 00:05:25,640 --> 00:05:31,370 and then you specify the name that you wanted to choose for the file. 74 00:05:31,580 --> 00:05:43,100 So here you start via editor, and in order to define that it is a YAML file, you have to type it in 75 00:05:43,550 --> 00:05:48,500 hyphen, hyphen, hyphen in the very first line. 76 00:05:48,530 --> 00:05:51,470 This is what defines that. 77 00:05:51,470 --> 00:05:52,970 It says yaml file. 78 00:05:53,690 --> 00:06:02,030 Once this is defined, then you put in spaces and put in the name of the playbook. 79 00:06:02,670 --> 00:06:08,670 This is the name of the entire playbook as to what playbook is exactly doing. 80 00:06:09,450 --> 00:06:12,130 Then you put down the host. 81 00:06:12,150 --> 00:06:16,490 If you notice, host is right below, exactly below the name. 82 00:06:16,500 --> 00:06:20,370 Make sure there are no additional or less spaces. 83 00:06:20,400 --> 00:06:23,220 It has to be right below the name. 84 00:06:23,550 --> 00:06:31,950 And this host defines where you want to run this playbook or which host you will be running this playbook 85 00:06:31,950 --> 00:06:32,880 against. 86 00:06:33,510 --> 00:06:43,470 Then you have another syntax or parameters in the GMO file is to become which states that run as a different 87 00:06:43,470 --> 00:06:44,190 user. 88 00:06:44,370 --> 00:06:47,430 If you type it in yes, then it means it will. 89 00:06:47,430 --> 00:06:51,480 You will have to define which user you want to run run it as. 90 00:06:51,660 --> 00:06:56,930 So in the second fourth line you define become underscore user root. 91 00:06:57,240 --> 00:07:02,460 Now you do not need to define become yes or become user. 92 00:07:02,460 --> 00:07:03,690 Which type of user? 93 00:07:03,870 --> 00:07:09,780 If you take it out and then you run this playbook as a root, then it will run it as a root. 94 00:07:09,930 --> 00:07:14,160 But if you run it as a regular user, then it will run a regular user. 95 00:07:14,820 --> 00:07:21,690 This is only defined when you are running as a regular user and you want it to run as a root or vice 96 00:07:21,690 --> 00:07:22,260 versa. 97 00:07:23,040 --> 00:07:23,520 All right. 98 00:07:23,520 --> 00:07:26,880 Next thing you notice, here is an empty line. 99 00:07:26,880 --> 00:07:30,930 And we are starting our tasks after the empty line. 100 00:07:31,500 --> 00:07:34,980 Empty line, once again, does not make any difference. 101 00:07:35,490 --> 00:07:45,900 And the task is a right below where the be or become is the indentation in the YAML syntax format matters 102 00:07:45,900 --> 00:07:46,800 a lot. 103 00:07:47,220 --> 00:07:50,780 The task is where you declare the task. 104 00:07:50,790 --> 00:07:51,980 You're telling the playbook. 105 00:07:51,990 --> 00:07:53,030 Okay, playbook. 106 00:07:53,040 --> 00:07:58,230 Now this is what I want you to run or this is what I want you to do. 107 00:07:59,010 --> 00:08:01,560 And the playbook says, Okay, what is that? 108 00:08:01,890 --> 00:08:10,650 Then you have to define the name of that play and the name is I want you to install Apache HTTP. 109 00:08:10,650 --> 00:08:12,060 And it's just a name. 110 00:08:12,060 --> 00:08:13,710 It's nothing executable. 111 00:08:13,710 --> 00:08:16,560 It's just a defining the name of the task. 112 00:08:17,340 --> 00:08:25,590 Look at the task, the T right below the T, that's where we put in the hyphen space. 113 00:08:25,590 --> 00:08:26,640 Then the name. 114 00:08:27,180 --> 00:08:31,740 You cannot move this on the left or to the right. 115 00:08:32,940 --> 00:08:36,420 Because it will cause problem when you're going to execute it. 116 00:08:36,420 --> 00:08:39,660 So indentation matters. 117 00:08:40,320 --> 00:08:43,470 Then it's saying, okay, you want me to run the task? 118 00:08:43,470 --> 00:08:46,740 And the name of the task is Install Apache Oak. 119 00:08:46,800 --> 00:08:49,500 But what are we doing? 120 00:08:49,770 --> 00:08:52,170 So you're telling it, use the module. 121 00:08:52,170 --> 00:08:52,980 Yum. 122 00:08:53,770 --> 00:08:55,570 Run task modules. 123 00:08:55,570 --> 00:08:56,380 Yum. 124 00:08:56,650 --> 00:08:59,980 To install this HTTP feed package. 125 00:09:00,010 --> 00:09:01,280 All right, perfect. 126 00:09:01,630 --> 00:09:07,000 What is the name of the package that you wanted to run as using the module? 127 00:09:07,030 --> 00:09:07,720 Yum. 128 00:09:08,050 --> 00:09:10,810 That would be a HTTP feed. 129 00:09:11,110 --> 00:09:18,580 Now, this is the exact name that you will use as if you are installing yum, install http or yum, 130 00:09:18,580 --> 00:09:21,850 install anti PD or yum install any package name. 131 00:09:21,850 --> 00:09:27,940 So that package name has to be the exact same name that you would install it with DNF or Yum! 132 00:09:28,720 --> 00:09:29,020 All right. 133 00:09:29,020 --> 00:09:31,360 You're saying this is the name of the package. 134 00:09:31,660 --> 00:09:32,410 Perfect. 135 00:09:32,440 --> 00:09:34,550 Next thing is state. 136 00:09:34,570 --> 00:09:35,860 Now, I. 137 00:09:36,160 --> 00:09:38,860 I'll go and and check that package. 138 00:09:38,860 --> 00:09:39,820 I'll get the package. 139 00:09:39,820 --> 00:09:41,220 Now, what do you want me to do with it? 140 00:09:41,230 --> 00:09:42,250 With that package? 141 00:09:42,430 --> 00:09:47,140 Well, when you define the state, followed by the present, it means what to do. 142 00:09:47,140 --> 00:09:50,350 And we are saying present, meaning install. 143 00:09:50,800 --> 00:09:58,120 Now there are different type of states in a package which you could get from the documentation, of 144 00:09:58,120 --> 00:10:02,740 course, which will cover in many, many of the playbooks. 145 00:10:03,160 --> 00:10:08,500 So that's what the task will do as one of the task. 146 00:10:09,250 --> 00:10:12,490 It will get the package and it will install a HTTP package. 147 00:10:12,580 --> 00:10:13,200 Beautiful. 148 00:10:13,210 --> 00:10:17,410 Now, do you want to do anything with this package? 149 00:10:17,410 --> 00:10:21,340 Once that package is installed, then yes. 150 00:10:21,340 --> 00:10:22,630 I want you to do. 151 00:10:23,590 --> 00:10:28,390 The task within that main task, of course, and this is the second task name. 152 00:10:28,960 --> 00:10:37,810 And in the second task I want you to use the module called service, and then the name of that service 153 00:10:37,810 --> 00:10:39,430 is a HTTP feed. 154 00:10:39,430 --> 00:10:40,320 And what do you want? 155 00:10:40,340 --> 00:10:44,520 We want the playbook to do you want the player. 156 00:10:44,590 --> 00:10:47,590 So you want that package to be started. 157 00:10:47,800 --> 00:10:55,520 So this these are two tasks that are defined within the same one big task of a HTTP. 158 00:10:56,920 --> 00:11:02,620 You can create additional task if you want to install another package, but that will be in a different 159 00:11:02,620 --> 00:11:03,880 indentation. 160 00:11:04,980 --> 00:11:06,690 All of these modules. 161 00:11:06,690 --> 00:11:14,490 If you are thinking Enron, hey, how would I know which state to use and what package to use, or what 162 00:11:14,490 --> 00:11:16,830 are the different modules that I could use? 163 00:11:17,400 --> 00:11:21,120 Well, as you're going to go through the module. 164 00:11:22,080 --> 00:11:24,480 And creating ansible playbooks. 165 00:11:24,480 --> 00:11:30,360 You will learn just like as if you are learning a new language or a programming language. 166 00:11:30,390 --> 00:11:33,930 The more you do it, the more you learn about it. 167 00:11:34,170 --> 00:11:37,230 So if you go to this documentation. 168 00:11:37,230 --> 00:11:39,330 Exactly dockside ansible dot com. 169 00:11:39,330 --> 00:11:43,890 I already have that open for you in my firefox. 170 00:11:44,070 --> 00:11:51,720 This is the address and here these are the lists of all the modules that are given to you out of the 171 00:11:51,720 --> 00:11:53,650 box by Ansible. 172 00:11:53,780 --> 00:12:01,470 So if you do control F to find your module, you'll see right here is the Yum module and you click on 173 00:12:01,470 --> 00:12:01,710 it. 174 00:12:01,710 --> 00:12:06,630 You will see all the parameters that can be used against that Yama module. 175 00:12:06,660 --> 00:12:10,710 So in our example, we are using parameter state. 176 00:12:11,450 --> 00:12:19,790 And the choice that we are using for state is present, which means the package, the defined package 177 00:12:19,790 --> 00:12:21,620 has to be installed. 178 00:12:21,980 --> 00:12:29,910 And in the second task, we want the package or that service of that package to be started. 179 00:12:29,930 --> 00:12:33,050 So for that, you have to go to the service module. 180 00:12:33,050 --> 00:12:35,060 This is the module. 181 00:12:35,150 --> 00:12:39,950 So we'll go back and do a find on service. 182 00:12:43,690 --> 00:12:45,250 Here is the service module. 183 00:12:46,880 --> 00:12:49,280 You click on it, you go to parameters. 184 00:12:49,280 --> 00:12:56,600 And the state that we define in our example is started, which means to start up the service. 185 00:12:56,720 --> 00:13:02,240 So you could, you could get all the different kind of helps from this documentation whenever you want 186 00:13:02,240 --> 00:13:09,530 to define a module and you should spend some time to go over or skim through the list of all the modules 187 00:13:09,530 --> 00:13:14,270 that that are available to you and what are the different parameters that you could define?