欢迎来到程序员中文网!

首页 Linux Mysql C++ Python PHP JavaScript 资源下载 动态 开源推荐
我要投稿 投诉建议

Linux 批量运维:Ansible 入门

时间:2026年08月12日 04:46:50 浏览:1

为什么用 Ansible?


Ansible 是无代理的自动化运维工具,通过 SSH 执行命令,无需在目标机器安装 Agent。


安装与配置


# 控制端安装
yum install ansible -y

# 定义主机清单 /etc/ansible/hosts
[web]
192.168.1.10
192.168.1.11

[db]
192.168.1.20

常用模块


# 批量 ping
ansible all -m ping

# 批量执行命令
ansible web -m shell -a "df -h"

# 批量拷贝文件
ansible web -m copy -a "src=/etc/hosts dest=/etc/hosts"

# 批量安装软件
ansible web -m yum -a "name=nginx state=present"

Playbook 示例


---
- hosts: web
tasks:
- name: 安装 Nginx
yum: name=nginx state=latest
- name: 启动服务
service: name=nginx state=started enabled=yes

Ansible 能将重复劳动变为一键执行,强烈推荐。