From 9a453b984f5683c3fb46d25f823e1bdb604653e4 Mon Sep 17 00:00:00 2001 From: Houzhong Xu Date: Thu, 18 Sep 2025 07:22:52 +0000 Subject: [PATCH] =?UTF-8?q?chore(ansible):=20=E9=87=8D=E6=9E=84inventory.i?= =?UTF-8?q?ni=E5=88=86=E7=BB=84=E5=92=8C=E8=8A=82=E7=82=B9=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 按地理和服务划分为多个组,如oci_kr, oci_us, huawei, google等 - 添加了多台主机的详细连接和权限配置 - 修正并移除旧的oci组,替换为更细化的组划分 - 规范了lxc、vm、hcp及feiniu等组的节点配置 - 新增armbian组及对应节点onecloud1配置 - 保留通用变量ansible_ssh_common_args不变 - 删除try.md文件中的无关内容记录 --- ansible/cloud-providers-update.yml | 72 +++++++++++++++++ ansible/inventory.ini | 53 +++++++++++-- ansible/run.sh | 123 +++++++++++++++++++++++++++++ try.md | 5 -- 4 files changed, 243 insertions(+), 10 deletions(-) create mode 100644 ansible/cloud-providers-update.yml create mode 100755 ansible/run.sh delete mode 100644 try.md diff --git a/ansible/cloud-providers-update.yml b/ansible/cloud-providers-update.yml new file mode 100644 index 0000000..1892247 --- /dev/null +++ b/ansible/cloud-providers-update.yml @@ -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 \ No newline at end of file diff --git a/ansible/inventory.ini b/ansible/inventory.ini index f80e4c5..5114413 100644 --- a/ansible/inventory.ini +++ b/ansible/inventory.ini @@ -2,13 +2,56 @@ 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 -[oci] -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 +[oci_kr] +ch2 ansible_host=ch2 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] -localhost ansible_connection=local +[ditigalocean] +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] ansible_ssh_common_args='-o StrictHostKeyChecking=no' \ No newline at end of file diff --git a/ansible/run.sh b/ansible/run.sh new file mode 100755 index 0000000..dc12746 --- /dev/null +++ b/ansible/run.sh @@ -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 -" + 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 -" + 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 "$@" \ No newline at end of file diff --git a/try.md b/try.md deleted file mode 100644 index dd35d70..0000000 --- a/try.md +++ /dev/null @@ -1,5 +0,0 @@ -8d7d70f324796be650b79415303c31f567bf459b -https://gitea.tailnet-68f9.ts.net/ -ben -git remote add origin git@gitea.tailnet-68f9.ts.net:ben/mgmt.git -git push -u origin main