4.1 Ad-hoc 命令概述

4.1.1 什么是 Ad-hoc 命令

Ad-hoc 命令是 Ansible 提供的一种快速执行单个任务的方式,无需编写完整的 Playbook。它们非常适合执行简单的管理任务、快速检查系统状态或进行一次性操作。

特点: - 快速执行单个模块 - 无需编写 Playbook 文件 - 适合临时性和探索性任务 - 支持所有 Ansible 模块 - 可以并行执行

基本语法

ansible <pattern> -m <module> -a "<arguments>" [options]

4.1.2 命令结构解析

# 完整的 Ad-hoc 命令示例
ansible webservers -m service -a "name=nginx state=started" \
  --become --become-user=root -i inventory/hosts -f 10

参数说明: - webservers:目标主机模式 - -m service:使用的模块 - -a "name=nginx state=started":模块参数 - --become:提升权限 - --become-user=root:提升到的用户 - -i inventory/hosts:指定 Inventory 文件 - -f 10:并发数

4.2 基本用法

4.2.1 主机模式(Patterns)

# 所有主机
ansible all -m ping

# 特定主机
ansible web1.example.com -m ping

# 主机组
ansible webservers -m ping

# 多个组
ansible webservers:databases -m ping

# 排除主机
ansible all:!databases -m ping

# 交集
ansible webservers:&production -m ping

# 正则表达式
ansible ~web[0-9]+ -m ping

# 主机范围
ansible web[1:5] -m ping

# 复杂模式
ansible 'webservers:&production:!web3.example.com' -m ping

4.2.2 常用选项

# 基本选项
ansible all -m ping \
  -i inventory/hosts \          # 指定 Inventory
  -u ansible \                  # 远程用户
  --private-key ~/.ssh/id_rsa \ # SSH 私钥
  -f 20 \                       # 并发数
  -v                            # 详细输出

# 权限提升
ansible all -m command -a "whoami" \
  --become \                    # 使用 sudo
  --become-user=root \          # 提升到 root
  --ask-become-pass             # 询问 sudo 密码

# 连接选项
ansible all -m ping \
  --connection=ssh \            # 连接类型
  --ssh-extra-args="-o StrictHostKeyChecking=no" \
  --timeout=30                  # 连接超时

# 输出控制
ansible all -m setup \
  -v \                          # 详细输出
  --one-line \                  # 单行输出
  --tree /tmp/facts             # 保存输出到文件

4.3 系统管理命令

4.3.1 系统信息收集

# 收集系统事实
ansible all -m setup

# 收集特定事实
ansible all -m setup -a "filter=ansible_distribution*"
ansible all -m setup -a "filter=ansible_memory_mb"
ansible all -m setup -a "filter=ansible_mounts"

# 收集网络信息
ansible all -m setup -a "filter=ansible_default_ipv4"
ansible all -m setup -a "filter=ansible_all_ipv4_addresses"

# 收集硬件信息
ansible all -m setup -a "filter=ansible_processor*"
ansible all -m setup -a "filter=ansible_memtotal_mb"

# 自定义事实收集
ansible all -m setup -a "gather_subset=network,hardware"
ansible all -m setup -a "gather_subset=!facter,!ohai"

# 保存事实到文件
ansible all -m setup --tree /tmp/facts/

4.3.2 命令执行

# 执行简单命令
ansible all -m command -a "uptime"
ansible all -m command -a "df -h"
ansible all -m command -a "free -m"

# 使用 shell 模块(支持管道和重定向)
ansible all -m shell -a "ps aux | grep nginx"
ansible all -m shell -a "cat /proc/meminfo | grep MemTotal"
ansible all -m shell -a "ls -la /var/log/*.log"

# 在特定目录执行命令
ansible all -m command -a "ls -la" -a "chdir=/opt"
ansible all -m shell -a "pwd && ls -la" -a "chdir=/tmp"

# 设置环境变量
ansible all -m shell -a "echo $CUSTOM_VAR" \
  -e "ansible_env={'CUSTOM_VAR': 'hello'}"

# 超时控制
ansible all -m command -a "sleep 30" -T 60

# 异步执行
ansible all -m command -a "long-running-command" -B 3600 -P 0

4.3.3 进程和服务管理

# 检查进程
ansible all -m shell -a "ps aux | grep nginx | grep -v grep"
ansible all -m shell -a "pgrep -f nginx"

