102 lines
2.5 KiB
Bash
Executable File
102 lines
2.5 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
CONFIG_FILE="ansible.cfg"
|
|
INVENTORY=$(awk -F '=' '/inventory/ {print $2}' $CONFIG_FILE | tr -d ' ')
|
|
PLAYBOOK="patch_servers.yml"
|
|
ANSIBLE_CMD="ansible-playbook -i $INVENTORY"
|
|
|
|
function update_all() {
|
|
$ANSIBLE_CMD $PLAYBOOK
|
|
}
|
|
|
|
function update_group() {
|
|
local group=$1
|
|
if grep -q "\[$group\]" $INVENTORY; then
|
|
$ANSIBLE_CMD -l $group $PLAYBOOK
|
|
else
|
|
echo "Error: Group '$group' not found in inventory."
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
function list_updates() {
|
|
ansible all -i $INVENTORY -m shell -a "if [ -f /etc/redhat-release ]; then yum check-update; elif [ -f /etc/debian_version ]; then apt list --upgradable; fi" | \
|
|
awk '/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ {print "\n----------------------------------"} {print}'
|
|
}
|
|
|
|
function list_servers() {
|
|
ansible-inventory -i $INVENTORY --list
|
|
}
|
|
|
|
function check_package_version() {
|
|
local package=$1
|
|
ansible all -i $INVENTORY -m shell -a "\
|
|
if [ -f /etc/redhat-release ]; then \
|
|
rpm -qa | grep $package; \
|
|
elif [ -f /etc/debian_version ]; then \
|
|
dpkg -l | grep $package; \
|
|
else \
|
|
echo 'Unknown distribution'; \
|
|
fi" | \
|
|
awk '/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/ {print "\n----------------------------------"} {print}'
|
|
}
|
|
|
|
function print_help() {
|
|
echo "Usage: $0 {--update {--all|--group <group>|--list}|--list|--check <package>}"
|
|
echo ""
|
|
echo "Options:"
|
|
echo " --update --all Update all servers in the inventory"
|
|
echo " --update --group <group> Update servers in the specified group"
|
|
echo " --update --list List all available updates"
|
|
echo " --list List all servers in the inventory"
|
|
echo " --check <package> Check the version of the specified package on all servers"
|
|
echo ""
|
|
echo "Examples:"
|
|
echo " $0 --update --all"
|
|
echo " $0 --update --group proxy"
|
|
echo " $0 --update --list"
|
|
echo " $0 --list"
|
|
echo " $0 --check openssh"
|
|
}
|
|
|
|
case "$1" in
|
|
--update)
|
|
case "$2" in
|
|
--all)
|
|
update_all
|
|
;;
|
|
--group)
|
|
if [ -z "$3" ]; then
|
|
echo "Please specify a group to update"
|
|
exit 1
|
|
fi
|
|
update_group $3
|
|
;;
|
|
--list)
|
|
list_updates
|
|
;;
|
|
*)
|
|
echo "Invalid option for --update"
|
|
print_help
|
|
exit 1
|
|
;;
|
|
esac
|
|
;;
|
|
--list)
|
|
list_servers
|
|
;;
|
|
--check)
|
|
if [ -z "$2" ]; then
|
|
echo "Please specify a package to check"
|
|
exit 1
|
|
fi
|
|
check_package_version $2
|
|
;;
|
|
*)
|
|
echo "Invalid option"
|
|
print_help
|
|
exit 1
|
|
;;
|
|
esac
|
|
|