In this lesson, you will learn about Ansible ad-hoc commands.
So Ansible provides more than 3,000 different modules.
And about 70 modules are provided in the Ansible core package.
So you'll get 70 modules more or less after installing Ansible core.
If you need additional modules, you can get them as a content collection.
The content collections are provided through the Ansible community website on https.galaxy.exe.
And Ansible content collections are provided from the community website galaxy.ansible.com.
Modules in Ansible provide specific functionality.
And they run as Python scripts on the managed nodes.
That's also where the Python requirement comes from.
You can use Ansible doc-l for a list of all modules.
Modules can be used in ad-hoc commands.
For instance, you can use ansible-ubuntu-i-inventory-u-student.
Minus B indicates that you want to use BCOM privileges.
Minus K prompts for the BCOM password.
Minus M, user is using the user module.
And minus A, name is Linda.
We'll add a user with the name Linda.
And likewise, here you see the package module, where the package module is a very flexible module
that allows you to install packages on any Linux distribution.
Apart from these modules, there's also the command module.
If for a specific task you can't find a specific Ansible module,
you can always run it as a Linux command in the command module.
But the golden rule in Ansible is use always the most Ansible way.
So if you have a specific module, you should be using that.
Let me demonstrate these two examples on the slide.
So I'm going to use ansible-ubuntu.
Ubuntu is the host that I want to manage.
Minus I, inventory.
So I'm using the inventory file in my current directory.
Minus U, student.
I want to do this using my current user account.
Minus B, I want to use BCOM privileges.
And minus K, I want Ansible to prompt for the BCOM password.
And next, minus M, user is calling the user module.
And minus A, name is Linda,
is providing the argument with the user name.
And that is going to make sure that this user will be created
on the remote machine.
Now we can see the output of the Ansible module,
which is telling me Ansible changed, changed true.
So it has successfully created the user.
Now the fun thing is, if I run the same module again,
it is not going to complain.
It's just telling me, hey, it changed false.
The user already existed.
No works was required.
This is what we call idempotency,
meaning that if the current state on the managed host
already meets the desired state, nothing has to be done.
Now let me change this command a little bit
and make it minus M, packets.
Minus A, name is nmap.
That's going to install the nmap package on the remote host.
Using modules that install packages
may take a little bit of time,
but here we can see it's already done.
So that's how you use Ansible modules
in Ansible ad hoc mode.