1 00:00:06,660 --> 00:00:08,130 - While working with Ansible 2 00:00:08,130 --> 00:00:11,050 you will also encounter the so-called magic variables. 3 00:00:11,050 --> 00:00:13,290 Let me give you a quick overview. 4 00:00:13,290 --> 00:00:17,110 So a magic variable is a special reserved system variables. 5 00:00:17,110 --> 00:00:19,150 There's a couple of them, like hostvars, 6 00:00:19,150 --> 00:00:21,880 which prints all the variables assigned to a host, 7 00:00:21,880 --> 00:00:24,640 or groups, which is about all host groups, 8 00:00:24,640 --> 00:00:27,570 or group_names, which prints all names of the host groups. 9 00:00:27,570 --> 00:00:28,907 Or inventory_hostname, 10 00:00:28,907 --> 00:00:32,994 that is the host name as defined in inventory. 11 00:00:32,994 --> 00:00:35,460 Inventory_hostname_short, the short alternative. 12 00:00:35,460 --> 00:00:38,800 And there's a couple more, which you won't see that often. 13 00:00:38,800 --> 00:00:41,000 Now, you should know that these metric variables 14 00:00:41,000 --> 00:00:41,940 are reserved. 15 00:00:41,940 --> 00:00:43,730 You cannot redefine them yourself. 16 00:00:43,730 --> 00:00:45,890 They have the highest possible priority. 17 00:00:45,890 --> 00:00:49,120 Funny story, first time I encountered magic variables 18 00:00:49,120 --> 00:00:51,770 was while working with the playbook to create users. 19 00:00:51,770 --> 00:00:54,520 And of course I wanted to define the variable groups. 20 00:00:54,520 --> 00:00:55,680 That's not going to work 21 00:00:55,680 --> 00:00:58,200 because of the magic variable groups. 22 00:00:58,200 --> 00:01:00,236 Can you just imagine that all my users 23 00:01:00,236 --> 00:01:05,236 were becoming a member of the groups, as in the host groups? 24 00:01:05,430 --> 00:01:06,680 That didn't make sense. 25 00:01:06,680 --> 00:01:08,913 Well, metric variables explain everything. 26 00:01:10,140 --> 00:01:11,450 So, how do you use them? 27 00:01:11,450 --> 00:01:13,540 Well, hostvars can be used to address facts 28 00:01:13,540 --> 00:01:16,230 or inventory variables from other hosts, 29 00:01:16,230 --> 00:01:20,220 as in hostvars ansible 2 ansible facts distribution. 30 00:01:20,220 --> 00:01:23,260 That's something that you can put in a playbook 31 00:01:23,260 --> 00:01:27,510 to point to a variable of another host 32 00:01:27,510 --> 00:01:29,530 in a specific playbook. 33 00:01:29,530 --> 00:01:33,560 Groups can be used to list all hosts in an inventory group. 34 00:01:33,560 --> 00:01:37,590 As in the template, conditional structure 35 00:01:37,590 --> 00:01:40,000 for host in group web servers. 36 00:01:40,000 --> 00:01:41,870 We'll see a better example of this 37 00:01:41,870 --> 00:01:46,330 while discovering templates in all of the upcoming lessons. 38 00:01:46,330 --> 00:01:50,020 Group_names has all the inventory groups a host is in. 39 00:01:50,020 --> 00:01:51,640 So that is very convenient 40 00:01:51,640 --> 00:01:54,460 because you can use a conditional, like here, 41 00:01:54,460 --> 00:01:57,710 the conditional structure, as you can see it in a template: 42 00:01:57,710 --> 00:01:59,710 if 'web servers' in group_names. 43 00:01:59,710 --> 00:02:02,450 That's going to discover if a host is a member 44 00:02:02,450 --> 00:02:03,963 of the group web servers. 45 00:02:04,980 --> 00:02:07,880 Now inventory_hostname or inventory_hostname_short 46 00:02:07,880 --> 00:02:09,650 can be used to address a host 47 00:02:09,650 --> 00:02:11,550 as alternative to ansible_hostname 48 00:02:11,550 --> 00:02:13,890 when fact-gathering is disabled. 49 00:02:13,890 --> 00:02:15,740 So at least it allows you to work with 50 00:02:15,740 --> 00:02:18,960 hostnames anyway. 51 00:02:18,960 --> 00:02:21,440 And that's what you need to know about magic variable. 52 00:02:21,440 --> 00:02:22,363 Let's continue.