1 00:00:01,110 --> 00:00:01,730 Hi. 2 00:00:01,740 --> 00:00:09,510 In this video we are going to learn how we could create a file and add text or content inside of a file. 3 00:00:09,750 --> 00:00:17,100 So we will log into our control node and we will create our playbook and we are going to name the playbook 4 00:00:17,100 --> 00:00:19,640 as add a text file. 5 00:00:19,680 --> 00:00:21,930 Of course you could pick any name of your choice. 6 00:00:22,230 --> 00:00:25,260 Now what goes inside of the playbook? 7 00:00:25,470 --> 00:00:29,310 And as you know, very first thing is be defined that it's a YAML file. 8 00:00:29,310 --> 00:00:34,380 And then we start with the name of the playbook, create a file and add text. 9 00:00:35,070 --> 00:00:40,470 Then the host Where do you want it to run against or where do you want to create that file? 10 00:00:40,870 --> 00:00:48,000 At this for this video, we will create a file to our local host, meaning we will create on the control 11 00:00:48,000 --> 00:00:50,070 node where we running our Ansible. 12 00:00:50,780 --> 00:00:51,630 All right, good. 13 00:00:51,650 --> 00:00:55,310 Now let's go into the task what exactly you want to do. 14 00:00:55,340 --> 00:01:00,860 So very first thing we want to do is, of course, to create a file, and the second is to add text. 15 00:01:00,860 --> 00:01:06,860 So these are the two tasks we wanted to do, and we could put them both under the same one task. 16 00:01:06,860 --> 00:01:08,720 So we'll define the tasks. 17 00:01:08,720 --> 00:01:12,860 The very first task and the name of that is to create a new file. 18 00:01:12,860 --> 00:01:16,220 And what is the module we should be using to create a file? 19 00:01:17,060 --> 00:01:19,850 It should be the file module, yes. 20 00:01:20,240 --> 00:01:26,330 Then you need to define where are you creating and what will be the name of the file. 21 00:01:26,330 --> 00:01:30,640 So we're creating in slash temp and the name of the file will be Jorge. 22 00:01:31,220 --> 00:01:31,540 All right. 23 00:01:31,580 --> 00:01:35,570 And that's the location and the name of the file, then the state. 24 00:01:35,570 --> 00:01:37,550 What do you want to do with the file? 25 00:01:37,550 --> 00:01:43,640 I mean, if it exists, do you want to delete it by putting in state equal Epson or state to be touch 26 00:01:43,670 --> 00:01:45,200 because you want to create it. 27 00:01:45,200 --> 00:01:49,490 So in this case we are creating it, so we need to use touch. 28 00:01:50,150 --> 00:01:58,820 All right, then the next task, but in this play is to add text to the file. 29 00:01:58,820 --> 00:02:00,110 So now file is created. 30 00:02:00,110 --> 00:02:03,710 Now we need to populate some text into the file. 31 00:02:03,710 --> 00:02:06,770 And by the way, this is very useful. 32 00:02:06,770 --> 00:02:15,440 Let's say if you want to update your ETSI host file your MTP configuration file or your HTTP dx configuration 33 00:02:15,440 --> 00:02:19,460 file, and you have to update that to, let's say 100 different servers. 34 00:02:19,460 --> 00:02:27,050 So you don't want it to log into those hundred servers and update or add some new information to that 35 00:02:27,050 --> 00:02:27,470 file. 36 00:02:27,470 --> 00:02:29,450 So that's when you this. 37 00:02:30,870 --> 00:02:33,910 A module lock in file comes to your rescue. 38 00:02:33,930 --> 00:02:38,910 This is the one that actually helps you add texts to your files. 39 00:02:39,450 --> 00:02:45,960 All right, then the module needs to know where your file is located, and we define the path. 40 00:02:45,990 --> 00:02:49,570 Our file is in temp directory and the name of the file is short. 41 00:02:49,590 --> 00:02:55,560 So let's say if you are updating at sea host file, then here you would put in here or any configuration 42 00:02:55,560 --> 00:02:56,010 file. 43 00:02:56,220 --> 00:02:57,510 Now the block. 44 00:02:57,540 --> 00:03:03,720 The block is the parameters that you need to use to put in what exactly that you wanted to add into 45 00:03:03,720 --> 00:03:04,090 it. 46 00:03:04,110 --> 00:03:09,180 I'm just adding random text to the file because George. 47 00:03:09,180 --> 00:03:14,590 George is one of the main characters of Seinfeld show and he is Jerry's best friend. 48 00:03:14,590 --> 00:03:20,550 And by the way, Seinfeld show is a sitcom and he is the best friend of Jerry who created the Seinfeld 49 00:03:20,550 --> 00:03:20,730 show. 50 00:03:20,730 --> 00:03:23,120 So it's just the text that I'm adding to it. 51 00:03:23,130 --> 00:03:30,780 So let's say if you are adding into ATC host file and you want it to be added in a proper indentation, 52 00:03:30,780 --> 00:03:37,440 you could define that proper indentation, but putting block call in space and then putting pipe. 53 00:03:38,020 --> 00:03:45,190 And then you could enter, hit, enter and put down the exact way that you want like IP and hostname. 54 00:03:45,850 --> 00:03:46,180 All right. 55 00:03:46,180 --> 00:03:51,700 So then once the file is created, these are the two tasks that we wanted to create in this play. 56 00:03:51,730 --> 00:03:53,110 Then what happens? 57 00:03:53,500 --> 00:03:59,920 Of course, text, add text to a file, and we run the Ansible playbook and it will create the file 58 00:03:59,920 --> 00:04:00,520 and add text. 59 00:04:00,520 --> 00:04:07,000 So let's go into our control node and let's create the file, which, by the way, I have already created 60 00:04:07,000 --> 00:04:08,440 for you to save some time. 61 00:04:08,440 --> 00:04:10,870 And we'll go over it and then we'll run it. 62 00:04:11,700 --> 00:04:12,570 The my. 63 00:04:14,290 --> 00:04:14,870 Aspen. 64 00:04:14,890 --> 00:04:22,240 Lord is right here and I have already I'm already logged in as route. 65 00:04:22,240 --> 00:04:27,730 Or if you are running as yourself or different user, just make sure you are logged in as that person. 66 00:04:28,150 --> 00:04:28,510 All right. 67 00:04:28,510 --> 00:04:33,130 Now let's go to our playbook that I created earlier. 68 00:04:33,130 --> 00:04:35,890 Add text dynamo file. 69 00:04:36,130 --> 00:04:41,780 And if you notice, I added everything exactly the same as I have it on my PowerPoint slide. 70 00:04:41,800 --> 00:04:49,510 So local host task is to create a new file and then the file is the module, the path to define where 71 00:04:49,510 --> 00:04:51,070 you want to create the file. 72 00:04:51,070 --> 00:04:55,780 The state is to touch meaning create a file name is next. 73 00:04:56,010 --> 00:04:57,910 This is the next task of the play. 74 00:04:57,940 --> 00:05:06,040 Add text to the file and then will use block and file the name of the magic module path where you want 75 00:05:06,100 --> 00:05:06,880 to add that text. 76 00:05:06,880 --> 00:05:09,640 And what is the text is actually the block. 77 00:05:09,640 --> 00:05:09,900 All right. 78 00:05:09,970 --> 00:05:11,170 So that is saved. 79 00:05:11,200 --> 00:05:19,800 Let's go ahead and run it and we'll run it as Ansible dash playbook and add text. 80 00:05:22,010 --> 00:05:23,660 All right, create a file and add. 81 00:05:23,660 --> 00:05:25,640 This is the name of the playbook. 82 00:05:26,090 --> 00:05:33,860 Gathering facts create local host is going to the local host which is this is the own host changed to 83 00:05:34,040 --> 00:05:38,420 perfect meaning it created the file and it added the text. 84 00:05:38,420 --> 00:05:43,070 So how do we verify we could just simply do cat temp. 85 00:05:43,190 --> 00:05:49,310 George This will tell me that the file has been created and the content of the file. 86 00:05:49,550 --> 00:05:51,500 So the file is there definitely. 87 00:05:51,500 --> 00:05:54,110 And here are the contents of the file. 88 00:05:54,110 --> 00:06:01,910 And by default when you run or when you add text through Ansible block, then it will add these two 89 00:06:01,910 --> 00:06:08,360 lines where it says Begin, assemble, manage block and then end as manage block. 90 00:06:08,360 --> 00:06:16,850 But of course this has no effect on any of your file or configuration file because it has it starts 91 00:06:16,850 --> 00:06:25,790 with LB So anything or any any line that start with LB it's actually the comment in a file and it has 92 00:06:25,790 --> 00:06:27,650 no effect on the file. 93 00:06:28,370 --> 00:06:36,590 So anyway, so now you know how to create a file or copy a file, and then you can add a text to a file.