1 00:00:05,995 --> 00:00:07,310 - In this video, 2 00:00:07,310 --> 00:00:10,280 we will discover options to change file contents. 3 00:00:10,280 --> 00:00:11,906 So how does it work? 4 00:00:11,906 --> 00:00:14,340 Well, there is ansible.builtin.copy 5 00:00:14,340 --> 00:00:16,670 by which we've talked before, which 6 00:00:16,670 --> 00:00:19,920 you can use to create files with simple contents. 7 00:00:19,920 --> 00:00:22,150 There's also ansible.builtin.replace, 8 00:00:22,150 --> 00:00:23,970 which is used for simple replacements. 9 00:00:23,970 --> 00:00:25,880 A lineinfile can be used to 10 00:00:25,880 --> 00:00:28,830 manipulate single lines in a text file. 11 00:00:28,830 --> 00:00:30,970 And finally, there's blockinfile. 12 00:00:30,970 --> 00:00:32,180 Blockinfile is useful for 13 00:00:32,180 --> 00:00:35,330 manipulating complete blocks of texts. 14 00:00:35,330 --> 00:00:38,373 It offers a little bit more possibilities. 15 00:00:39,270 --> 00:00:41,520 Let's talk about copy and replace. 16 00:00:41,520 --> 00:00:44,680 Both of them can be used for simple operations. 17 00:00:44,680 --> 00:00:47,000 You can use a content argument to the copy module 18 00:00:47,000 --> 00:00:49,840 to copy your string into a destination file. 19 00:00:49,840 --> 00:00:51,808 As in ansible all minus m copy, 20 00:00:51,808 --> 00:00:56,060 content is hello, destination is TMP hellofile. 21 00:00:56,060 --> 00:00:58,910 So that will create a file with the name hellofile, 22 00:00:58,910 --> 00:01:01,143 with the content hello inside the file. 23 00:01:02,041 --> 00:01:04,830 The replace module is going a little bit further. 24 00:01:04,830 --> 00:01:06,830 It allows you to replace file contents 25 00:01:06,830 --> 00:01:08,330 based on a regular expression. 26 00:01:09,180 --> 00:01:10,570 It uses the regexp argument to identify the regular 27 00:01:12,058 --> 00:01:14,490 expression that you want to replace. 28 00:01:14,490 --> 00:01:16,560 And, replace, itself, is where 29 00:01:16,560 --> 00:01:19,310 you specified the replacement text. 30 00:01:19,310 --> 00:01:22,600 You can also just omit the replace argument 31 00:01:22,600 --> 00:01:24,263 if you just want to remove text. 32 00:01:25,870 --> 00:01:27,810 Completely different solutions are offered 33 00:01:27,810 --> 00:01:30,459 by lineinfile and blockinfile. 34 00:01:30,459 --> 00:01:34,120 You can use lineinfile to add a single line to a file. 35 00:01:34,120 --> 00:01:36,550 And, also to determine where exactly 36 00:01:36,550 --> 00:01:39,230 this file is going to be created. 37 00:01:39,230 --> 00:01:40,620 And, you can use blockinfile to 38 00:01:40,620 --> 00:01:42,990 add multiple lines to a file. 39 00:01:42,990 --> 00:01:44,740 Let's go have a look at an example. 40 00:01:45,740 --> 00:01:48,521 So the example is the file copied.yml 41 00:01:48,521 --> 00:01:52,040 and you can find it in the files directly in 42 00:01:52,040 --> 00:01:54,050 the get up repository. 43 00:01:54,050 --> 00:01:57,480 In this file, there's a couple of modules I used. 44 00:01:57,480 --> 00:02:00,942 So to start with, there is the task copy, 45 00:02:00,942 --> 00:02:04,563 which is copying over the source, etc hosts, 46 00:02:05,510 --> 00:02:07,580 to the destination TMP. 47 00:02:07,580 --> 00:02:10,360 If you are using copy, then you will always copy 48 00:02:10,360 --> 00:02:12,700 from the control host to the managed host. 49 00:02:12,700 --> 00:02:17,700 So etc hosts is a file as it exists on the control host. 50 00:02:17,723 --> 00:02:21,000 Next, this playbook is using blockinfile 51 00:02:21,000 --> 00:02:23,996 to add some lines to the TMP hosts. 52 00:02:23,996 --> 00:02:26,996 So, the path is TMP host and the block 53 00:02:26,996 --> 00:02:29,330 specified is right here. 54 00:02:29,330 --> 00:02:32,530 Notice the pipe behind the name of the module 55 00:02:32,530 --> 00:02:34,190 which is making sure that the next couple 56 00:02:34,190 --> 00:02:37,588 of lines is written to the configuration file 57 00:02:37,588 --> 00:02:40,970 with this specific layout. 58 00:02:40,970 --> 00:02:43,740 You can see this text is wrapped over multiple lines. 59 00:02:43,740 --> 00:02:46,290 If you want to keep that, you need this pipe. 60 00:02:46,290 --> 00:02:50,043 State present, ensures that these lines will be added. 61 00:02:51,240 --> 00:02:54,140 Next, we are using this stat module. 62 00:02:54,140 --> 00:02:56,210 I haven't mentioned the stat module before 63 00:02:56,210 --> 00:02:58,340 but the stat module is a convenient module 64 00:02:58,340 --> 00:03:01,920 that helps you to verify properties of files. 65 00:03:01,920 --> 00:03:04,742 So, we are verifying the TMP host file 66 00:03:04,742 --> 00:03:09,742 and the checksum algorithm is MD five. 67 00:03:10,015 --> 00:03:12,750 And then, the stat model is mostly used 68 00:03:12,750 --> 00:03:14,450 in combination with the register 69 00:03:14,450 --> 00:03:16,940 because otherwise you don't see what it is doing. 70 00:03:16,940 --> 00:03:20,010 So it registers a variable with a name result 71 00:03:20,010 --> 00:03:23,370 and then it debugs the checksum of TMP host is a 72 00:03:23,370 --> 00:03:25,633 result.stat.checksum. 73 00:03:26,740 --> 00:03:31,140 And, finally we are using fetch to fetch a file. 74 00:03:31,140 --> 00:03:35,287 So it fetches TMP hosts to the destination slash TMP. 75 00:03:35,287 --> 00:03:36,680 Let's learn this playbook 76 00:03:36,680 --> 00:03:38,580 and let's figure out what it is doing. 77 00:03:39,900 --> 00:03:42,993 So let me use Ansible playbook on copy. 78 00:03:45,930 --> 00:03:46,763 And there we go. 79 00:03:46,763 --> 00:03:48,680 We can see all the work that it is doing. 80 00:03:48,680 --> 00:03:51,432 The output itself is not revealing very much. 81 00:03:51,432 --> 00:03:54,290 We can see the result of the debug command. 82 00:03:54,290 --> 00:03:56,210 That's an interesting one because 83 00:03:56,210 --> 00:03:58,480 that's what we've used the debug module for. 84 00:03:58,480 --> 00:04:00,150 And then it fetches a file. 85 00:04:00,150 --> 00:04:02,710 Now the interesting thing here is in fetch. 86 00:04:02,710 --> 00:04:06,933 Let's check out the TMP directory because it was 87 00:04:06,933 --> 00:04:10,640 instructed to fetch to the TMP directory. 88 00:04:10,640 --> 00:04:13,280 There, you can see in the TMP directory, there's 89 00:04:13,280 --> 00:04:16,700 an ansible1 as well as an ansible2 sub directory. 90 00:04:16,700 --> 00:04:19,450 And in this ansible1 directory, what do we find? 91 00:04:19,450 --> 00:04:21,561 Well, let's check it out. 92 00:04:21,561 --> 00:04:25,820 Ansible1, where we can find a TMP sub directory. 93 00:04:25,820 --> 00:04:27,580 That is what fetch is doing. 94 00:04:27,580 --> 00:04:30,350 Fetch is maintaining the entire patent 95 00:04:30,350 --> 00:04:31,597 as it exists on the target host, 96 00:04:31,597 --> 00:04:33,840 and you will find the target host name 97 00:04:33,840 --> 00:04:36,660 in the path on the control host as well. 98 00:04:36,660 --> 00:04:38,720 And that is to allow you optimal control 99 00:04:38,720 --> 00:04:40,930 to the different files that are fetched 100 00:04:40,930 --> 00:04:42,790 from different hosts, because otherwise 101 00:04:42,790 --> 00:04:45,440 how would Ansible ever see the difference 102 00:04:45,440 --> 00:04:47,500 between the different hosts? 103 00:04:47,500 --> 00:04:50,160 In the next video we are going to talk about find. 104 00:04:50,160 --> 00:04:53,090 And I have a nice example of advanced file 105 00:04:53,090 --> 00:04:55,863 manipulations in that video as well.