Here let’s discuss few ansible modules using adhoc commandline.
Before getting into the topic of “ how to run Linux commands on Ad Hoc commandline ?”, let me show you how these system commands are executed in Linux commandLine.
For example, let’s take up a question-
How to check whether httpd daemon is active or not in linux command line ?
In Linux Commandline, we use systemctl command to check the daemon status..
Syntax:- systemctl status daemonName
Example :- systemctl status httpd
An Another way.
Syntax :- systemctl is-active daemonName
Example :- systemctl is-active httpd
Now how to use this system command in adhoc commandline?
To run Linux command, we can use any of these Ansible Modules called command module and shell Module.
Before moving to adhoc commandline, let us have a quick look on my system setups.
The below snaps shows
My control system name is vsk-VirtualBox , in my inventory file group name is called webserver, Under the webserver group there are two nodes named host01 & student.
Now If I want to test whether my nodes are connected or not , we can use ping module.
Yes all the nodes are reachable, so we can communicate to each node.
Now how to run linux command in adhoc way .
ansible hostname –m command –a “systemctl status httpd”
ansible hostname –m shell –a “systemctl status httpd”
Another way :-
ansible hostname –m command –a “systemctl is-active httpd”
ansible hostname -m shell –a “systemctl is-active httpd”
So if you want to run Linux system commands in adhoc commandline, use command module or shell module.
If you want to know more about the difference between command and shell module, watch this hands-on video.
So far we know how to check the daemon status.
Now if it is not active, lets see how to start in ansible way !!.
Just like command and shell module, we have an another module called service module.
The service module is used to start/ stop the daemon service.
Now let’s see how can we use this..
ansible hostname –m service –a “name=httpd state=started”
The above command starts the all the machines under the hostname group.
In order to stop the particular daemon, use stopped as state field entry in module argument as follows.
ansible hostname –m service –a “name= httpd state= stopped”
-m refers module option
- a refers arbitrary module argument
We have discussed 4 modules- ping, command, shell and service modules.
- ping module is used to test the control nodes are reachable or not .
- command and shell module are specific for Linux system commands
- service module is meant to start/ stop daemon service.
command and shell module are specific for Linux system commands whereas service module is meant to start/ stop daemon service.
Ansible & its Features