Ansible Modules |How to work with ad-hoc command

Hello, everyone In this Article we are going to learn How to work with ansible ad-hoc Modules in a very simple and easy step-by-step approach.

Yellow Fishing Boat Blog Banner 49

Ansible Modules

There are 2000+ modules in ansible it is not possible to explain each module soo we are making one article we will teach in this article that how we can work with different modules and how we can implement the modules in the daily life activities.

After reading this article we are sure that you have a very good understanding of the modules that are there in ansible and you can work with any module in ansible

There is no need to learn the functionality of every module we will work with the modules according to our need.

1 How to get a list of modules that are available – When we run the ansible-doc command with -l option then we will get the list of all modules that are available in the ansible.

# ansible-doc -l
Screenshot 1 20

2 How to count the total number of modules – If we want to know the total number of modules available then we can add wc -l command with ansible-doc when we run this command we can see that the total number of modules are there in the ansible currently we have 3400 total modules available in the ansible.

# ansible-doc -l | wc -l 
Screenshot 2 19

3 How to get documentation for a specific module = If we want to know the documentation for the specific module then we can specify the module name with the ansible-doc command. This will give the Examples/ Description etc. information for the module

# ansible-doc copy
Screenshot 3 18

Now we are learning How we can use the Modules using the ad-hoc command in Ansible

In this we are taking the example for the copy module Step by step I will teach how we can use the modules for the ad-hoc command

# ansible-doc copy = Run this command to get the full documentation for the copy module in ansible with the examples

First this will tell the brief introduction what is use of the copy module in ansible.

Then this will tells that what ate the Options available for the module

After that this will tell us that all the detailed examples of how we can use the copy module in the ansible .

After reading the Usage for the copy module and option come to the examples section in the doc

In the examples section we can see that there are large number of examples are there in my case just to teach I’m taking the Copy file with owner and permissions example

Screenshot 5 19

Here we can see that there are different options are there now we are going to learn how we can use this options in the ansible ad-hoc command

Now First let us see that where is the host file is located in the host file we added the IP address for the Target machine where we want to run the modules in ansible

# ansible –version = With this command we can see there where is the Configuration file is located

Our configuration file is located at /etc/ansible/ansible.cfg . Now when we see this configuration file we will come to know that here is our host file is located

Screenshot 6 18

After viewing this file using # cat /etc/ansible/ansible.cfg file we come to know that our host file is located at /etc/ansible/hosts

after doing # cat /etc/ansible/hosts command we can see that our target IP machine is added here

Screenshot 7 16

We are working with the Worker-1-Node Machine

Now we are executing the Ansible ad-hoc command for the copy module in the Worker-1-Node group machine.

# ansible Worker-1-Node -m copy -a 'src=/etc/passwd dest=/tmp'

In This command, we can see that Worker-1-Node is the group where we have defined the IP of the target machine and -m is used to specify what module we are going to implement and -a is used to determine the options we need to specify here there are number of option with different use case

Screenshot 8 16

Soo After reading the example section of the documentation of the copy module you will get to know about the options according to your need

But to implement the options in the ansible ad-hoc command we have to us the above syntax command

We can see that in the Worker-1-Node there is the file name as passwd is created. This file is coming to form the controller node and copies to the Machine that is in the Worker-1-Node group.

Screenshot 9 16

Important Note – Please note that when we run the ansible ad-hoc command above we can see that the color is yellow. This means that the task is performed successfully.

When we again run the same command then we can see that color is changed to green.

Screenshot 10 15

This means that Ansible doesn’t have changed anything on the Worker-1-Node it is just as it is.

Now when we see the red color this means that there is some problem occurs when we execute the command here I have changed the /passwd file name to /passed that not exist on the controller Node now we can see that we can get the red color

Screenshot 11 17

Now let us take another example that we need to change the permission to 777 again with the # ansible-doc copy command we can see the example here we find that there is the option for changing the permission also that is the mode option

Screenshot 12 16

Now when we run the ansible ad-hoc command we can add the mode inside the commad.

# ansible -m copy -a 'src=/etc/passwd dest=/tmp mode=007'
Screenshot 13 14

We can also see that now output is in yellow color this means that something is changed on the ManageNode1 group Machines

Now when we go to the ManageNode1 Machine and we can see the permisison for the passwd file the permission now become 777

Screenshot 14 11

You can use ad-hoc commands to run any module according to your requirements the concept will be same as above to run any module in ansible using ad-hoc command

Conclusion

This is the complete knowledge of How we can use ansible module using ad-hoc command if you have any doubts please feel free to comment below. Please don’t forget to join our email subscription to get the latest updates on the Linux post.

If you want to learn Ansible in the most simple language then please click on this link

If you want to learn Linux in the most simple language then please click on this link.

Leave a Comment