为什么用 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=yesAnsible 能将重复劳动变为一键执行,强烈推荐。