# 服务管理
ansible webservers -m service -a "name=nginx state=started"
ansible webservers -m service -a "name=nginx state=stopped"
ansible webservers -m service -a "name=nginx state=restarted"
ansible webservers -m service -a "name=nginx state=reloaded"

# 启用/禁用服务
ansible webservers -m service -a "name=nginx enabled=yes"
ansible webservers -m service -a "name=nginx enabled=no"

# systemd 服务管理
ansible all -m systemd -a "name=nginx state=started enabled=yes"
ansible all -m systemd -a "daemon_reload=yes"

# 检查服务状态
ansible all -m command -a "systemctl status nginx"
ansible all -m command -a "systemctl is-active nginx"
ansible all -m command -a "systemctl is-enabled nginx"

4.4 文件和目录操作

4.4.1 文件操作

# 创建文件
ansible all -m file -a "path=/tmp/test.txt state=touch"
ansible all -m file -a "path=/tmp/test.txt state=touch mode=0644 owner=root group=root"

# 创建目录
ansible all -m file -a "path=/opt/myapp state=directory"
ansible all -m file -a "path=/opt/myapp state=directory mode=0755 owner=app group=app"

# 创建多级目录
ansible all -m file -a "path=/opt/myapp/logs/archive state=directory recurse=yes"

# 删除文件或目录
ansible all -m file -a "path=/tmp/test.txt state=absent"
ansible all -m file -a "path=/tmp/testdir state=absent"

# 创建符号链接
ansible all -m file -a "src=/opt/myapp/current dest=/opt/myapp/releases/v1.0 state=link"

# 修改权限和所有者
ansible all -m file -a "path=/opt/myapp mode=0755"
ansible all -m file -a "path=/opt/myapp owner=app group=app recurse=yes"

# 检查文件状态
ansible all -m stat -a "path=/etc/passwd"
ansible all -m stat -a "path=/opt/myapp get_checksum=yes"

4.4.2 文件内容操作

# 复制文件
ansible all -m copy -a "src=/local/file.txt dest=/remote/file.txt"
ansible all -m copy -a "content='Hello World' dest=/tmp/hello.txt"
ansible all -m copy -a "src=/local/file.txt dest=/remote/file.txt backup=yes"

# 获取文件
ansible all -m fetch -a "src=/etc/hostname dest=/tmp/hostnames/"
ansible all -m fetch -a "src=/var/log/messages dest=/tmp/logs/ flat=yes"

# 查看文件内容
ansible all -m command -a "cat /etc/hostname"
ansible all -m shell -a "head -10 /var/log/messages"
ansible all -m shell -a "tail -f /var/log/nginx/access.log" -B 60 -P 0

# 搜索文件内容
ansible all -m shell -a "grep -n 'error' /var/log/messages"
ansible all -m shell -a "find /var/log -name '*.log' -mtime -1"

# 文件编辑
ansible all -m lineinfile -a "path=/etc/hosts line='192.168.1.100 myserver.local'"
ansible all -m lineinfile -a "path=/etc/ssh/sshd_config regexp='^#?PermitRootLogin' line='PermitRootLogin no'"

# 替换文件内容
ansible all -m replace -a "path=/etc/nginx/nginx.conf regexp='worker_processes auto' replace='worker_processes 4'"

4.4.3 文件传输和同步

# 下载文件
ansible all -m get_url -a "url=https://example.com/file.tar.gz dest=/tmp/file.tar.gz"
ansible all -m get_url -a "url=https://example.com/file.tar.gz dest=/tmp/file.tar.gz checksum=sha256:abc123..."

# 解压文件
ansible all -m unarchive -a "src=/tmp/file.tar.gz dest=/opt/ remote_src=yes"
ansible all -m unarchive -a "src=files/app.tar.gz dest=/opt/"

# 同步目录
ansible all -m synchronize -a "src=/local/dir/ dest=/remote/dir/"
ansible all -m synchronize -a "src=/local/dir/ dest=/remote/dir/ delete=yes"

# 压缩文件
ansible all -m archive -a "path=/opt/myapp dest=/tmp/myapp-backup.tar.gz"
ansible all -m archive -a "path=/var/log/*.log dest=/tmp/logs-backup.tar.gz remove=yes"

4.5 包管理

4.5.1 通用包管理

# 使用 package 模块(自动检测包管理器)
ansible all -m package -a "name=vim state=present"
ansible all -m package -a "name=nginx state=latest"
ansible all -m package -a "name=apache2 state=absent"

