Running Commands with the Command Module
One of the most commonly used modules in ad-hoc commands is the command
module. It allows you to run arbitrary commands on the target hosts.
Let's use the command
module to check the disk space on all hosts:
ansible all -i /home/labex/project/inventory -m command -a "df -h"
You should see output showing the disk usage on your local machine.
Now, let's try running a command only on the webservers group:
ansible webservers -i /home/labex/project/inventory -m command -a "uptime"
This command will show the uptime of all hosts in the webservers group.
The command
module is the default module, so you can omit -m command
if you're using this module. For example:
ansible dbservers -i /home/labex/project/inventory -a "free -m"
This command will show the memory usage on all hosts in the dbservers group.
Remember, the command
module doesn't support shell variables or operations like |
, >
, <
, &
. For those, you'd need to use the shell
module.