Ansible command line helps you to meet your requirement without creating & running playbook.
The purpose of Ansible command is basically:
--> run a single task
--> helpful to run some ad-hoc commands e.g., server uptime, date, filesystem usage report etc.
syntax: ansible <host-pattern> [options]
-u REMOTE_USER
-m MODULE_NAME
-a module arguments
-i specify inventory host path or comma separated host
-s run operations with sudo
Examples:
1) Get uptime of remote servers:
# ansible all -m command -a uptime
192.168.100.2 | CHANGED | rc=0 >>
06:04:05 up 3 days, 18:03, 1 user, load average: 1.49, 1.12, 1.03
192.168.100.1 | CHANGED | rc=0 >>
06:04:05 up 3 days, 18:02, 1 user, load average: 1.01, 1.06, 1.08
2) Show hostnames of remote server:
# ansible all -m command -a /usr/bin/hostname
192.168.100.1 | CHANGED | rc=0 >>
example01
192.168.100.2 | CHANGED | rc=0 >>
example02
3) See remote server OS version:
# ansible all -m command -a "cat /etc/redhat-release"
192.168.100.2 | CHANGED | rc=0 >>
Red Hat Enterprise Linux Server release 7.8 (Maipo)
192.168.100.1 | CHANGED | rc=0 >>
Red Hat Enterprise Linux Server release 7.8 (Maipo)
4) See file system usage:
# ansible all -m command -a "df -h /u01"
192.168.100.2 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 300G 183G 118G 61% /u01
192.168.100.1 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
/dev/sdb1 300G 192G 109G 64% /u01
5) See remote server dates:
# ansible all -m command -a "date"
192.168.100.2 | CHANGED | rc=0 >>
Thu Nov 18 06:08:13 GMT 2021
192.168.100.1 | CHANGED | rc=0 >>
Thu Nov 18 06:08:13 GMT 2021