Ansible Handler
This project contains an Ansible playbook and a handler script to update servers to the latest patch level, check if a reboot is required, and manage the reboot process. The script and playbook handle both RPM-based (e.g., RedHat, CentOS, Rocky Linux) and Debian-based (e.g., Debian, Ubuntu) systems. Additionally, the playbook prints the uptime of each server at the end.
Files
patch_servers.yml
: Ansible playbook to update servers, check for required reboots, manage reboots, and print uptime.ansible_handler
: Shell script to handle various Ansible operations.hosts
: Ansible inventory file listing servers and groups.ansible.cfg
: Ansible configuration file.
Prerequisites
- Ansible installed on the control node.
- SSH access to all servers with key-based authentication.
Inventory File
The hosts
inventory file should contain your servers grouped accordingly. Example:
[all]
server1
server2
server3
[proxy]
proxy1
proxy2
[apache]
apache1
apache2
Configuration File
The ansible.cfg
file should be configured to use your inventory file and SSH private key. Example:
[defaults]
inventory = hosts
remote_user = your_user
private_key_file = /path/to/your/private_key
host_key_checking = False
Handler Script
The ansible_handler
script provides a command-line interface for various operations.
Usage
./ansible_handler {--update {--all|--group <group>|--list}|--list|--check <package>}
Options
--update --all
: Update all servers in the inventory.--update --group <group>
: Update servers in the specified group.--update --list
: List all available updates.--list
: List all servers in the inventory.--check <package>
: Check the version of the specified package on all servers.
Examples
-
Update all servers:
./ansible_handler --update --all
-
Update servers in the
proxy
group:./ansible_handler --update --group proxy
-
List all available updates:
./ansible_handler --update --list
-
List all servers in the inventory:
./ansible_handler --list
-
Check the version of the
openssh
package on all servers:./ansible_handler --check openssh
Ansible Playbook
The patch_servers.yml
playbook performs the following actions:
- Determines if the system is RPM or Debian based.
- Updates the system packages.
- Checks if a reboot is required.
- Reboots the system if required, unless Elasticsearch is installed.
- Notifies the user if a reboot is required but Elasticsearch is installed.
- Prints the uptime of each server.
Running the Playbook
To run the playbook manually:
ansible-playbook -i hosts patch_servers.yml
Notes
- Modify the
ansible.cfg
andhosts
files according to your environment.
License
This project is licensed under the MIT License.
This `README.md` provides an overview of the project, instructions on configuring and using the handler script and playbook, and examples of common operations. Adjust the content as needed for your specific environment and requirements.