ansible

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Ansible

Ansible

Ansible is an agentless automation tool. It handles Configuration Management (installing Nginx, editing configs). 2025 sees the Automation Platform 2.5 unified UI and AI Lightspeed assistance.
Ansible是一款无代理自动化工具,可用于配置管理(如安装Nginx、编辑配置文件)。2025年推出的Automation Platform 2.5具备统一UI和AI Lightspeed辅助功能。

When to Use

适用场景

  • Configuration Mgmt: Terraform creates the VM, Ansible installs the software inside it.
  • Agentless: No software to install on targets. Needs only SSH.
  • Network Automation: Configuring Routers/Switches.
  • 配置管理:Terraform创建虚拟机后,Ansible负责在内部安装软件。
  • 无代理架构:无需在目标主机上安装软件,仅需SSH即可。
  • 网络自动化:配置路由器/交换机。

Quick Start

快速开始

yaml
undefined
yaml
undefined

playbook.yml

playbook.yml

  • name: Configure Webservers hosts: web become: true tasks:
    • name: Install Nginx ansible.builtin.apt: name: nginx state: present
    • name: Start Service ansible.builtin.service: name: nginx state: started
undefined
  • name: Configure Webservers hosts: web become: true tasks:
    • name: Install Nginx ansible.builtin.apt: name: nginx state: present
    • name: Start Service ansible.builtin.service: name: nginx state: started
undefined

Core Concepts

核心概念

Inventory

清单(Inventory)

List of hosts (
hosts.ini
or YAML). Can be dynamic (AWS plugin).
主机列表(存储为
hosts.ini
或YAML格式),支持动态清单(如AWS插件)。

Playbooks

Playbook

YAML files defining the desired state.
定义期望状态的YAML文件。

Roles / Collections

角色/集合(Roles / Collections)

Reusable units of code. Download from Ansible Galaxy.
可复用的代码单元,可从Ansible Galaxy下载。

Best Practices (2025)

2025年最佳实践

Do:
  • Use Collections: The modern packaging format.
    ansible.builtin
    is better than short modules.
  • Use
    ansible-lint
    : Enforce best practices.
  • Idempotency: Ensure running a playbook twice produces the same result without errors.
Don't:
  • Don't use
    shell
    module
    : Unless absolutely necessary. Use specific modules (
    user
    ,
    file
    ,
    apt
    ) for key tasks.
建议
  • 使用集合:采用现代化的打包格式,优先使用
    ansible.builtin
    而非短模块。
  • 使用
    ansible-lint
    :强制遵循最佳实践。
  • 幂等性:确保重复运行Playbook两次能得到相同结果且无错误。
不建议
  • 避免使用
    shell
    模块
    :除非绝对必要,核心任务应使用专用模块(如
    user
    file
    apt
    )。

References

参考资料