自动配置Ansible Tower的脚本

wen 实用脚本 2

本文目录导读:

自动配置Ansible Tower的脚本

  1. 方案1:Ansible自动化安装Tower脚本
  2. 方案2:Shell脚本自动化部署
  3. 使用说明
  4. 注意事项

我来为您提供一个自动配置Ansible Tower的脚本方案,这里提供两种方式:基于Ansible的自动化安装和基于Shell脚本的自动化部署。

方案1:Ansible自动化安装Tower脚本

# install_ansible_tower.yml
---
- name: 自动化安装Ansible Tower
  hosts: tower_server
  become: yes
  vars:
    tower_version: "3.8.6"
    tower_package: "ansible-tower-setup-bundle-{{ tower_version }}.tar.gz"
    tower_admin_password: "Admin123!"
    pg_password: "Pg123456!"
    rabbitmq_password: "Rabbit123!"
  tasks:
    - name: 安装必要依赖包
      yum:
        name:
          - wget
          - git
          - curl
          - python3
          - python3-pip
        state: present
    - name: 设置主机名
      hostname:
        name: "{{ inventory_hostname }}"
    - name: 添加主机名到hosts文件
      lineinfile:
        path: /etc/hosts
        line: "127.0.1.1 {{ inventory_hostname }}"
        create: yes
    - name: 创建tower用户
      user:
        name: tower
        groups: wheel
        shell: /bin/bash
    - name: 关闭防火墙和SELinux
      systemd:
        name: firewalld
        state: stopped
        enabled: no
      selinux:
        state: disabled
    - name: 下载Ansible Tower安装包
      get_url:
        url: "https://releases.ansible.com/ansible-tower/setup-bundle/{{ tower_package }}"
        dest: "/tmp/{{ tower_package }}"
        timeout: 300
    - name: 解压安装包
      unarchive:
        src: "/tmp/{{ tower_package }}"
        dest: /tmp/
        remote_src: yes
    - name: 配置安装变量
      template:
        src: inventory.j2
        dest: "/tmp/ansible-tower-setup-bundle-{{ tower_version }}/inventory"
    - name: 执行安装脚本
      shell: |
        cd /tmp/ansible-tower-setup-bundle-{{ tower_version }}
        ./setup.sh
      async: 3600
      poll: 30
    - name: 验证Tower服务状态
      shell: |
        systemctl status ansible-tower-service | grep Active
      register: tower_status
    - name: 显示Tower状态
      debug:
        msg: "{{ tower_status.stdout }}"

方案2:Shell脚本自动化部署