# 安装多个包
ansible all -m package -a "name=vim,git,curl state=present"

# 检查包状态
ansible all -m shell -a "which nginx"
ansible all -m command -a "nginx -v"

4.5.2 特定包管理器

# APT (Debian/Ubuntu)
ansible ubuntu -m apt -a "update_cache=yes"
ansible ubuntu -m apt -a "name=nginx state=present"
ansible ubuntu -m apt -a "name=nginx=1.18.0-1ubuntu1 state=present"
ansible ubuntu -m apt -a "upgrade=dist"

# YUM (CentOS/RHEL)
ansible centos -m yum -a "name=nginx state=present"
ansible centos -m yum -a "name='@Development tools' state=present"
ansible centos -m yum -a "name=nginx enablerepo=epel"

# DNF (Fedora)
ansible fedora -m dnf -a "name=nginx state=present"
ansible fedora -m dnf -a "name=nginx state=latest"

# PIP (Python 包)
ansible all -m pip -a "name=django state=present"
ansible all -m pip -a "name=requests version=2.25.1"
ansible all -m pip -a "requirements=/tmp/requirements.txt"
ansible all -m pip -a "name=django virtualenv=/opt/myapp/venv"

# NPM (Node.js 包)
ansible all -m npm -a "name=express global=yes"
ansible all -m npm -a "name=lodash path=/opt/myapp"

# GEM (Ruby 包)
ansible all -m gem -a "name=rails state=present"
ansible all -m gem -a "name=bundler user_install=no"

4.6 用户和组管理

4.6.1 用户管理

# 创建用户
ansible all -m user -a "name=john state=present"
ansible all -m user -a "name=john shell=/bin/bash home=/home/john createhome=yes"
ansible all -m user -a "name=john groups=sudo,developers append=yes"

# 设置用户密码
ansible all -m user -a "name=john password={{ 'password123' | password_hash('sha512') }}"

# 修改用户属性
ansible all -m user -a "name=john shell=/bin/zsh"
ansible all -m user -a "name=john groups=admin append=no"

# 删除用户
ansible all -m user -a "name=john state=absent"
ansible all -m user -a "name=john state=absent remove=yes"

# 管理 SSH 密钥
ansible all -m authorized_key -a "user=john key='{{ lookup('file', '~/.ssh/id_rsa.pub') }}'"
ansible all -m authorized_key -a "user=john key='ssh-rsa AAAAB3...' state=absent"

# 检查用户信息
ansible all -m command -a "id john"
ansible all -m shell -a "getent passwd john"

4.6.2 组管理

# 创建组
ansible all -m group -a "name=developers state=present"
ansible all -m group -a "name=developers gid=1001"

# 删除组
ansible all -m group -a "name=oldgroup state=absent"

# 检查组信息
ansible all -m command -a "getent group developers"
ansible all -m shell -a "groups john"

4.7 网络和连接测试

4.7.1 连接测试

# 基本连通性测试
ansible all -m ping

# 测试特定端口
ansible all -m wait_for -a "host=google.com port=80 timeout=10"
ansible all -m wait_for -a "host=localhost port=3306 state=started"
ansible all -m wait_for -a "host=localhost port=8080 state=stopped"

# 测试文件存在
ansible all -m wait_for -a "path=/tmp/ready.txt state=present"

# URI 测试
ansible all -m uri -a "url=http://localhost:8080/health"
ansible all -m uri -a "url=https://api.example.com/status method=GET"
ansible all -m uri -a "url=http://localhost:8080/api method=POST body='{"test": true}' body_format=json"

4.7.2 网络信息收集

# 网络接口信息
ansible all -m setup -a "filter=ansible_interfaces"
ansible all -m setup -a "filter=ansible_default_ipv4"
ansible all -m setup -a "filter=ansible_all_ipv4_addresses"

# 网络连接状态
ansible all -m shell -a "netstat -tuln"
ansible all -m shell -a "ss -tuln"
ansible all -m shell -a "lsof -i :80"

# DNS 解析测试
ansible all -m shell -a "nslookup google.com"
ansible all -m shell -a "dig google.com"

# 路由信息
ansible all -m shell -a "ip route show"
ansible all -m shell -a "route -n"

4.8 系统监控和诊断

4.8.1 系统资源监控

# CPU 信息
ansible all -m shell -a "top -bn1 | head -20"
ansible all -m shell -a "cat /proc/loadavg"
ansible all -m shell -a "vmstat 1 5"

