1 00:00:05,908 --> 00:00:10,310 - In this video, I'm going to explain ad-hoc commands. 2 00:00:10,310 --> 00:00:11,500 In an ad-hoc command, 3 00:00:11,500 --> 00:00:14,470 Ansible modules are used to perform specific tasks 4 00:00:14,470 --> 00:00:15,303 on managed hosts. 5 00:00:15,303 --> 00:00:17,770 An ad-hoc commands are perfect 6 00:00:17,770 --> 00:00:20,450 for running one task on a managed host. 7 00:00:20,450 --> 00:00:23,610 Some common use cases, are for initial host set up 8 00:00:23,610 --> 00:00:27,010 for instance, that is something that we have already covered 9 00:00:27,010 --> 00:00:30,090 in the first lesson of this course. 10 00:00:30,090 --> 00:00:32,880 Or for checking configuration on hosts, 11 00:00:32,880 --> 00:00:33,950 if you want to find out 12 00:00:33,950 --> 00:00:36,170 if a specific configuration is present, 13 00:00:36,170 --> 00:00:38,680 ad-hoc commands are the way to go. 14 00:00:38,680 --> 00:00:41,300 Also ad-hoc commands are a perfect way to learn 15 00:00:41,300 --> 00:00:44,410 how easy Ansible really is. 16 00:00:44,410 --> 00:00:47,410 The opposite of the ad-hoc commands is the playbook. 17 00:00:47,410 --> 00:00:50,010 The playbook is the way to implement more complex 18 00:00:50,010 --> 00:00:53,210 tasks, where dependency relations have to be managed. 19 00:00:53,210 --> 00:00:54,890 You may find for instance, 20 00:00:54,890 --> 00:00:57,760 that an ad-hoc command to start your web service 21 00:00:57,760 --> 00:01:01,010 is going to fill because the web service is not there. 22 00:01:01,010 --> 00:01:03,720 That is the use case for creating a playbook, 23 00:01:03,720 --> 00:01:05,300 and the playbook is taking care 24 00:01:05,300 --> 00:01:08,301 of creating the entire desired state. 25 00:01:08,301 --> 00:01:11,520 In ad-hoc commands modules are used. 26 00:01:11,520 --> 00:01:13,640 An Ansible module is a python script 27 00:01:13,640 --> 00:01:16,320 that will be executed on a managed host. 28 00:01:16,320 --> 00:01:20,500 And there are over 3000 modules available in Ansible. 29 00:01:20,500 --> 00:01:25,500 Modules can be offered as such which was pre Ansible 2.10 30 00:01:25,940 --> 00:01:28,420 or as a part of an Ansible collection, 31 00:01:28,420 --> 00:01:31,223 which you will find an Ansible two 2.10 and later. 32 00:01:32,730 --> 00:01:35,730 Now, how do we run these ad-hoc commands? 33 00:01:35,730 --> 00:01:37,930 Well, you use the Ansible commands 34 00:01:37,930 --> 00:01:39,750 to run the ad-hoc commands. 35 00:01:39,750 --> 00:01:41,880 And while running the Ansible commands, 36 00:01:41,880 --> 00:01:44,220 an inventory must be present, 37 00:01:44,220 --> 00:01:47,250 you must specify which hosts to address. 38 00:01:47,250 --> 00:01:49,349 You will also use a -m option to address 39 00:01:49,349 --> 00:01:53,900 specific module, a -a for the module arguments. 40 00:01:53,900 --> 00:01:55,120 An example for instance 41 00:01:55,120 --> 00:01:57,713 is Ansible - i inventory windows, 42 00:01:58,640 --> 00:02:00,740 which is addressing a host 43 00:02:00,740 --> 00:02:04,893 with the name windows- m ansible.windows.win_ping, 44 00:02:06,860 --> 00:02:08,860 which is the Ansible collection name 45 00:02:08,860 --> 00:02:11,320 of the module that you are going to use, 46 00:02:11,320 --> 00:02:13,000 to ping the windows host. 47 00:02:13,000 --> 00:02:18,000 Or ansible- i inventory all which is addressing all hosts, 48 00:02:18,470 --> 00:02:22,940 - m ansible.builtin.command-a reboot, 49 00:02:22,940 --> 00:02:26,300 which is going to use the command module, 50 00:02:26,300 --> 00:02:28,160 and this command module, 51 00:02:28,160 --> 00:02:31,310 as it is executing arbitrary Linux commands 52 00:02:31,310 --> 00:02:34,153 is next going to issue the Linux a reboot commands. 53 00:02:35,060 --> 00:02:36,170 Or for instance, 54 00:02:36,170 --> 00:02:40,970 ansible-i inventory rocky1-m ansible.builtin.user, 55 00:02:40,970 --> 00:02:43,480 which is using the user module on the host, 56 00:02:43,480 --> 00:02:48,120 with the name rocky1- a name is anna state is present 57 00:02:48,120 --> 00:02:49,680 which is going to make sure 58 00:02:49,680 --> 00:02:51,910 that this specific user is present. 59 00:02:51,910 --> 00:02:53,113 Let's go check this out, 60 00:02:55,430 --> 00:02:58,500 right, let's run one example, 61 00:02:58,500 --> 00:03:02,360 ansible-i inventory all. 62 00:03:02,360 --> 00:03:04,830 So we are going to run against all host 63 00:03:04,830 --> 00:03:09,287 - m ansible.builtin.command-a reboot. 64 00:03:12,770 --> 00:03:15,943 Well, no, let's do another, -a id. 65 00:03:21,060 --> 00:03:23,120 There we can see that the id command 66 00:03:23,120 --> 00:03:25,210 is giving us some results. 67 00:03:25,210 --> 00:03:27,430 Now, the reason I wanted to do the id 68 00:03:27,430 --> 00:03:29,113 before the reboot is this. 69 00:03:30,940 --> 00:03:33,240 do you see that connection refused connection 70 00:03:33,240 --> 00:03:36,280 refused, reachable through. 71 00:03:36,280 --> 00:03:37,160 Now why is that? 72 00:03:37,160 --> 00:03:41,000 That is because Ansible is running the reboot command, 73 00:03:41,000 --> 00:03:43,360 then it's requesting status information 74 00:03:43,360 --> 00:03:46,358 and it can't because it is rebooting. 75 00:03:46,358 --> 00:03:49,760 So once the hosts are back, 76 00:03:49,760 --> 00:03:53,900 then we can use a command like uptime for instance, 77 00:03:53,900 --> 00:03:58,813 to verify that the hosts have successfully rebooted. 78 00:04:00,050 --> 00:04:02,160 As you can see up zero minutes, 79 00:04:02,160 --> 00:04:04,080 zero minutes, zero minutes. 80 00:04:04,080 --> 00:04:07,920 Another example is ansible-i inventory. 81 00:04:07,920 --> 00:04:10,810 Do we really need minus i inventory? 82 00:04:10,810 --> 00:04:11,953 Let's go check it out, 83 00:04:11,953 --> 00:04:15,440 ansible.cfg as inventory, is inventory 84 00:04:15,440 --> 00:04:19,410 which means that ansible.cfg is going to look for a file 85 00:04:19,410 --> 00:04:21,800 with name inventory in the current directory. 86 00:04:21,800 --> 00:04:24,020 So we can make this comment a little bit shorter, 87 00:04:24,020 --> 00:04:29,020 ansible on rocky-m ansible.builtin.dot.user -a name=anna 88 00:04:37,590 --> 00:04:39,803 state=present, 89 00:04:43,830 --> 00:04:47,120 and oops that's a so called typo. 90 00:04:47,120 --> 00:04:48,880 Let's fix the typo and run it again, 91 00:04:48,880 --> 00:04:52,040 and now we can see it is giving us a change 92 00:04:52,040 --> 00:04:55,710 which means a user anna did not yet exist. 93 00:04:55,710 --> 00:04:58,830 Now, here we have used FQCN 94 00:04:58,830 --> 00:05:01,570 the fully qualified collection names. 95 00:05:01,570 --> 00:05:04,820 How about we just use the module name, 96 00:05:04,820 --> 00:05:07,240 there you can see that on Ansible 2.9 97 00:05:07,240 --> 00:05:09,940 that is perfectly working as well. 98 00:05:09,940 --> 00:05:12,390 The future is collections, but as long 99 00:05:12,390 --> 00:05:14,800 as you are on Ansible 2.9, you will find 100 00:05:14,800 --> 00:05:17,883 that the module names themselves will also work.