Ansible是目前非常流行的开源配置管理和自动化工具,凭借其简洁的设计、强大的功能和无代理(Agentless)架构,深受运维、开发和云管理员的喜爱。下面我为你详细介绍一下Ansible的核心概念、架构、使用方式以及优缺点。
一、什么是Ansible
Ansible是由Red Hat维护的自动化工具,主要用于配置管理、应用部署、任务自动化和IT基础设施的编排。它的设计目标是简洁、高效、易用,特别适合快速自动化日常运维任务。
二、核心架构
-
控制节点(控制端)
- 也叫"Ansible Master"或" Ansible 控制机器"。运行Ansible命令和Playbook的机器。
- 可以是任何Linux或macOS系统,不需要安装agent。
-
被控节点(Managed Hosts)
- 需要管理的远程服务器或设备。
- 通过SSH(Linux/Unix系统)或WinRM(Windows)与控制节点通信。
- 不需要安装任何agent软件。
-
通信机制
- 通过SSH或WinRM。使用标准协议,不依赖额外安装的代理。
- 传递任务执行的Playbook或命令。
-
Inventory(主机清单文件)
- 描述目标服务器的配置(IP、主机名、组等)。
- 格式以INI,YAML,或动态脚本(如Cloud provider插件)实现。
三、核心概念
-
Playbook :
以YAML格式编写的自动化脚本,描述要在目标节点上执行的任务。例如安装软件、复制文件、配置服务等。
-
Module(模块) :
Ansible提供的功能单元(如包管理、文件复制、服务管理等)。用户也可以编写自定义模块。
-
Task(任务) :
在Playbook中调用模块的具体操作。
-
Role(角色) :
将一组相关的任务、文件、变量等封装成可复用的单元,提高复杂项目的结构化。
-
Inventory(清单) :
定义管理的服务器列表,可以是静态的(文件)或动态生成。
四、基本使用流程
1. 安装Ansible
在控制节点(一般是一台Linux机器)安装:
sudo apt update
sudo apt install ansible # Debian/Ubuntu
# 或
yum install ansible # CentOS
# 也可以使用pip安装:
pip install ansible
2. 配置Inventory
编辑/etc/ansible/hosts
或自定义清单文件,例如hosts.ini
:
[webservers]
192.168.1.10
192.168.1.11
[dbservers]
192.168.1.20
3. 试运行命令(Ad-hoc Command)
快速测试:
ansible all -m ping
-m ping
:使用ping模块测试连通性。- 输出:显示每台机器的响应状态。
4. 编写Playbook
创建install_nginx.yml
示例:
---
- hosts: webservers
become: yes
tasks:
- name: 安装 Nginx
apt:
name: nginx
state: present
- name: 确认 Nginx 服务启动
service:
name: nginx
state: started
enabled: yes
运行:
ansible-playbook install_nginx.yml
五、优点与特点
优点 | 说明 |
---|---|
无代理(Agentless) | 不需要在目标节点安装任何Agent,只通过SSH或WinRM通信即可实现管理。 |
简单易用 | 使用YAML编写Playbook,语法清晰,学习门槛低。 |
跨平台支持 | 支持Linux、Unix、Windows操作系统(部分模块)等。 |
强大的模块系统 | 提供丰富的开箱即用模块,涵盖文件操作、包管理、服务控制、云平台等领域。 |
支持多种复杂场景 | 可以做配额管理、应用部署、持续集成,甚至云基础设施的编排。 |
良好的社区和文档 | 开源社区活跃,官方文档详细,扩展和自定义方便。 |
六、缺点与挑战
- 对大规模环境管理稍慢:默认采用同步SSH连接,管理大量节点时可能效率不够,需要配合其他工具(如Ansible Tower)优化。
- 状态管理能力有限:不像Chef或Puppet持续状态维护,更偏向"推"式的"配置执行"。
- 复杂项目结构需组织:大型项目建议使用Roles、Playbooks架构清晰,否则难以维护。
七、扩展和集成
- Ansible Tower/AWX:提供Web界面、权限管理、作业调度和审计,企业级管理。
- 集成持续集成工具链:与Jenkins、Git等流程无缝结合。
- 云平台支持:支持AWS、Azure、GCP等云资源的动态管理。
八、总结
Ansible以其简单、轻量和强大的功能成为IT基础设施自动化的主流工具之一,尤其适合:
- 快速部署与配置;
- 自动化运维和云资源管理;
- 配合DevOps流程实现持续交付。
如果你还想深入具体使用技巧、角色设计或高级编排,也可以告诉我!
实践
手册参考:Getting started with Execution Environments --- Ansible Community Documentation
安装
sudo apt install ansible
配置
创建并进入目录
么有找到ai说的/etc/ansible目录,自己创建一个
mkdir ansible_quickstart && cd ansible_quickstart
创建一个配置文件
创建文件inventory.ini
[myhosts]
192.0.2.50
192.0.2.51
192.0.2.52
配置文件可以使用ini也可以使用yaml
站点少的时候用ini,多的时候用yaml
测试
查看节点
ansible-inventory -i inventory.ini --list
输出
ansible-inventory -i inventory.ini --list
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped",
"myhosts",
"test"
]
},
"myhosts": {
"hosts": [
"192.0.2.50",
"192.0.2.51",
"192.0.2.52"
]
},
"test": {
"hosts": [
"192.168.1.5",
"192.168.0.1"
]
}
}
ping节点
ansible myhosts -m ping -i inventory.ini
192.0.2.50 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.0.2.50 port 22: Connection timed out",
"unreachable": true
}
192.0.2.51 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.0.2.51 port 22: Connection timed out",
"unreachable": true
}
192.0.2.52 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 192.0.2.52 port 22: Connection timed out",
"unreachable": true
}
可以看到三个没有ping通,2个ping通的没有显示报错。
palybook操作列表
创建一个playbook.yaml配置文件
- name: My first play
hosts: test
tasks:
- name: Ping my hosts
ansible.builtin.ping:
- name: Print message
ansible.builtin.debug:
msg: Hello world
注意,因为msg需要ssh,需要安装库
sudo apt update && sudo apt install sshpass
同时需要在inviens.ini文件中,添加ssh的用户名和口令(当然也可以使用key,那样设置略微要负复杂一些)
[myhosts]
192.0.2.50
192.0.2.51
192.0.2.52
[test]
192.168.1.5 ansible_user=ansibleuser ansible_password=ansible123
192.168.0.1
执行任务
ansible-playbook -i inventory.ini playbook.yaml
任务输出
ansible-playbook -i inventory.ini playbook.yaml
PLAY [My first play] ***********************************************************
TASK [Gathering Facts] *********************************************************
fatal: [192.168.0.1]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.0.1 port 22: Connection refused", "unreachable": true}
[WARNING]: Platform freebsd on host 192.168.1.5 is using the discovered Python
interpreter at /usr/local/bin/python3.12, but future installation of another
Python interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-
core/2.16/reference_appendices/interpreter_discovery.html for more information.
ok: [192.168.1.5]
TASK [Ping my hosts] ***********************************************************
ok: [192.168.1.5]
TASK [Print message] ***********************************************************
ok: [192.168.1.5] => {
"msg": "Hello world"
}
PLAY RECAP *********************************************************************
192.168.0.1 : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0
192.168.1.5 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
执行一体化操作
创建目录并进入
mkdir my_first_ee && cd my_first_ee
创建文件execution-environment.yml,文件内容
version: 3
images:
base_image:
name: registry.fedoraproject.org/fedora:42
dependencies:
python_interpreter:
package_system: python3
ansible_core:
package_pip: ansible-core
ansible_runner:
package_pip: ansible-runner
system:
- openssh-clients
- sshpass
galaxy:
collections:
- name: community.postgresql
创建一个EE容器
用pip安装ansible-builder
pip install ansible-builder
创建一个EE容器叫postgresql_ee
ansible-builder build --tag postgresql_ee
当然,还需要安装docker才可以使用。
使用docker还可以加上--container-runtime docker参数。
列出容器镜像
podman image list
localhost/postgresql_ee latest 2e866777269b 6 minutes ago 1.11 GB
关于运行部分,可以参考这里:
Running your EE --- Ansible Community Documentation
先到这里吧,以后再研究学习。
调试
测试时报错you must install the sshpass program
fatal: [192.168.1.5]: FAILED! => {"msg": "to use the 'ssh' connection type with passwords or pkcs11_provider, you must install the sshpass program"}
解决问题:
sudo apt update && sudo apt install sshpass
一体化操作时报错ansible-builder:未找到命令
ansible-builder build --tag postgresql_ee
ansible-builder:未找到命令
用pip安装ansible-builder
pip install ansible-builder