chore(ansible): 重构inventory.ini分组和节点配置
- 按地理和服务划分为多个组,如oci_kr, oci_us, huawei, google等 - 添加了多台主机的详细连接和权限配置 - 修正并移除旧的oci组,替换为更细化的组划分 - 规范了lxc、vm、hcp及feiniu等组的节点配置 - 新增armbian组及对应节点onecloud1配置 - 保留通用变量ansible_ssh_common_args不变 - 删除try.md文件中的无关内容记录
This commit is contained in:
parent
d2a88cef3b
commit
9a453b984f
|
|
@ -0,0 +1,72 @@
|
||||||
|
---
|
||||||
|
- name: Cloud Providers System Update Playbook
|
||||||
|
hosts: huawei,google,ditigalocean,aws
|
||||||
|
become: yes
|
||||||
|
gather_facts: yes
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
# Ubuntu/Debian 系统更新 (apt)
|
||||||
|
- name: Update apt cache (Ubuntu/Debian)
|
||||||
|
apt:
|
||||||
|
update_cache: yes
|
||||||
|
cache_valid_time: 3600
|
||||||
|
when: ansible_os_family == "Debian"
|
||||||
|
|
||||||
|
- name: Upgrade all packages (Ubuntu/Debian)
|
||||||
|
apt:
|
||||||
|
upgrade: yes
|
||||||
|
autoremove: yes
|
||||||
|
autoclean: yes
|
||||||
|
when: ansible_os_family == "Debian"
|
||||||
|
register: apt_upgrade_result
|
||||||
|
|
||||||
|
# AWS Linux 系统更新 (dnf)
|
||||||
|
- name: Update dnf cache (AWS Linux/RHEL)
|
||||||
|
dnf:
|
||||||
|
update_cache: yes
|
||||||
|
when: ansible_os_family == "RedHat"
|
||||||
|
|
||||||
|
- name: Upgrade all packages (AWS Linux/RHEL)
|
||||||
|
dnf:
|
||||||
|
name: "*"
|
||||||
|
state: latest
|
||||||
|
skip_broken: yes
|
||||||
|
when: ansible_os_family == "RedHat"
|
||||||
|
register: dnf_upgrade_result
|
||||||
|
|
||||||
|
# 显示升级结果
|
||||||
|
- name: Display apt upgrade results
|
||||||
|
debug:
|
||||||
|
msg: "APT system upgrade completed. {{ apt_upgrade_result.changed }} packages were updated."
|
||||||
|
when: ansible_os_family == "Debian" and apt_upgrade_result is defined
|
||||||
|
|
||||||
|
- name: Display dnf upgrade results
|
||||||
|
debug:
|
||||||
|
msg: "DNF system upgrade completed. {{ dnf_upgrade_result.changed }} packages were updated."
|
||||||
|
when: ansible_os_family == "RedHat" and dnf_upgrade_result is defined
|
||||||
|
|
||||||
|
# 检查是否需要重启 (Ubuntu/Debian)
|
||||||
|
- name: Check if reboot is required (Ubuntu/Debian)
|
||||||
|
stat:
|
||||||
|
path: /var/run/reboot-required
|
||||||
|
register: debian_reboot_required
|
||||||
|
when: ansible_os_family == "Debian"
|
||||||
|
|
||||||
|
# 检查是否需要重启 (AWS Linux/RHEL)
|
||||||
|
- name: Check if reboot is required (AWS Linux/RHEL)
|
||||||
|
command: needs-restarting -r
|
||||||
|
register: rhel_reboot_required
|
||||||
|
failed_when: false
|
||||||
|
changed_when: false
|
||||||
|
when: ansible_os_family == "RedHat"
|
||||||
|
|
||||||
|
# 通知重启信息
|
||||||
|
- name: Notify if reboot is required (Ubuntu/Debian)
|
||||||
|
debug:
|
||||||
|
msg: "System reboot is required to complete the update."
|
||||||
|
when: ansible_os_family == "Debian" and debian_reboot_required.stat.exists is defined and debian_reboot_required.stat.exists
|
||||||
|
|
||||||
|
- name: Notify if reboot is required (AWS Linux/RHEL)
|
||||||
|
debug:
|
||||||
|
msg: "System reboot is required to complete the update."
|
||||||
|
when: ansible_os_family == "RedHat" and rhel_reboot_required.rc == 1
|
||||||
|
|
@ -2,13 +2,56 @@
|
||||||
dev1 ansible_host=dev1 ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
dev1 ansible_host=dev1 ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
dev2 ansible_host=dev2 ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
dev2 ansible_host=dev2 ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
|
|
||||||
[oci]
|
[oci_kr]
|
||||||
dev1 ansible_host=dev1 ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
ch2 ansible_host=ch2 ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
dev2 ansible_host=dev2 ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
ch3 ansible_host=ch3 ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
|
master ansible_host=master ansible_port=60022 ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
|
|
||||||
|
[oci_us]
|
||||||
|
ash1d ansible_host=ash1d ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
|
ash2e ansible_host=ash2e ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
|
ash3c ansible_host=ash3c ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
|
[huawei]
|
||||||
|
hcs ansible_host=hcs ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
|
[google]
|
||||||
|
benwork ansible_host=benwork ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
|
|
||||||
[testlocal]
|
[ditigalocean]
|
||||||
localhost ansible_connection=local
|
syd ansible_host=syd ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
|
[aws]
|
||||||
|
#aws linux dnf
|
||||||
|
awsirish ansible_host=awsirish ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
|
|
||||||
|
[proxmox]
|
||||||
|
pve ansible_host=pve ansible_user=root ansible_become=yes ansible_become_pass=Aa313131@ben
|
||||||
|
xgp ansible_host=xgp ansible_user=root ansible_become=yes ansible_become_pass=Aa313131@ben
|
||||||
|
nuc12 ansible_host=nuc12 ansible_user=root ansible_become=yes ansible_become_pass=Aa313131@ben
|
||||||
|
|
||||||
|
[lxc]
|
||||||
|
#集中在三台机器,不要同时upgrade 会死掉,顺序调度来
|
||||||
|
warden ansible_host=warden ansible_user=ben ansible_become=yes ansible_become_pass=3131
|
||||||
|
gitea ansible_host=gitea ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
redis ansible_host=redis ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
influxdb ansible_host=influxdb ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
mysql ansible_host=mysql ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
postgresql ansible_host=postgresql ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
kali ansible_host=kali ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
authentik ansible_host=authentik ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
kali ansible_host=kali ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
calibreweb ansible_host=kali ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
calibreweb ansible_host=calibreweb ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
[vm]
|
||||||
|
kali ansible_host=kali ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
|
||||||
|
[hcp]
|
||||||
|
hcp1 ansible_host=hcp1 ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
hcp2 ansible_host=hcp2 ansible_user=root ansible_become=yes ansible_become_pass=313131
|
||||||
|
|
||||||
|
[feiniu]
|
||||||
|
snail ansible_host=snail ansible_user=houzhongxu ansible_ssh_pass=Aa313131@ben ansible_become=yes ansible_become_pass=Aa313131@ben
|
||||||
|
|
||||||
|
[armbian]
|
||||||
|
onecloud1 ansible_host=onecloud1 ansible_user=ben ansible_ssh_pass=3131 ansible_become=yes ansible_become_pass=3131
|
||||||
|
|
||||||
[all:vars]
|
[all:vars]
|
||||||
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
|
ansible_ssh_common_args='-o StrictHostKeyChecking=no'
|
||||||
|
|
@ -0,0 +1,123 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Ansible Playbook Runner Script
|
||||||
|
# Usage: ./run.sh -dev (or any group name)
|
||||||
|
|
||||||
|
# Set script directory
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
|
INVENTORY_FILE="$SCRIPT_DIR/inventory.ini"
|
||||||
|
PLAYBOOK_FILE="$SCRIPT_DIR/system-update.yml"
|
||||||
|
|
||||||
|
# Function to display usage
|
||||||
|
show_usage() {
|
||||||
|
echo "Usage: $0 -<group_name>"
|
||||||
|
echo ""
|
||||||
|
echo "Examples:"
|
||||||
|
echo " $0 -dev # Run on dev group (dev1, dev2)"
|
||||||
|
echo " $0 -prod # Run on prod group"
|
||||||
|
echo " $0 -all # Run on all hosts"
|
||||||
|
echo ""
|
||||||
|
echo "Available groups in inventory:"
|
||||||
|
grep '^\[' "$INVENTORY_FILE" | grep -v ':vars' | sed 's/\[//g' | sed 's/\]//g' | sort
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to check if group exists in inventory
|
||||||
|
check_group_exists() {
|
||||||
|
local group_name="$1"
|
||||||
|
if [ "$group_name" = "all" ]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if grep -q "^\[$group_name\]" "$INVENTORY_FILE"; then
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Function to run ansible playbook
|
||||||
|
run_playbook() {
|
||||||
|
local group_name="$1"
|
||||||
|
|
||||||
|
echo "========================================="
|
||||||
|
echo "Running Ansible Playbook on group: $group_name"
|
||||||
|
echo "========================================="
|
||||||
|
echo "Inventory: $INVENTORY_FILE"
|
||||||
|
echo "Playbook: $PLAYBOOK_FILE"
|
||||||
|
echo "Target: $group_name"
|
||||||
|
echo "========================================="
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Set environment variables for better output
|
||||||
|
export LANG=C
|
||||||
|
export ANSIBLE_HOST_KEY_CHECKING=False
|
||||||
|
|
||||||
|
# Run the playbook
|
||||||
|
cd "$SCRIPT_DIR"
|
||||||
|
ansible-playbook -i "$INVENTORY_FILE" "$PLAYBOOK_FILE" --limit "$group_name" -v
|
||||||
|
|
||||||
|
local exit_code=$?
|
||||||
|
echo ""
|
||||||
|
echo "========================================="
|
||||||
|
if [ $exit_code -eq 0 ]; then
|
||||||
|
echo "✅ Playbook execution completed successfully!"
|
||||||
|
else
|
||||||
|
echo "❌ Playbook execution failed with exit code: $exit_code"
|
||||||
|
fi
|
||||||
|
echo "========================================="
|
||||||
|
|
||||||
|
return $exit_code
|
||||||
|
}
|
||||||
|
|
||||||
|
# Main script logic
|
||||||
|
main() {
|
||||||
|
# Check if argument is provided
|
||||||
|
if [ $# -eq 0 ]; then
|
||||||
|
echo "❌ Error: No group specified"
|
||||||
|
echo ""
|
||||||
|
show_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Parse argument
|
||||||
|
local arg="$1"
|
||||||
|
if [[ "$arg" =~ ^-(.+)$ ]]; then
|
||||||
|
local group_name="${BASH_REMATCH[1]}"
|
||||||
|
else
|
||||||
|
echo "❌ Error: Invalid argument format. Use -<group_name>"
|
||||||
|
echo ""
|
||||||
|
show_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if files exist
|
||||||
|
if [ ! -f "$INVENTORY_FILE" ]; then
|
||||||
|
echo "❌ Error: Inventory file not found: $INVENTORY_FILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -f "$PLAYBOOK_FILE" ]; then
|
||||||
|
echo "❌ Error: Playbook file not found: $PLAYBOOK_FILE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if group exists
|
||||||
|
if ! check_group_exists "$group_name"; then
|
||||||
|
echo "❌ Error: Group '$group_name' not found in inventory"
|
||||||
|
echo ""
|
||||||
|
show_usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Run the playbook
|
||||||
|
run_playbook "$group_name"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Handle help argument
|
||||||
|
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
|
||||||
|
show_usage
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Run main function
|
||||||
|
main "$@"
|
||||||
Loading…
Reference in New Issue