1 00:00:01,200 --> 00:00:07,590 In this video, we will create a directory and file inside of the directory and at the end we will remove 2 00:00:07,590 --> 00:00:09,890 that file through our playbook. 3 00:00:09,900 --> 00:00:11,430 So let's go right into it. 4 00:00:11,430 --> 00:00:17,340 We'll go and log into our control node and we'll create a file, a YAML file, which will create it 5 00:00:17,340 --> 00:00:23,280 as a CR for change or create or remove file YAML. 6 00:00:23,580 --> 00:00:29,880 All right, so let's go over our entire syntax or parameters that we will be using inside of our YAML 7 00:00:29,880 --> 00:00:30,450 file. 8 00:00:30,450 --> 00:00:36,480 So very first thing is we'll define that it's a YAML file by putting dash, dash, dash then comes in 9 00:00:36,480 --> 00:00:39,900 the name of the playbook, create and remove file. 10 00:00:40,200 --> 00:00:44,220 All right, host, where are you going to run this playbook against? 11 00:00:44,220 --> 00:00:52,440 So I'm putting it all meaning it is going to Etsy as a host file and whatever the client information 12 00:00:52,440 --> 00:00:55,050 in there, it will send it to all of those clients. 13 00:00:55,440 --> 00:01:01,800 Now in our lab environment, we only have one client and we have added that into our host file, so 14 00:01:01,800 --> 00:01:08,640 it will run it against our client one All right, then the task are what do you want this playbook to 15 00:01:08,640 --> 00:01:09,030 do? 16 00:01:09,030 --> 00:01:15,660 So very first task is, and the name of that task is to create a directory, the directory. 17 00:01:15,660 --> 00:01:19,170 And for that we will use the file module. 18 00:01:19,200 --> 00:01:25,260 So you either create a file or a directory, you will have to use a same module which is file. 19 00:01:25,680 --> 00:01:27,510 Where do you want to create the directory? 20 00:01:27,510 --> 00:01:30,330 Specify the path, the location. 21 00:01:30,330 --> 00:01:36,210 We wanted to create it in a slash temp and the name of the directory is Seinfeld. 22 00:01:36,480 --> 00:01:36,840 All right. 23 00:01:36,840 --> 00:01:41,220 Who is or who is going to be the owner of that directory? 24 00:01:41,910 --> 00:01:43,110 I want to be the owner. 25 00:01:43,110 --> 00:01:46,410 So I'm just putting in I have Zal, who's the group owner. 26 00:01:46,410 --> 00:01:47,610 I have all the same thing. 27 00:01:47,610 --> 00:01:50,220 These are the permissions of that directory. 28 00:01:50,220 --> 00:01:51,480 770. 29 00:01:51,510 --> 00:01:55,590 Then the state is going to be a file or is it going to be a directory? 30 00:01:55,590 --> 00:01:57,990 So we want it to be a directory. 31 00:01:58,080 --> 00:02:00,620 So that's what you have to specify it. 32 00:02:00,750 --> 00:02:01,110 All right. 33 00:02:01,110 --> 00:02:09,180 Once that is created, the next task within the same main task is to create a file in that same directory 34 00:02:09,180 --> 00:02:10,140 that we created. 35 00:02:10,140 --> 00:02:13,110 So we will use the same module. 36 00:02:13,110 --> 00:02:18,090 And module is the file module path, same way we did it for directory. 37 00:02:18,090 --> 00:02:23,460 We have to specify the path, slash, temp, slash Seinfeld and the name of the file. 38 00:02:23,460 --> 00:02:25,350 And this time it's going to be Jerry. 39 00:02:25,650 --> 00:02:25,980 All right. 40 00:02:25,980 --> 00:02:27,690 So this is to create a file. 41 00:02:27,690 --> 00:02:32,070 Now, the state is not a directory, so we just use for the file. 42 00:02:32,070 --> 00:02:33,540 We'll just use a touch. 43 00:02:34,080 --> 00:02:35,340 All right, next step. 44 00:02:35,340 --> 00:02:41,070 The third task within this task is start the new file, Jerry. 45 00:02:41,070 --> 00:02:46,410 So, meaning we have to check and confirm whether that Jerry file has been created. 46 00:02:46,410 --> 00:02:53,460 So we'll use the module stat and the path is which file do we want to start? 47 00:02:53,580 --> 00:02:58,980 It's a file we created in the earlier tasks Temp, Seinfeld and Jerry. 48 00:02:59,430 --> 00:03:05,670 Okay, so we once we check it, we cannot just really show it on the screen or Ansible or Playbook cannot 49 00:03:05,670 --> 00:03:10,380 show it on the screen, but it has to do it has to put it into some kind of container. 50 00:03:10,380 --> 00:03:17,000 So you will use a registered parameter and the name of that variable or the name of container. 51 00:03:17,010 --> 00:03:18,420 Anything of your choice. 52 00:03:18,420 --> 00:03:20,730 I'll just put Jeff's for Jerry file. 53 00:03:21,090 --> 00:03:21,510 All right. 54 00:03:21,510 --> 00:03:26,820 So this to put the result in the container, now show file status. 55 00:03:26,820 --> 00:03:31,320 Now we have to read that container and we'll use a debug module for it. 56 00:03:31,320 --> 00:03:37,170 And for that we'll use message and the name of the message will be file, status and attribute and it 57 00:03:37,170 --> 00:03:41,160 will read from that container that we specified in a register. 58 00:03:41,490 --> 00:03:42,210 And that is. 59 00:03:43,380 --> 00:03:43,800 All right. 60 00:03:43,800 --> 00:03:46,590 So print container or variable contents. 61 00:03:46,590 --> 00:03:46,950 All right. 62 00:03:46,950 --> 00:03:53,430 And the last thing that we want to do in this entire task is to remove that Jerry file. 63 00:03:53,430 --> 00:03:58,680 So we'll use the same file module to remove to create anything we need to do. 64 00:03:58,680 --> 00:04:05,430 We need to use the file module and we'll use the path which file or where is that file located and the 65 00:04:05,430 --> 00:04:06,120 state. 66 00:04:06,120 --> 00:04:07,290 What do you want it to do now? 67 00:04:07,290 --> 00:04:10,470 You want the state to be, not touch, but instead absent. 68 00:04:10,470 --> 00:04:12,150 So it will remove it. 69 00:04:12,150 --> 00:04:13,740 This will delete that file. 70 00:04:14,370 --> 00:04:18,210 All right, so this is how we're going to create our playbook. 71 00:04:18,240 --> 00:04:20,670 I'll go ahead and log into my control node. 72 00:04:20,670 --> 00:04:24,630 As you see, I have will be creating in my control node. 73 00:04:24,720 --> 00:04:26,400 So right here. 74 00:04:27,390 --> 00:04:28,470 My control node. 75 00:04:28,470 --> 00:04:32,280 As the control node is here, I'm going to log in as root. 76 00:04:33,360 --> 00:04:33,690 All right. 77 00:04:33,690 --> 00:04:35,720 I already have a. 78 00:04:37,900 --> 00:04:38,800 A terminal open. 79 00:04:38,800 --> 00:04:44,620 Sorry, and I am logged in as who am I am logged in as route. 80 00:04:44,950 --> 00:04:47,620 I am an Etsy as well playbook directory. 81 00:04:47,620 --> 00:04:50,650 That's where I keeping all my playbooks. 82 00:04:50,650 --> 00:04:51,730 So it depends. 83 00:04:51,730 --> 00:04:54,490 Where are you keeping or where your company is keeping it? 84 00:04:54,490 --> 00:04:58,120 You should go by the organization standards anyway. 85 00:04:58,120 --> 00:05:03,010 So here I have already created the YAML file to save some time. 86 00:05:03,010 --> 00:05:08,890 So what I'll do is I'll just do vim see or file yaml. 87 00:05:09,070 --> 00:05:09,400 All right. 88 00:05:09,400 --> 00:05:12,040 So as you see, let's start from the top. 89 00:05:12,040 --> 00:05:19,840 We defined that it is a YAML file the name create and remove file host on all. 90 00:05:19,840 --> 00:05:28,210 So make sure when you are running against all the host that is inside of your Etsy Ansible host directory, 91 00:05:28,210 --> 00:05:31,120 that host or that client is up and running. 92 00:05:31,120 --> 00:05:42,160 So for that, let's see if I have a go to file a new tab and I'll do Cat at C and Sybil and Host. 93 00:05:42,160 --> 00:05:43,480 This is my host file. 94 00:05:43,480 --> 00:05:48,010 Let me move up and I notice that I have defined only one host. 95 00:05:48,010 --> 00:05:51,010 So 10 to 53 1.115. 96 00:05:51,010 --> 00:05:53,260 So make sure you can ping that. 97 00:05:53,260 --> 00:05:58,090 So that is ping 10.2 53.1 .115. 98 00:05:58,270 --> 00:05:59,560 Yes you are. 99 00:05:59,560 --> 00:06:01,090 It is payable great. 100 00:06:01,330 --> 00:06:08,350 I also have a client up and running, so if you do not have the client up and running, you should make 101 00:06:08,350 --> 00:06:11,110 sure your client is there. 102 00:06:11,110 --> 00:06:12,910 So right here I have my client. 103 00:06:13,540 --> 00:06:15,010 So let me go in. 104 00:06:16,140 --> 00:06:18,480 And right here, this is my client. 105 00:06:19,760 --> 00:06:20,390 All right. 106 00:06:20,390 --> 00:06:24,890 So let me go in back to the control node. 107 00:06:24,890 --> 00:06:28,940 And the control node is. 108 00:06:29,720 --> 00:06:30,560 Right here. 109 00:06:30,560 --> 00:06:32,270 My control node right here. 110 00:06:32,600 --> 00:06:33,080 All right. 111 00:06:33,080 --> 00:06:35,630 So clear the screen back again. 112 00:06:35,630 --> 00:06:41,660 And I'm going to go back to my file, the file that I am editing. 113 00:06:41,660 --> 00:06:42,110 All right. 114 00:06:42,140 --> 00:06:45,980 Coming down the task, what task do you want to create? 115 00:06:45,980 --> 00:06:47,750 The first task is create a directory. 116 00:06:47,750 --> 00:06:49,220 We specified all of that. 117 00:06:49,250 --> 00:06:52,820 Make sure the indentation is defined. 118 00:06:52,820 --> 00:06:54,980 Clearly, indentation is the key. 119 00:06:54,980 --> 00:06:59,270 If you have the wrong indentation anywhere, it will not run. 120 00:06:59,570 --> 00:06:59,930 All right. 121 00:06:59,930 --> 00:07:00,590 Name. 122 00:07:00,590 --> 00:07:02,990 Create a file in that directory. 123 00:07:03,950 --> 00:07:05,960 The file, the module is file. 124 00:07:05,960 --> 00:07:12,560 Then we using the stat, then we using a debugger to message the output and then we are doing remove 125 00:07:12,560 --> 00:07:12,980 file. 126 00:07:12,980 --> 00:07:14,570 So I have everything in there. 127 00:07:14,570 --> 00:07:20,330 And by the way, I have already created this file for you in the notepad plus, plus and I am going 128 00:07:20,330 --> 00:07:24,800 to upload that with in the handouts directory. 129 00:07:24,800 --> 00:07:25,190 All right. 130 00:07:25,190 --> 00:07:28,220 So anyway, let's save that now. 131 00:07:28,220 --> 00:07:29,660 Let's go ahead and run it. 132 00:07:29,660 --> 00:07:39,410 So we will use the command ansible dash playbook and we will going to specify the YAML file. 133 00:07:39,410 --> 00:07:46,550 So while I'm going to run it, I'm going to have this one just to make sure my client is there and it's 134 00:07:46,550 --> 00:07:50,660 going to create it in the temp directory and I'm going to run that. 135 00:07:50,660 --> 00:07:56,030 I'm just going to do ls minus l tr And you see right now in that temp directory there is no sign file 136 00:07:56,030 --> 00:07:56,810 directory. 137 00:07:56,810 --> 00:07:57,230 All right. 138 00:07:57,230 --> 00:07:59,420 So going back to. 139 00:08:00,550 --> 00:08:01,810 My. 140 00:08:02,480 --> 00:08:03,830 Control load. 141 00:08:04,130 --> 00:08:04,460 Right. 142 00:08:04,460 --> 00:08:07,430 Here's a control node and let's go ahead and run it. 143 00:08:08,210 --> 00:08:08,510 All right. 144 00:08:08,510 --> 00:08:11,370 So it's running it, creating and removing the file. 145 00:08:11,390 --> 00:08:12,860 This is the main play. 146 00:08:12,860 --> 00:08:20,950 And the task is to go into this client and stat let's go up, create a directory. 147 00:08:20,960 --> 00:08:22,520 It did create a file in the directory. 148 00:08:22,520 --> 00:08:23,480 It did change. 149 00:08:23,480 --> 00:08:24,590 Start the new file. 150 00:08:24,590 --> 00:08:27,320 It did the result of the stat came back. 151 00:08:27,350 --> 00:08:29,150 It's with all the attributes. 152 00:08:29,150 --> 00:08:32,330 Then remove the file from that directory. 153 00:08:32,330 --> 00:08:35,140 It did and everything worked perfectly fine. 154 00:08:35,150 --> 00:08:36,460 Change three. 155 00:08:36,470 --> 00:08:36,890 All right. 156 00:08:36,890 --> 00:08:38,720 So let's go to our. 157 00:08:39,680 --> 00:08:40,550 Client. 158 00:08:42,120 --> 00:08:43,260 And we will see. 159 00:08:43,260 --> 00:08:45,630 The directory is there, but we will not see. 160 00:08:47,310 --> 00:08:51,060 They'll file because it created the file and it removed the file. 161 00:08:51,060 --> 00:08:54,060 So let's do ls minus later in the temp directory. 162 00:08:54,060 --> 00:08:54,870 I'm in temp. 163 00:08:55,110 --> 00:08:56,460 So there you go. 164 00:08:56,470 --> 00:09:03,850 There is the Seinfeld directory owned by I of XL and group by I Absol and this is the permission that 165 00:09:03,850 --> 00:09:05,160 you specified. 166 00:09:05,160 --> 00:09:12,540 And if I go into the Seinfeld directory and do LS minus l tr you'll see nothing in there. 167 00:09:12,540 --> 00:09:15,570 So it created the file and it removed it. 168 00:09:15,570 --> 00:09:22,080 So if you go back to the control node, you'll notice that the file is right here, created the path 169 00:09:22,840 --> 00:09:23,070 here. 170 00:09:23,070 --> 00:09:27,570 Temp Seinfeld, Jerry and it was created, but then of course it was deleted. 171 00:09:27,570 --> 00:09:32,460 So that's how you create a file state file, check the status of the file if it is there. 172 00:09:32,460 --> 00:09:36,540 And also you can run certain condition based on the status of the file. 173 00:09:36,540 --> 00:09:39,300 For example, if the file existed, do that. 174 00:09:39,450 --> 00:09:42,090 So you could put that conditions which you could learn later on. 175 00:09:42,090 --> 00:09:47,700 But anyway, this is just a very simple way of creating file directory and removing files. 176 00:09:47,700 --> 00:09:54,240 And that was the purpose of this video that you will learn how to manipulate your file system structure.