Wednesday, February 5, 2025

Ansible ad-hoc commands: How to use ad-hoc commands to checking Resource Availability in the Hosts



 Double-click on the image to zoom-out ...Larger.

To return to Home page: Refresh Page or Take ESC Button on Keyboard

How to check resource availability for hosts using ansible ad-hoc commands.

We can use Ansible ad-hoc commands to check resource availability (CPU, memory, disk, etc.) on managed hosts. Here are some common checks:

1. Check CPU Usage
ansible all -m shell -a "top -bn1 | grep 'Cpu(s)'" -i inventory.ini

or using /proc/stat:

ansible all -m command -a "cat /proc/stat | head -n 1" -i inventory.ini
2. Check Memory Usage
ansible all -m command -a "free -m" -i inventory.ini

This will display total, used, and available memory in MB.

3. Check Disk Usage
ansible all -m command -a "df -h" -i inventory.ini

This will list disk usage for all mounted filesystems.

To check a specific partition (e.g., /):

ansible all -m command -a "df -h /" -i inventory.ini
4. Check Available Inodes
ansible all -m command -a "df -i" -i inventory.ini

This checks the available inodes, useful for diagnosing filesystem issues.

5. Check Network Bandwidth Usage
ansible all -m shell -a "sar -n DEV 1 5" -i inventory.ini

(Requires sysstat package installed.)

6. Check Open File Descriptors
ansible all -m command -a "ulimit -n" -i inventory.ini
7. Check Uptime of Servers
ansible all -m command -a "uptime" -i inventory.ini

This helps monitor how long a server has been running.

8. Check Running Processes
ansible all -m shell -a "ps aux --sort=-%mem | head -n 10" -i inventory.ini

This lists the top 10 processes consuming the most memory.

9. Check Load Average
ansible all -m command -a "cat /proc/loadavg" -i inventory.ini

It displays the system’s load over the last 1, 5, and 15 minutes.

10. Check Open Ports
ansible all -m command -a "netstat -tulnp" -i inventory.ini

Lists active ports and services.

11. Check Swap Usage
ansible all -m command -a "swapon --summary" -i inventory.ini

This checks if swap is enabled and in use.

12. Check System Reboot Status
ansible all -m command -a "who -b" -i inventory.ini

Shows the last system boot time.

13. Check Logged-in Users
ansible all -m command -a "who" -i inventory.ini

Lists users currently logged in.

14. Check Available Package Updates

For Debian-based systems:

ansible all -m command -a "apt list --upgradable" -i inventory.ini

For RHEL-based systems:

ansible all -m command -a "yum check-update" -i inventory.ini
15. Check Firewall Status

For Ubuntu/Debian:

ansible all -m command -a "ufw status" -i inventory.ini

For RHEL/CentOS:

ansible all -m command -a "systemctl status firewalld" -i inventory.ini

 Running Commands as Root (with elevated privileges)

If a command requires root privileges, use -b (become) to execute it with sudo:

ansible all -m command -a "df -h" -b -i inventory.ini

 twtech-Thoughts:

These Ansible ad-hoc commands provide quick insights into system resource availability.

For continuous monitoring, you can integrate Ansible with Nagios, Prometheus, or Grafana

No comments:

Post a Comment

Serverless CRON Job | Overview.

  Serverless CRON Job Overview A serverless CRON job refers to a scheduled task that runs in a serverless environment — without needing to...