Ansible自动化运维实战:配置管理与批量操作指南

文章最后更新时间:2026-04-14 12:54:21

服务器多了手动管理不过来?Ansible是自动化运维的利器,配置简单、无需代理、适合批量操作。本文从入门到实战讲解Ansible,帮你实现运维自动化。

一、Ansible核心概念

为什么用Ansible?

  • 无代理:不需要在服务器安装客户端
  • 配置简单:使用YAML编写Playbook
  • 幂等性:重复执行结果一致
  • 批量操作:一次操作多台服务器

核心组件

  • Inventory:主机清单,定义管理哪些服务器
  • Playbook:任务剧本,定义执行什么任务
  • Module:模块,执行具体的操作
  • Role:角色,任务模块化封装

二、环境准备

安装Ansible

# Ubuntu/Debian
sudo apt-get update
sudo apt-get install ansible

# CentOS/RHEL
sudo yum install ansible

# pip安装
pip install ansible

配置SSH免密登录

# 生成SSH密钥
ssh-keygen -t rsa

# 复制到目标服务器
ssh-copy-id user@server_ip

创建主机清单

# /etc/ansible/hosts
[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com
db2.example.com

[all:vars]
ansible_user=root
ansible_python_interpreter=/usr/bin/python3

三、常用模块

command模块:执行命令

- name: 查看服务器 uptime
  command: uptime

- name: 重启服务
  command: systemctl restart nginx

shell模块:执行Shell命令

- name: 执行脚本
  shell: /path/to/script.sh

copy模块:复制文件

- name: 复制配置文件
  copy:
    src: /local/nginx.conf
    dest: /etc/nginx/nginx.conf
    owner: root
    group: root
    mode: '0644'

template模块:复制模板文件

- name: 复制配置模板
  template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf

file模块:文件操作

- name: 创建目录
  file:
    path: /path/to/dir
    state: directory
    mode: '0755'

- name: 创建软链接
  file:
    src: /path/to/file
    dest: /path/to/link
    state: link

yum/apt模块:软件安装

- name: 安装nginx
  yum:
    name: nginx
    state: present

- name: 安装多个软件
  apt:
    name:
      - nginx
      - mysql-server
      - python3
    state: present

service模块:服务管理

- name: 启动nginx
  service:
    name: nginx
    state: started
    enabled: yes

四、Playbook实战

部署Nginx

---
- hosts: webservers
  become: yes
  tasks:
    - name: 安装nginx
      apt:
        name: nginx
        state: present
        update_cache: yes

    - name: 复制配置文件
      copy:
        src: nginx.conf
        dest: /etc/nginx/nginx.conf
      notify: 重启nginx

    - name: 启动nginx
      service:
        name: nginx
        state: started
        enabled: yes

  handlers:
    - name: 重启nginx
      service:
        name: nginx
        state: restarted

批量部署Docker

---
- hosts: all
  become: yes
  tasks:
    - name: 安装依赖
      apt:
        name:
          - apt-transport-https
          - ca-certificates
          - curl
          - gnupg
        state: present
        update_cache: yes

    - name: 添加Docker GPG key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: 添加Docker仓库
      apt_repository:
        repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
        state: present

    - name: 安装Docker
      apt:
        name:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-compose-plugin
        state: present

    - name: 启动Docker
      service:
        name: docker
        state: started
        enabled: yes

五、Role使用

Role目录结构

roles/
  nginx/
    tasks/
      main.yml
    handlers/
      main.yml
    templates/
      nginx.conf.j2
    vars/
      main.yml
    defaults/
      main.yml

使用Role的Playbook

---
- hosts: webservers
  become: yes
  roles:
    - nginx

六、最佳实践

项目结构

ansible_project/
├── inventory/
│   └── prod
├── playbooks/
│   ├── deploy.yml
│   └── configure.yml
├── roles/
│   ├── nginx/
│   ├── docker/
│   └── mysql/
└── ansible.cfg

安全建议

  • 敏感信息使用Vault加密
  • 不同环境使用不同Inventory
  • 正式环境操作前先测试
  • 记录操作日志,便于审计

性能优化

  • 开启SSH长连接:ansible_ssh_common_args=’-o ControlMaster=auto’
  • 批量执行数量:forks=50
  • 异步执行:async和poll参数

七、常见问题

Q:Ansible执行慢怎么办?
A:开启SSH长连接、增加forks数量、使用异步执行。

Q:怎么管理敏感信息?
A:使用Ansible Vault加密敏感文件。

Q:可以同时管理Windows服务器吗?
A:可以,需要配置WinRM连接。

Q:如何回滚操作?
A:使用ansible-playbook的–start-at-task参数,或使用version control回滚。

总结

Ansible是运维自动化利器。核心概念:Inventory主机清单、Playbook任务剧本、Module执行模块、Role角色封装。常用模块:command、copy、template、file、yum、service。掌握这些,批量服务器管理变得简单高效。

瀚煜云提供Ansible自动化运维培训及方案实施。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容