1 00:00:00,000 --> 00:00:10,560 In this lesson, you will learn about Ansible ad hoc commands. 2 00:00:10,560 --> 00:00:14,540 So Ansible provides more than 3,000 different modules. 3 00:00:14,540 --> 00:00:18,219 And about 70 modules are provided in the Ansible core package. 4 00:00:18,219 --> 00:00:23,240 So you'll get 70 modules more or less after installing Ansible core. 5 00:00:23,240 --> 00:00:27,600 If you need additional modules, you can get them as a content collection. 6 00:00:27,600 --> 00:00:34,639 The content collections are provided through the Ansible community website on https.galaxy.exe. 7 00:00:34,639 --> 00:00:41,480 And Ansible content collections are provided from the community website galaxy.ansible.com. 8 00:00:41,480 --> 00:00:44,480 Modules in Ansible provide specific functionality. 9 00:00:44,480 --> 00:00:47,320 And they run as Python scripts on the managed nodes. 10 00:00:47,320 --> 00:00:50,880 That's also where the Python requirement comes from. 11 00:00:50,880 --> 00:00:54,740 You can use Ansible doc-l for a list of all modules. 12 00:00:54,740 --> 00:00:58,220 And modules can be used in ad hoc commands. 13 00:00:58,220 --> 00:01:07,419 For instance, you can use ansible-ubuntu-i-inventory-u-student-b indicates that you want to use bcomprivileges, 14 00:01:07,419 --> 00:01:14,500 minus-k prompts for the bcompassword, minus-m user is using the user module, and minus-a 15 00:01:14,500 --> 00:01:18,860 name is Linda, will add a user with the name Linda. 16 00:01:18,860 --> 00:01:23,620 And likewise, here you see the package module, where the package module is a very flexible 17 00:01:23,620 --> 00:01:28,779 module that allows you to install packages on any Linux distribution. 18 00:01:28,779 --> 00:01:32,180 Apart from these modules, there's also the command module. 19 00:01:32,180 --> 00:01:37,580 If for a specific task you can't find a specific Ansible module, you can always run it as a 20 00:01:37,580 --> 00:01:40,660 Linux command in the command module. 21 00:01:40,660 --> 00:01:44,980 But the golden rule in Ansible is use always the most Ansible way. 22 00:01:44,980 --> 00:01:48,500 So if you have a specific module, you should be using that. 23 00:01:48,500 --> 00:01:52,139 Let me demonstrate these two examples on the slide. 24 00:01:52,139 --> 00:02:09,380 So I'm going to use ansible-ubuntu-i-inventory-u-student-b, I want to do this using my current user account, 25 00:02:09,380 --> 00:02:14,259 minus-b, I want to use bcomprivileges, and minus-k, I want Ansible to prompt for the 26 00:02:14,259 --> 00:02:15,259 bcompassword. 27 00:02:15,259 --> 00:02:24,300 And next, minus-m, user is calling the user module, and minus-a, name is Linda, is providing 28 00:02:24,300 --> 00:02:27,539 the argument with the user name. 29 00:02:27,539 --> 00:02:33,339 And that is going to make sure that this user will be created on the remote machine. 30 00:02:33,339 --> 00:02:38,580 Now we can see the output of the Ansible module, which is telling me Ansible changed through, 31 00:02:38,580 --> 00:02:41,539 so it has successfully created the user. 32 00:02:41,539 --> 00:02:46,779 Now the fun thing is, if I run the same module again, it is not going to complain, it's just 33 00:02:46,779 --> 00:02:53,500 telling me, hey, it changed false, the user already existed, no works, was required. 34 00:02:53,500 --> 00:02:59,059 This is what we call idempotency, meaning that if the current state on the managed host 35 00:02:59,059 --> 00:03:03,660 already meets the desired state, nothing has to be done. 36 00:03:03,660 --> 00:03:11,979 Now let me change this command a little bit, and make it minus-m, packets, minus-a, name 37 00:03:11,979 --> 00:03:17,580 is nmap, that's going to install the nmap packets on the remote host. 38 00:03:17,580 --> 00:03:21,740 Using modules that install packages may take a little bit of time, but here we can see 39 00:03:21,740 --> 00:03:23,059 it's already done. 40 00:03:23,059 --> 00:03:26,740 So that's how you use Ansible modules in Ansible ad hoc mode.