# 内存使用
ansible all -m shell -a "free -h"
ansible all -m shell -a "cat /proc/meminfo | head -10"

# 磁盘使用
ansible all -m shell -a "df -h"
ansible all -m shell -a "du -sh /var/log/*"
ansible all -m shell -a "lsblk"

# 网络流量
ansible all -m shell -a "iftop -t -s 10" -B 15 -P 0
ansible all -m shell -a "cat /proc/net/dev"

# 系统负载
ansible all -m shell -a "uptime"
ansible all -m shell -a "w"

4.8.2 日志分析

# 系统日志
ansible all -m shell -a "tail -50 /var/log/syslog"
ansible all -m shell -a "journalctl -n 50 --no-pager"
ansible all -m shell -a "dmesg | tail -20"

# 应用日志
ansible webservers -m shell -a "tail -100 /var/log/nginx/access.log"
ansible webservers -m shell -a "grep ERROR /var/log/nginx/error.log"

# 日志统计
ansible all -m shell -a "grep -c 'ERROR' /var/log/syslog"
ansible all -m shell -a "awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10"

# 实时日志监控
ansible all -m shell -a "tail -f /var/log/syslog" -B 60 -P 0

4.9 高级用法

4.9.1 并行执行和异步操作

# 设置并发数
ansible all -m ping -f 50

# 异步执行长时间任务
ansible all -m shell -a "sleep 300" -B 600 -P 0

# 检查异步任务状态
ansible all -m async_status -a "jid=123456789.12345"

# 批量异步执行
ansible all -m shell -a "backup-script.sh" -B 3600 -P 60

4.9.2 条件执行

# 基于事实的条件执行
ansible all -m package -a "name=nginx state=present" \
  --limit "{{ groups['webservers'] | select('match', '.*ubuntu.*') | list }}"

# 使用变量
ansible all -m service -a "name={{ item }} state=started" \
  -e "item=nginx" --limit webservers

# 基于主机组
ansible 'webservers:&production' -m service -a "name=nginx state=restarted"

4.9.3 输出处理

# 格式化输出
ansible all -m setup -a "filter=ansible_distribution*" --one-line

# 保存输出
ansible all -m setup --tree /tmp/facts/

# JSON 输出
ansible all -m setup -a "filter=ansible_distribution" | jq '.'

# 提取特定信息
ansible all -m setup -a "filter=ansible_default_ipv4" | \
  grep -o '"address": "[^"]*"' | cut -d'"' -f4

4.9.4 错误处理

# 忽略错误
ansible all -m shell -a "exit 1" || echo "Some hosts failed"

# 详细错误信息
ansible all -m command -a "invalid-command" -vvv

# 超时控制
ansible all -m shell -a "sleep 60" -T 30

# 重试机制(通过脚本实现)
for i in {1..3}; do
  ansible all -m ping && break
  echo "Retry $i failed, waiting..."
  sleep 5
done

4.10 实用脚本和技巧

4.10.1 常用管理脚本

#!/bin/bash
# ansible-tools.sh - Ansible 管理工具脚本

# 检查所有主机连通性
check_connectivity() {
    echo "检查主机连通性..."
    ansible all -m ping --one-line
}

# 收集系统信息
collect_system_info() {
    echo "收集系统信息..."
    ansible all -m setup -a "filter=ansible_distribution*,ansible_memory_mb,ansible_processor_count" \
        --tree /tmp/system-info/
}

# 检查服务状态
check_services() {
    local service=$1
    echo "检查 $service 服务状态..."
    ansible all -m shell -a "systemctl is-active $service" --one-line
}

# 更新所有系统
update_systems() {
    echo "更新 Ubuntu 系统..."
    ansible ubuntu -m apt -a "update_cache=yes upgrade=dist" --become
    
    echo "更新 CentOS 系统..."
    ansible centos -m yum -a "name=* state=latest" --become
}

# 清理日志文件
cleanup_logs() {
    echo "清理旧日志文件..."
    ansible all -m shell -a "find /var/log -name '*.log' -mtime +30 -delete" --become
    ansible all -m shell -a "journalctl --vacuum-time=30d" --become
}

