Enhance Efficiency: Server Monitoring Simplified with Ansible and CPU, RAM, Disk

Subhomay Banerjee
3 min readJul 18, 2023

--

Introduction:

Server monitoring is a critical aspect of maintaining the health and performance of your infrastructure. It allows you to identify potential bottlenecks, preemptively address issues, and optimize resource utilization. Ansible, a powerful automation tool, can simplify the process of monitoring CPU, RAM, and disk usage across multiple servers simultaneously. In this blog post, we’ll explore how to create an Ansible script to monitor these vital metrics effortlessly.

Prerequisites:

Before diving into the Ansible script, ensure the following prerequisites are met:

  1. Ansible Installation: Make sure Ansible is installed on the control machine from which you will run the script.
  2. SSH Access: Ensure that the control machine can SSH into the target servers using passwordless authentication or SSH keys.

Step 1: Set Up the Inventory:

Create an Ansible inventory file (e.g., host) containing the list of servers you want to monitor. Define the server hostnames or IP addresses under appropriate groups based on your server's roles (e.g., web_servers, database_servers, etc.). Here is an example of the host file:

[web_server]
Web-1 ansible_host=<IP> ansible_user=<username> ansible_ssh_private_key_file=/key/file/path
Web-2 ansible_host=<IP> ansible_user=<username> ansible_ssh_private_key_file=/key/file/path
Web-3 ansible_host=<IP> ansible_user=<username> ansible_ssh_private_key_file=/key/file/path

[database_server]
DB-1 ansible_host=<IP> ansible_user=<username> ansible_ssh_private_key_file=/key/file/path
DB-2 ansible_host=<IP> ansible_user=<username> ansible_ssh_private_key_file=/key/file/path
DB-3 ansible_host=<IP> ansible_user=<username> ansible_ssh_private_key_file=/key/file/path

Step 2: Create the Playbook:

Create an Ansible playbook (e.g., monitoring.yml) to define the tasks for monitoring CPU, RAM, and disk usage. The playbook should include the following tasks:

---
- name: Gather CPU RAM and Disk Space Usage
hosts: all
gather_facts: yes
tasks:
- name: Gather CPU usage
shell: top -bn1 | grep 'Cpu(s)' | awk '{print $2}'
register: cpu_usage
- name: Gather RAM Usage
shell: top -bn1 | awk '/MiB Mem/ {printf "%.2f\n", $8/$4 * 100}'
register: ram_usage
- name: Gather Disk Space
shell: "df -h --output=pcent / | awk 'NR==2 {print $1}'"
register: disk_space
- name: Usage Stats
debug:
msg:
- "Executed On: {{ ansible_date_time.date }} {{ ansible_date_time.hour }}:{{ ansible_date_time.minute }}"
- "CPU Usage: {{ cpu_usage.stdout }}%"
- "RAM Usage: {{ ram_usage.stdout }}%"
- "Disk Space Used: {{ disk_space.stdout }}"

Step 3: Run the Ansible script:

Once the host and monitor.yml file is ready, it is important now to run the script to gather the information and display it for us to monitor. To do so:

ansible-playbook -i host monitoring.yml

Step 4: Troubleshoot error:

If you see that there is an error while running the ansible-playbook which says:

ERROR: Ansible could not initialize the preferred locale: unsupported locale setting

Then set the locale to Language USA English:

export LANG="en_US.UTF-8"

Once you set the locale, then run the ansible-playbook command.

Step 5: Parsing and Storing Metrics:

To ensure easy access to the collected metrics, use Ansible’s template module to create a report template. Parse the gathered data from each server and populate the template with the relevant information.

You can store the compiled data in a central location on the control machine or upload it to a cloud-based storage service for historical analysis and trend monitoring.

Step 6: Schedule the Playbook Execution:

To automate the monitoring process, schedule the execution of the Ansible playbook using cron jobs or any task scheduler suitable for your operating system. Set the desired interval for monitoring, based on the frequency you need to check your server metrics.

Step 7: Alerts and Notifications (Optional):

For proactive monitoring, consider setting up alerting and notification mechanisms. Use Ansible to trigger alerts when certain thresholds are exceeded, and send notifications via email, Slack, or other communication channels.

Conclusion:

Automating server monitoring with Ansible allows you to efficiently keep track of CPU, RAM, and disk usage across multiple servers simultaneously. By creating a centralized monitoring system, you can promptly detect issues, optimize resource utilization, and ensure the smooth operation of your infrastructure. Embrace the power of Ansible, and elevate your server monitoring to new heights of efficiency and proactive management. Happy monitoring!

--

--

Subhomay Banerjee

I have a huge interest in technology. Over 15+ years of experience in Information Technology with expertise in DevOps.