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,759
You can use Ansible doc-l for a list of all modules.

12
00:00:54,759 --> 00:00:58,080
Modules can be used in ad-hoc commands.

13
00:00:58,080 --> 00:01:03,160
For instance, you can use ansible-ubuntu-i-inventory-u-student.

14
00:01:03,160 --> 00:01:07,120
Minus B indicates that you want to use BCOM privileges.

15
00:01:07,120 --> 00:01:10,040
Minus K prompts for the BCOM password.

16
00:01:10,040 --> 00:01:13,680
Minus M, user is using the user module.

17
00:01:13,680 --> 00:01:15,959
And minus A, name is Linda.

18
00:01:15,959 --> 00:01:18,760
We'll add a user with the name Linda.

19
00:01:18,760 --> 00:01:24,040
And likewise, here you see the package module, where the package module is a very flexible module

20
00:01:24,040 --> 00:01:28,639
that allows you to install packages on any Linux distribution.

21
00:01:28,639 --> 00:01:32,040
Apart from these modules, there's also the command module.

22
00:01:32,040 --> 00:01:36,400
If for a specific task you can't find a specific Ansible module,

23
00:01:36,400 --> 00:01:40,440
you can always run it as a Linux command in the command module.

24
00:01:40,440 --> 00:01:44,839
But the golden rule in Ansible is use always the most Ansible way.

25
00:01:44,839 --> 00:01:48,360
So if you have a specific module, you should be using that.

26
00:01:48,360 --> 00:01:52,000
Let me demonstrate these two examples on the slide.

27
00:01:52,000 --> 00:01:54,919
So I'm going to use ansible-ubuntu.

28
00:01:54,919 --> 00:01:57,639
Ubuntu is the host that I want to manage.

29
00:01:57,639 --> 00:02:00,879
Minus I, inventory.

30
00:02:00,879 --> 00:02:04,480
So I'm using the inventory file in my current directory.

31
00:02:04,480 --> 00:02:06,160
Minus U, student.

32
00:02:06,160 --> 00:02:09,000
I want to do this using my current user account.

33
00:02:09,000 --> 00:02:11,559
Minus B, I want to use BCOM privileges.

34
00:02:11,559 --> 00:02:15,759
And minus K, I want Ansible to prompt for the BCOM password.

35
00:02:15,759 --> 00:02:19,520
And next, minus M, user is calling the user module.

36
00:02:19,559 --> 00:02:23,559
And minus A, name is Linda,

37
00:02:23,559 --> 00:02:27,399
is providing the argument with the user name.

38
00:02:27,399 --> 00:02:30,880
And that is going to make sure that this user will be created

39
00:02:30,880 --> 00:02:33,199
on the remote machine.

40
00:02:33,199 --> 00:02:35,360
Now we can see the output of the Ansible module,

41
00:02:35,360 --> 00:02:38,479
which is telling me Ansible changed, changed true.

42
00:02:38,479 --> 00:02:41,399
So it has successfully created the user.

43
00:02:41,399 --> 00:02:44,399
Now the fun thing is, if I run the same module again,

44
00:02:44,399 --> 00:02:46,240
it is not going to complain.

45
00:02:46,240 --> 00:02:49,039
It's just telling me, hey, it changed false.

46
00:02:49,039 --> 00:02:50,800
The user already existed.

47
00:02:50,800 --> 00:02:53,360
No works was required.

48
00:02:53,360 --> 00:02:55,759
This is what we call idempotency,

49
00:02:55,759 --> 00:02:59,080
meaning that if the current state on the managed host

50
00:02:59,080 --> 00:03:03,559
already meets the desired state, nothing has to be done.

51
00:03:03,559 --> 00:03:06,839
Now let me change this command a little bit

52
00:03:06,839 --> 00:03:10,440
and make it minus M, packets.

53
00:03:10,440 --> 00:03:13,399
Minus A, name is nmap.

54
00:03:13,440 --> 00:03:17,240
That's going to install the nmap package on the remote host.

55
00:03:17,240 --> 00:03:19,160
Using modules that install packages

56
00:03:19,160 --> 00:03:20,759
may take a little bit of time,

57
00:03:20,759 --> 00:03:23,000
but here we can see it's already done.

58
00:03:23,000 --> 00:03:25,199
So that's how you use Ansible modules

59
00:03:25,199 --> 00:03:26,759
in Ansible ad hoc mode.

