跳转至

第一章:Ansible 简介

什么是 Ansible?

Ansible 是一个开源的 IT 自动化平台,由 Michael DeHaan 于 2012 年创建,后被 Red Hat 收购。它使用简单的 YAML 语法来描述自动化任务,无需在目标主机上安装代理程序。

核心特点

  1. 无代理(Agentless) - 通过 SSH 连接目标主机,无需安装客户端
  2. 幂等性(Idempotent) - 多次执行结果相同,不会重复操作
  3. 简单易学 - 使用 YAML 语法,学习曲线平缓
  4. 模块化 - 丰富的内置模块,支持自定义扩展
  5. 跨平台 - 支持 Linux、Windows、网络设备等

Ansible 架构

控制节点与被管理节点

┌─────────────────────────────────────────────────────────────┐
│                    控制节点(Control Node)                   │
│                                                              │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐         │
│  │  Ansible    │  │  Inventory  │  │  Modules    │         │
│  │   Core      │  │   主机清单   │  │   模块库    │         │
│  └─────────────┘  └─────────────┘  └─────────────┘         │
│                                                              │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐         │
│  │  Playbooks  │  │   Roles     │  │   Plugins   │         │
│  │   剧本      │  │   角色      │  │   插件      │         │
│  └─────────────┘  └─────────────┘  └─────────────┘         │
└──────────────────────────┬──────────────────────────────────┘
                           │ SSH
        ┌──────────────────┼──────────────────┐
        │                  │                  │
        ▼                  ▼                  ▼
┌───────────────┐  ┌───────────────┐  ┌───────────────┐
│  被管理节点1   │  │  被管理节点2   │  │  被管理节点3   │
│  (Web Server) │  │  (DB Server)  │  │  (App Server) │
└───────────────┘  └───────────────┘  └───────────────┘

核心组件

1. Inventory(主机清单)

定义 Ansible 管理的主机和组:

# inventory.ini
[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com

[all:children]
webservers
dbservers

2. Modules(模块)

执行具体任务的代码单元:

- name: Install nginx
  ansible.builtin.apt:
    name: nginx
    state: present

3. Playbook(剧本)

定义自动化任务的 YAML 文件:

- name: Deploy Web Application
  hosts: webservers
  become: yes
  tasks:
    - name: Install nginx
      ansible.builtin.apt:
        name: nginx
        state: present

    - name: Start nginx
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: yes

4. Roles(角色)

组织 Playbook 的标准化目录结构:

roles/
└── nginx/
    ├── tasks/
    │   └── main.yml
    ├── templates/
    │   └── nginx.conf.j2
    ├── files/
    ├── handlers/
    │   └── main.yml
    ├── defaults/
    │   └── main.yml
    └── meta/
        └── main.yml

Ansible 工作原理

执行流程

1. 读取配置文件
2. 加载 Inventory
3. 解析 Playbook
4. 生成任务列表
5. 通过 SSH 连接目标主机
6. 传输模块代码
7. 执行模块
8. 返回执行结果
9. 清理临时文件

推送模式

Ansible 采用推送模式,控制节点主动连接被管理节点执行任务:

# 控制节点执行
ansible-playbook site.yml

# Ansible 通过 SSH 推送任务到目标主机

Ansible vs 其他工具

特性 Ansible SaltStack Puppet Chef
架构 无代理 主从架构 主从架构 主从架构
语言 YAML YAML Ruby DSL Ruby
学习曲线 简单 中等 较难 较难
协议 SSH ZeroMQ HTTPS HTTPS
幂等性 支持 支持 支持 支持
Windows 支持 较好 较好 较好 较好

Ansible 应用场景

1. 配置管理

- name: Configure servers
  hosts: all
  tasks:
    - name: Set timezone
      community.general.timezone:
        name: Asia/Shanghai

    - name: Configure sysctl
      ansible.posix.sysctl:
        name: net.core.somaxconn
        value: "65535"
        state: present

2. 应用部署

- name: Deploy application
  hosts: appservers
  tasks:
    - name: Pull latest code
      ansible.builtin.git:
        repo: https://github.com/user/app.git
        dest: /opt/app
        version: main

    - name: Install dependencies
      ansible.builtin.pip:
        requirements: /opt/app/requirements.txt

    - name: Restart service
      ansible.builtin.systemd:
        name: app
        state: restarted

3. 批量任务

- name: Update all servers
  hosts: all
  tasks:
    - name: Update apt cache
      ansible.builtin.apt:
        update_cache: yes

    - name: Upgrade packages
      ansible.builtin.apt:
        upgrade: dist

4. 云资源管理

- name: Create EC2 instance
  hosts: localhost
  tasks:
    - name: Launch instance
      amazon.aws.ec2_instance:
        name: "web-server"
        key_name: "my-key"
        instance_type: "t3.micro"
        image_id: "ami-12345678"
        region: "us-east-1"

Ansible 生态系统

Ansible Core

核心组件,包含:

  • ansible-playbook
  • ansible
  • ansible-vault
  • ansible-galaxy
  • ansible-doc

Ansible Galaxy

社区共享的 Roles 平台:

# 搜索角色
ansible-galaxy search nginx

# 安装角色
ansible-galaxy install geerlingguy.nginx

# 从 requirements.yml 安装
ansible-galaxy install -r requirements.yml

AWX / Ansible Tower

企业级 Ansible 管理平台:

  • Web UI 界面
  • RBAC 权限控制
  • 任务调度
  • 审计日志
  • API 接口

安装要求

控制节点

  • Python 3.8+
  • Linux/macOS(Windows 需 WSL)
  • SSH 客户端

被管理节点

  • Python 2.7+ 或 Python 3.5+
  • SSH 服务运行
  • 无需安装 Ansible

快速开始

# 安装 Ansible
pip install ansible

# 创建主机清单
echo "localhost ansible_connection=local" > inventory

# 测试连接
ansible all -i inventory -m ping

# 运行 Playbook
ansible-playbook -i inventory playbook.yml

小结

本章学习了:

  • ✅ Ansible 概念和特点
  • ✅ Ansible 架构和组件
  • ✅ Ansible 工作原理
  • ✅ Ansible 应用场景
  • ✅ Ansible 生态系统

下一章

第二章:Ansible 安装与配置 - 学习如何安装和配置 Ansible。