1 00:00:07,260 --> 00:00:10,670 - In this video, you will learn about Ansible facts. 2 00:00:10,670 --> 00:00:13,660 Ansible facts are system variables that contain information 3 00:00:13,660 --> 00:00:15,570 about managed system. 4 00:00:15,570 --> 00:00:17,880 And each playbook starts with an implicit task 5 00:00:17,880 --> 00:00:20,983 to gather facts unless you specifically disable that. 6 00:00:22,170 --> 00:00:24,520 To use fact gathering from an ad-hoc command, 7 00:00:24,520 --> 00:00:27,630 that's also possible, you use a setup module. 8 00:00:27,630 --> 00:00:30,770 And additional modules for fact gathering are available, 9 00:00:30,770 --> 00:00:31,720 like package_facts. 10 00:00:32,610 --> 00:00:35,280 These additional modules allow you to gather facts 11 00:00:35,280 --> 00:00:38,173 for no matter which assets you are managing with Ansible. 12 00:00:39,140 --> 00:00:40,860 If you want to disable fact gathering, 13 00:00:40,860 --> 00:00:43,310 use gather_facts: no in the play header. 14 00:00:43,310 --> 00:00:44,470 And that might be interesting 15 00:00:44,470 --> 00:00:47,020 because gathering facts takes time. 16 00:00:47,020 --> 00:00:49,930 And if you don't want to wait the time to gather facts 17 00:00:49,930 --> 00:00:52,903 that you are not going to use anyway, just disable them. 18 00:00:54,110 --> 00:00:56,200 We are going to have a look at gather_facts.yaml 19 00:00:56,200 --> 00:00:58,570 to see how it is working. 20 00:00:58,570 --> 00:00:59,520 Let me demonstrate. 21 00:01:01,210 --> 00:01:03,570 So I would like to start with Ansible on 22 00:01:05,028 --> 00:01:08,000 ansible1 -m setup. 23 00:01:08,000 --> 00:01:11,257 And let me redirect that to ansible1_facts.txt. 24 00:01:15,630 --> 00:01:18,760 So this is how we manually get effects. 25 00:01:18,760 --> 00:01:21,940 And I want to write that to a text file. 26 00:01:21,940 --> 00:01:23,890 As you can see, it takes a little while 27 00:01:23,890 --> 00:01:26,440 so that the next time we are referring to these facts 28 00:01:26,440 --> 00:01:28,893 we can easily get them from the text file. 29 00:01:30,970 --> 00:01:34,730 So there we go. We have this ansible1_facts.txt, 30 00:01:34,730 --> 00:01:36,810 and here we can see the Ansible facts. 31 00:01:36,810 --> 00:01:40,230 So ansible1, that is the host name. 32 00:01:40,230 --> 00:01:42,420 And there, you can see that all the effects are stored 33 00:01:42,420 --> 00:01:44,943 in one big variable with the name ansible_facts. 34 00:01:46,120 --> 00:01:49,700 Within that variable we have the second tier variables 35 00:01:49,700 --> 00:01:50,960 like ansible_all_ipv4_address 36 00:01:52,280 --> 00:01:55,220 and these variables have their own value. 37 00:01:55,220 --> 00:01:57,360 And if you look through them, then you can see 38 00:01:57,360 --> 00:01:59,180 that the facts are really categorized 39 00:01:59,180 --> 00:02:00,560 in different variables. 40 00:02:00,560 --> 00:02:03,330 Like here, ansible_default_ipv4, 41 00:02:03,330 --> 00:02:06,770 which contains all the information about IP addresses 42 00:02:06,770 --> 00:02:09,910 or ansible_devices, which contains information 43 00:02:09,910 --> 00:02:12,980 about the block devices that you are using. 44 00:02:12,980 --> 00:02:14,910 And the nice thing about all these facts 45 00:02:14,910 --> 00:02:16,720 is that these facts are variables. 46 00:02:16,720 --> 00:02:20,360 And again, you can use these facts in conditionals. 47 00:02:20,360 --> 00:02:23,000 That's exactly what we are going to do later. 48 00:02:23,000 --> 00:02:25,830 Now let's have a look at the gather facts playbook. 49 00:02:25,830 --> 00:02:27,990 So if you have a look at that, 50 00:02:27,990 --> 00:02:30,390 then you can see that it's a really 51 00:02:30,390 --> 00:02:33,130 simple playbook where nothing really is happening. 52 00:02:33,130 --> 00:02:35,180 And that is because fact gathering, 53 00:02:35,180 --> 00:02:38,440 if it is not disabled, is implicit. 54 00:02:38,440 --> 00:02:41,250 So I don't have to run the setup module or anything 55 00:02:41,250 --> 00:02:42,610 that is only something that you do 56 00:02:42,610 --> 00:02:44,593 if you want to manually get effects. 57 00:02:46,000 --> 00:02:49,690 I am just using the debug module and using the debug module 58 00:02:49,690 --> 00:02:53,890 will print the value of the variable ansible_facts. 59 00:02:53,890 --> 00:02:54,723 Let's run it. 60 00:03:01,970 --> 00:03:04,280 We can ignore the message about a tower host, 61 00:03:04,280 --> 00:03:07,440 which is not currently up and running. 62 00:03:07,440 --> 00:03:08,783 It's not really important. 63 00:03:11,500 --> 00:03:14,320 The important thing is what we see here 64 00:03:14,320 --> 00:03:17,740 which is basically the same thing as what we have just seen. 65 00:03:17,740 --> 00:03:21,630 These again, are all these Ansible facts. 66 00:03:21,630 --> 00:03:24,620 But, and we need to scroll up a little bit 67 00:03:24,620 --> 00:03:26,300 in order to see the difference 68 00:03:26,300 --> 00:03:29,963 with the setup module that we've used before. 69 00:03:32,350 --> 00:03:35,760 If you look closely, here we have the ansible2 host. 70 00:03:35,760 --> 00:03:38,890 You can see that it's still ansible_facts 71 00:03:38,890 --> 00:03:42,013 and it has variables like all_ipv4_addresses 72 00:03:43,135 --> 00:03:47,100 and all_ipv6_addresses and ansible_local 73 00:03:47,100 --> 00:03:49,500 and apparmor and architecture. 74 00:03:49,500 --> 00:03:53,900 And if you compare that to the output of the setup module 75 00:03:53,900 --> 00:03:55,630 then you can see that in the setup module 76 00:03:55,630 --> 00:03:58,960 that we show you again, in the setup module output 77 00:03:58,960 --> 00:04:00,640 we have a slightly different approach. 78 00:04:00,640 --> 00:04:03,030 And that is where all these tier two variables 79 00:04:03,030 --> 00:04:05,283 have ansible underscore in front of them. 80 00:04:06,300 --> 00:04:08,400 Something strange is going on here. 81 00:04:08,400 --> 00:04:10,850 In fact, the setup module is still compatible 82 00:04:10,850 --> 00:04:12,520 with the old approach. 83 00:04:12,520 --> 00:04:15,240 Whereas the fact gathering is showing you the new approach. 84 00:04:15,240 --> 00:04:16,590 What exactly is the difference 85 00:04:16,590 --> 00:04:18,940 between the old approach and the new approach? 86 00:04:18,940 --> 00:04:21,740 Well, before Ansible version 2.5, 87 00:04:21,740 --> 00:04:24,440 and we are talking about year 2019, 88 00:04:24,440 --> 00:04:26,637 or 2018 something like that, 89 00:04:26,637 --> 00:04:31,200 facts were not stored in one big variable ansible_facts. 90 00:04:31,200 --> 00:04:32,550 They were just stored 91 00:04:32,550 --> 00:04:34,700 in the variable ansible_all_ipv4_addresses. 92 00:04:36,875 --> 00:04:40,330 Then in Ansible 2.5, they decided to put everything 93 00:04:40,330 --> 00:04:42,060 in one variable ansible_facts 94 00:04:43,272 --> 00:04:47,605 and to drop the name, the prefix ansible underscore. 95 00:04:48,823 --> 00:04:49,656 Now the thing is that 96 00:04:49,656 --> 00:04:52,100 the setup module is still printing this prefix. 97 00:04:52,100 --> 00:04:55,180 And if you use fact gathering, you don't see that prefix. 98 00:04:55,180 --> 00:04:59,790 And that prefix actually makes sense in the different ways, 99 00:04:59,790 --> 00:05:02,920 how you can refer to your variables and facts. 100 00:05:02,920 --> 00:05:05,580 That is what I'm going to explain in the next video 101 00:05:05,580 --> 00:05:07,380 because it's a little bit confusing.