#!/bin/bash
# auto_install_tower.sh
# 自动化安装Ansible Tower脚本
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
# 配置变量
TOWER_VERSION="3.8.6"
TOWER_ADMIN_PASSWORD="Admin123!"
PG_PASSWORD="Pg123456!"
RABBITMQ_PASSWORD="Rabbit123!"
DOWNLOAD_URL="https://releases.ansible.com/ansible-tower/setup-bundle/ansible-tower-setup-bundle-${TOWER_VERSION}.tar.gz"
# 日志函数
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}
# 检查root权限
check_root() {
    if [[ $EUID -ne 0 ]]; then
        log_error "此脚本需要root权限运行"
        exit 1
    fi
}
# 检查系统要求
check_system() {
    log_info "检查系统要求..."
    # 检查操作系统
    if [[ -f /etc/redhat-release ]]; then
        OS_TYPE="centos"
        log_info "操作系统: CentOS/RHEL"
    elif [[ -f /etc/lsb-release ]]; then
        OS_TYPE="ubuntu"
        log_info "操作系统: Ubuntu"
    else
        log_error "不支持的操作系统"
        exit 1
    fi
    # 检查内存
    MEM_TOTAL=$(free -m | awk '/^Mem:/{print $2}')
    if [[ $MEM_TOTAL -lt 4096 ]]; then
        log_warn "内存小于4GB,建议增加内存"
    fi
    # 检查磁盘空间
    DISK_SPACE=$(df -h / | awk 'NR==2{print $4}' | sed 's/G//')
    if [[ $DISK_SPACE -lt 20 ]]; then
        log_error "磁盘空间不足20GB"
        exit 1
    fi
}
# 安装依赖
install_dependencies() {
    log_info "安装系统依赖..."
    if [[ "$OS_TYPE" == "centos" ]]; then
        yum update -y
        yum install -y wget git curl python3 python3-pip net-tools
    elif [[ "$OS_TYPE" == "ubuntu" ]]; then
        apt-get update
        apt-get install -y wget git curl python3 python3-pip net-tools
    fi
    # 安装Ansible
    pip3 install ansible
}
# 系统准备
prepare_system() {
    log_info "准备系统环境..."
    # 设置主机名
    hostnamectl set-hostname tower-server
    # 关闭防火墙
    if [[ "$OS_TYPE" == "centos" ]]; then
        systemctl stop firewalld
        systemctl disable firewalld
    elif [[ "$OS_TYPE" == "ubuntu" ]]; then
        ufw disable
    fi
    # 禁用SELinux
    if [[ -f /etc/selinux/config ]]; then
        sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
        setenforce 0
    fi
    # 优化系统参数
    cat >> /etc/sysctl.conf << EOF
fs.file-max = 65535
net.core.somaxconn = 65535
EOF
    sysctl -p
    # 设置文件描述符限制
    cat >> /etc/security/limits.conf << EOF
* soft nofile 65535
* hard nofile 65535
EOF
}
# 下载安装包
download_tower() {
    log_info "下载Ansible Tower安装包..."
    cd /tmp
    if [[ ! -f "ansible-tower-setup-bundle-${TOWER_VERSION}.tar.gz" ]]; then
        wget -c "$DOWNLOAD_URL" --timeout=300
    fi
    tar -xzf "ansible-tower-setup-bundle-${TOWER_VERSION}.tar.gz"
}
# 配置安装参数
configure_inventory() {
    log_info "配置安装参数..."
    cd "/tmp/ansible-tower-setup-bundle-${TOWER_VERSION}"
    cat > inventory << EOF
[tower]
localhost ansible_connection=local
[database]
localhost ansible_connection=local
[all:vars]
admin_password='${TOWER_ADMIN_PASSWORD}'
pg_password='${PG_PASSWORD}'
rabbitmq_password='${RABBITMQ_PASSWORD}'
pg_database='awx'
pg_username='awx'
EOF
}
# 执行安装
install_tower() {
    log_info "开始安装Ansible Tower..."
    cd "/tmp/ansible-tower-setup-bundle-${TOWER_VERSION}"
    # 执行安装脚本
    if [[ -f setup.sh ]]; then
        ./setup.sh
        INSTALL_STATUS=$?
        if [[ $INSTALL_STATUS -eq 0 ]]; then
            log_info "Ansible Tower安装成功"
        else
            log_error "Ansible Tower安装失败"
            exit 1
        fi
    else
        log_error "安装脚本不存在"
        exit 1
    fi
}
# 验证安装
verify_installation() {
    log_info "验证安装结果..."
    # 检查服务状态
    if systemctl is-active --quiet ansible-tower-service; then
        log_info "Ansible Tower服务运行正常"
    else
        log_error "Ansible Tower服务未运行"
        exit 1
    fi
    # 检查Web界面
    TOWER_IP=$(hostname -I | awk '{print $1}')
    if curl -s -o /dev/null -w "%{http_code}" "https://${TOWER_IP}/" | grep -q "200\|302"; then
        log_info "Tower Web界面可访问"
        log_info "访问地址: https://${TOWER_IP}/"
        log_info "管理员密码: ${TOWER_ADMIN_PASSWORD}"
    else
        log_warn "Web界面可能无法访问,请检查防火墙设置"
    fi
}
# 创建备份脚本
create_backup_script() {
    log_info "创建备份脚本..."
    cat > /opt/backup_tower.sh << 'EOF'
#!/bin/bash
# Ansible Tower备份脚本
BACKUP_DIR="/backup/ansible-tower"
DATE=$(date +%Y%m%d_%H%M%S)
tar -czf "${BACKUP_DIR}/tower_backup_${DATE}.tar.gz" /var/lib/awx /etc/tower
EOF
    chmod +x /opt/backup_tower.sh
}
# 清理安装文件
cleanup() {
    log_info "清理临时文件..."
    rm -rf "/tmp/ansible-tower-setup-bundle-${TOWER_VERSION}"
    rm -f "/tmp/ansible-tower-setup-bundle-${TOWER_VERSION}.tar.gz"
}
# 主函数
main() {
    echo "========================================="
    echo "  Ansible Tower 自动化安装脚本"
    echo "========================================="
    check_root
    check_system
    install_dependencies
    prepare_system
    download_tower
    configure_inventory
    install_tower
    verify_installation
    create_backup_script
    cleanup
    echo ""
    echo "========================================="
    echo "  安装完成!"
    echo "========================================="
    echo ""
    echo "访问地址: https://$(hostname -I | awk '{print $1}')/"
    echo "管理员密码: ${TOWER_ADMIN_PASSWORD}"
    echo ""
    echo "备份脚本位置: /opt/backup_tower.sh"
    echo "========================================="
}
# 执行主函数
main

使用说明

使用Ansible脚本:

# 创建inventory文件
cat > inventory << EOF
[tower_server]
192.168.1.100 ansible_user=root
[all:vars]
ansible_python_interpreter=/usr/bin/python3
EOF
# 执行playbook
ansible-playbook -i inventory install_ansible_tower.yml

使用Shell脚本:

# 赋予执行权限
chmod +x auto_install_tower.sh
# 运行脚本
./auto_install_tower.sh

注意事项

  1. 系统要求

    • CentOS 7+ 或 Ubuntu 18.04+
    • 至少4GB内存
    • 至少20GB磁盘空间
    • 干净的安装环境
  2. 密码设置

    • 修改脚本中的默认密码
    • 建议使用强密码
  3. 许可证

    • 如需商业许可证,请从Red Hat购买
    • 可使用AWX作为开源替代
  4. 安全建议

    • 安装后修改默认密码
    • 配置防火墙规则
    • 启用SSL证书

此脚本会自动完成Tower的部署,根据实际环境调整配置参数。

抱歉,评论功能暂时关闭!