Tuesday, March 18, 2025

Ansible ad-hoc commands: How to use ad-hoc commands to check 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

NB: The same results are got from these two commands. the first is
longer and the second is shorter.
All the ad-hoc commands herein can be executed without -m command

ansible all -m command -a "df -h"
and
ansible all -a "df -h"

Explanation:

ansible all -m command -a "df -h"

    • This explicitly specifies the command module and passes the argument "df -h".
    • The command module is used to execute commands on remote hosts but does not process shell operators like |,>,&&,etc.

ansible all -a "df -h"

    • This is a shorthand version where, if no module is specified, Ansible uses the command module by default.
    • Since -m command is omitted, Ansible implicitly assumes the command module.

twtech insights:

Both commands will produce the same result, because Ansible defaults to the command module when no module is explicitly provided. However, if you needed shell features like piping (|) or redirections (>,>>), you would need to use the shell module instead.

# bash
 ansible all -m shell -a "df -h | grep '/dev/sda1'"

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:

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

For continuous monitoring, twtech  integrates Ansible with:

 Prometheus, 

Grafana,

 Datadog, 

CloudWatch,

ELK (elasticsearch logstash and kibana) 

Trivy operator. 

No comments:

Post a Comment

Kubernetes Clusters | Upstream Vs Downstream.

  The terms "upstream" and "downstream" in the context of Kubernetes clusters often refer to the direction of code fl...