# 备份配置文件
backup_configs() {
    local backup_dir="/tmp/config-backup-$(date +%Y%m%d)"
    echo "备份配置文件到 $backup_dir..."
    
    ansible all -m file -a "path=$backup_dir state=directory" --become
    ansible webservers -m copy -a "src=/etc/nginx/nginx.conf dest=$backup_dir/nginx.conf remote_src=yes" --become
    ansible databases -m copy -a "src=/etc/mysql/my.cnf dest=$backup_dir/my.cnf remote_src=yes" --become
}

# 主菜单
case "$1" in
    "connectivity")
        check_connectivity
        ;;
    "info")
        collect_system_info
        ;;
    "service")
        check_services "$2"
        ;;
    "update")
        update_systems
        ;;
    "cleanup")
        cleanup_logs
        ;;
    "backup")
        backup_configs
        ;;
    *)
        echo "用法: $0 {connectivity|info|service <name>|update|cleanup|backup}"
        exit 1
        ;;
esac

4.10.2 监控脚本

#!/bin/bash
# monitor.sh - 系统监控脚本

# 检查磁盘使用率
check_disk_usage() {
    echo "=== 磁盘使用率检查 ==="
    ansible all -m shell -a "df -h | awk 'NR>1 {if(\$5+0 > 80) print \$0}'"
}

# 检查内存使用率
check_memory_usage() {
    echo "=== 内存使用率检查 ==="
    ansible all -m shell -a "free | awk 'NR==2{printf \"Memory Usage: %s/%sMB (%.2f%%)\\n\", \$3,\$2,\$3*100/\$2}'"
}

# 检查 CPU 负载
check_cpu_load() {
    echo "=== CPU 负载检查 ==="
    ansible all -m shell -a "uptime | awk '{print \$NF}' | cut -d',' -f1"
}

# 检查关键服务
check_critical_services() {
    echo "=== 关键服务检查 ==="
    local services=("nginx" "mysql" "redis" "ssh")
    
    for service in "${services[@]}"; do
        echo "检查 $service 服务..."
        ansible all -m shell -a "systemctl is-active $service 2>/dev/null || echo 'not-found'" --one-line
    done
}

# 检查网络连接
check_network() {
    echo "=== 网络连接检查 ==="
    ansible all -m shell -a "ping -c 1 8.8.8.8 > /dev/null && echo 'OK' || echo 'FAILED'"
}

# 生成报告
generate_report() {
    local report_file="/tmp/system-report-$(date +%Y%m%d-%H%M%S).txt"
    
    {
        echo "系统监控报告 - $(date)"
        echo "=============================="
        check_disk_usage
        echo
        check_memory_usage
        echo
        check_cpu_load
        echo
        check_critical_services
        echo
        check_network
    } | tee "$report_file"
    
    echo "报告已保存到: $report_file"
}

# 执行监控
generate_report

4.10.3 部署脚本

#!/bin/bash
# deploy.sh - 应用部署脚本

APP_NAME="myapp"
APP_VERSION="$1"
DEPLOY_USER="deploy"
DEPLOY_PATH="/opt/$APP_NAME"

if [ -z "$APP_VERSION" ]; then
    echo "用法: $0 <version>"
    exit 1
fi

# 预检查
echo "=== 预检查 ==="
ansible webservers -m ping || exit 1

# 下载应用
echo "=== 下载应用 $APP_VERSION ==="
ansible webservers -m get_url -a \
    "url=https://releases.example.com/$APP_NAME-$APP_VERSION.tar.gz \
     dest=/tmp/$APP_NAME-$APP_VERSION.tar.gz" \
    --become-user="$DEPLOY_USER"

# 备份当前版本
echo "=== 备份当前版本 ==="
ansible webservers -m shell -a \
    "if [ -d $DEPLOY_PATH/current ]; then \
       cp -r $DEPLOY_PATH/current $DEPLOY_PATH/backup-$(date +%Y%m%d-%H%M%S); \
     fi" \
    --become-user="$DEPLOY_USER"

# 解压新版本
echo "=== 解压新版本 ==="
ansible webservers -m unarchive -a \
    "src=/tmp/$APP_NAME-$APP_VERSION.tar.gz \
     dest=$DEPLOY_PATH/releases/ \
     remote_src=yes \
     creates=$DEPLOY_PATH/releases/$APP_NAME-$APP_VERSION" \
    --become-user="$DEPLOY_USER"

# 更新符号链接
echo "=== 更新符号链接 ==="
ansible webservers -m file -a \
    "src=$DEPLOY_PATH/releases/$APP_NAME-$APP_VERSION \
     dest=$DEPLOY_PATH/current \
     state=link \
     force=yes" \
    --become-user="$DEPLOY_USER"

