Ansible Automated Server Inspection Guide (Part 2)

Using Ansible to implement automated inspections allows you to quickly understand the health status of your server cluster, making it ideal for daily operations and troubleshooting. Below, I will introduce how to quickly check server status and provide a solution for generating a visual inspection report.

Quickly Check Server Status

When you need to quickly understand core metrics of the server (such as CPU, memory, and disk), a concise Ansible Playbook can achieve this.

  1. Create a Playbook file, for example, check_status.yml, with the following content:

yaml

– name: Quickly Check Server Status

hosts: all # For all hosts defined in the inventory

gather_facts: no # To speed up, do not collect facts

tasks:

– name: Get CPU, memory, and disk usage

shell: |

echo “Hostname: $(hostname)”

echo “CPU usage: $(top -bn1 | grep ‘Cpu’ | awk ‘{print $2}’)”

echo “Memory usage: $(free -m | awk ‘/Mem:/ {printf “Used: %sMB, Total: %sMB”, $3, $2}’)”

echo “Root partition disk usage: $(df -h / | awk ‘NR==2 {print $5}’)”

register: server_status # Register execution results

– name: Output status information

debug:

msg: “{{ inventory_hostname }} status: {{ server_status.stdout_lines }}”

  1. Execute the check: Run the following command in the terminal, Ansible will connect to all target servers to perform the check and return results.

bash

ansible-playbook -i your_inventory_file check_status.yml

Execution Effect: Typically, within 10 seconds to 1 minute, you will see an overview of the core status of all servers in the terminal, which is very suitable for quick troubleshooting.

📊Generate Detailed Inspection Report

If you need a detailed report that includes multiple metrics, is readable, and can be archived or emailed, you can utilize mature Ansible Roles, such as ansible-HealthCheck or os-check roles.

Such solutions typically:

  • Check more comprehensive metrics: Including hostname, main IP, operating system version, CPU usage and load, memory and Swap usage, disk capacity and Inode usage, TCP connection counts, etc.
  • Generate intuitive HTML reports: By using Jinja2 templates to render the collected data into HTML pages, the data will be highlighted in different colors based on preset thresholds (for example, usage below 80% is normal, 80%-90% is a warning, and above 90% is critical), making it easy to quickly identify issues.
  • Support email sending: After configuring SMTP information, the generated report can be directly sent to a specified email.

A typical usage process is as follows:

  1. Obtain the role: Get the relevant Role from platforms like GitHub , such as os-check or ansible-HealthCheck.
  2. Place roles and plugins: Place the Role files in your Ansible project directory under the roles folder. Depending on project requirements, you may also need to copy specific filter plugins (Filter Plugins) to the filter_plugins directory recognized by Ansible .
  3. Configure and execute:
  • Create or modify your Playbook (such as os_check.yaml), reference the role, and set necessary variables, such as report output path, mail server information, recipients, etc.

yaml

– name: Server Inspection

hosts: all # or your specific host group

gather_facts: false

vars:

check_report_path: /tmp # Temporary report storage path

check_mail_host: “smtp.163.com” # Email server

check_mail_port: “465”

check_mail_username: “[email protected]

check_mail_password: “your_password”

check_mail_to: [ “[email protected]” ]

roles:

– os-check # Reference role

  • Run the Playbook:

bash

ansible-playbook -i your_inventory_file os_check.yaml

  1. View the report: After successful execution, you can find the generated HTML report file in the defined path (such as /tmp), or check your email for the report.

💡Advanced Tips and Tricks

  • Scheduled Automatic Inspections: You can use crontab to set up scheduled tasks, allowing Ansible inspection scripts to run automatically at specific times daily or weekly, achieving true unattended inspections.

bash

# For example, run the inspection once every day at 9 AM

0 9 * * * /usr/bin/ansible-playbook -i /path/to/your/inventory /path/to/your/check_playbook.yaml

  • Custom Inspection Content: You can completely modify the tasks in the Playbook or the scripts in the Role to check any items of interest, such as the status of specific services (Nginx, MySQL), error messages in logs, etc.
  • Pay Attention to Permissions and Security: The account executing the Ansible Playbook needs to have the appropriate permissions on the target server (for example, sudo permissions to execute specific commands). For email sending, please keep the email password secure, and it is recommended to use an authorization code.

🤔How to Choose?

  • If you need to quickly answerWhat is the overall status of the server now?, use the quick check script.
  • If you need a detailed, visual, reportable, or archivable record, or need to monitor trends regularly, then deploying a complete inspection report solution is a better choice.

I hope this information helps everyone quickly get started with Ansible automated inspections. If you can share more about your specific operational environment (such as server scale, main services or metrics of concern), I might be able to provide more specific advice.

#Ansible

#Automated Operations

#Server Inspection

Leave a Comment