# 重启服务
echo "=== 重启服务 ==="
ansible webservers -m service -a "name=$APP_NAME state=restarted" --become

# 健康检查
echo "=== 健康检查 ==="
sleep 10
ansible webservers -m uri -a "url=http://localhost:8080/health status_code=200"

if [ $? -eq 0 ]; then
    echo "部署成功!"
    # 清理旧版本
    ansible webservers -m shell -a \
        "ls -t $DEPLOY_PATH/releases/ | tail -n +6 | xargs -I {} rm -rf $DEPLOY_PATH/releases/{}" \
        --become-user="$DEPLOY_USER"
else
    echo "部署失败,回滚中..."
    # 回滚逻辑
    ansible webservers -m shell -a \
        "if [ -d $DEPLOY_PATH/backup-* ]; then \
           latest_backup=\$(ls -t $DEPLOY_PATH/backup-* | head -1); \
           rm -f $DEPLOY_PATH/current; \
           ln -s \$latest_backup $DEPLOY_PATH/current; \
         fi" \
        --become-user="$DEPLOY_USER"
    
    ansible webservers -m service -a "name=$APP_NAME state=restarted" --become
    exit 1
fi

4.11 性能优化

4.11.1 并发优化

# 调整并发数
ansible all -m ping -f 100  # 增加到 100 个并发

# 使用 SSH 连接复用
export ANSIBLE_SSH_ARGS="-C -o ControlMaster=auto -o ControlPersist=60s"
ansible all -m ping

# 禁用事实收集(如果不需要)
ansible all -m command -a "uptime" --gather-facts=no

# 使用 pipelining
export ANSIBLE_PIPELINING=True
ansible all -m ping

4.11.2 输出优化

# 简化输出
ansible all -m ping --one-line

# 只显示失败的主机
ansible all -m ping 2>/dev/null | grep -v SUCCESS

# 静默模式
ansible all -m ping -q

# 自定义输出格式
ansible all -m setup -a "filter=ansible_hostname" | \
    grep -A1 '"ansible_hostname"' | grep -v '"ansible_hostname"' | \
    sed 's/.*"\(.*\)".*/\1/'

4.12 本章总结

本章详细介绍了 Ansible Ad-hoc 命令的使用,主要内容包括:

  • 基本概念:Ad-hoc 命令的特点和语法结构
  • 主机模式:灵活的主机选择和过滤方式
  • 系统管理:系统信息收集、命令执行、服务管理
  • 文件操作:文件和目录的创建、修改、传输
  • 包管理:各种包管理器的使用
  • 用户管理:用户和组的创建和管理
  • 网络测试:连接测试和网络信息收集
  • 系统监控:资源监控和日志分析
  • 高级用法:并行执行、异步操作、条件执行
  • 实用脚本:管理、监控、部署脚本示例
  • 性能优化:提高执行效率的技巧

Ad-hoc 命令是 Ansible 的重要组成部分,掌握它们可以大大提高日常运维工作的效率。

4.13 练习题

基础练习

  1. 连通性测试

    • 测试所有主机的连通性
    • 测试特定组的主机连通性
    • 使用不同的主机模式进行测试
  2. 系统信息收集

    • 收集所有主机的操作系统信息
    • 收集内存和 CPU 信息
    • 将信息保存到本地文件
  3. 服务管理

    • 检查 nginx 服务状态
    • 启动/停止/重启服务
    • 启用/禁用服务自启动

进阶练习

  1. 文件操作

    • 批量创建目录结构
    • 复制配置文件到多台主机
    • 从远程主机获取日志文件
  2. 包管理

    • 在不同操作系统上安装相同软件
    • 更新所有主机的软件包
    • 安装 Python 包到虚拟环境
  3. 用户管理

    • 批量创建用户账户
    • 配置 SSH 密钥认证
    • 管理用户组权限

实战练习

  1. 监控脚本

    • 编写系统资源监控脚本
    • 实现服务状态检查
    • 生成监控报告
  2. 部署脚本

    • 编写应用部署脚本
    • 实现回滚机制
    • 添加健康检查
  3. 故障排除

    • 诊断网络连接问题
    • 分析系统性能问题
    • 处理服务异常

下一章第5章:Playbook 编写基础

返回目录Ansible 自动化运维教程