Ansible是一款简单的运维自动化工具,只需要使用ssh协议连接就可以来进行系统管理,自动化执行命令,部署等任务。
Ansible的优点
- 不需要安装客户端,不需要运行服务
 - 使用python开发的一套自动执行任务的模块
 - playbook采用yaml配置,结构清晰
 
Ansible的组成结构
- Ansible:核心命令工具,一次性或临时性执行的操作都由该工具执行
 - Ansible playbook:任务剧本(又称任务集),编排定义Ansible任务集的配置文件,由Ansible执行,格式是
yaml - Inventory:Ansible管理的主机,在
/etc/ansible/hosts中配置 - Modules:Ansible执行命令的功能模块,Ansible2.3版本为止,共有1039个模块。还可以自定义模块。
 - Plugins:插件,模块功能的补充,常有连接类型插件,循环插件,变量插件,过滤插件,插件功能用的较少。
 - API:提供给第三方程序调用的应用程序编程接口。
 
Ansible常用的功能模块
命令模块
  command
  shell
文件模块
  copy
  fetch
  file
安装模块
  yum
服务模块
  service
挂载模块
  mount
定时任务
  cron
用户模块
  group
  user
压缩解压
  unarchive
Ansible安装
> yum install ansible -y
配置主机
> vim /etc/ansible/hosts
[web]
192.168.2.10 ansible_ssh_port=22 ansible_ssh_user=rumenz ansible_ssh_pass="123456"
192.168.2.11 ansible_ssh_port=22 ansible_ssh_user=rumenz ansible_ssh_pass="123456"
我们定义了一个web组(可以加很多主机),Ansible默认使用的是ssh协议,指定好端口,账号,密码就可以了。
除了使用账号,密码的形式配置,我们也可以用秘钥,后面访问。
测试
> ansible web -m ping
web就是上面我们定义的分组,-m我后面指定模块,这里我们使用ping模块,用于查看主机是否网络可达。
使用Ansible执行pwd命令
> ansible  web -m shell -a "pwd"
这里我们使用的是shell 模块,-a后面跟上需要执行的shell命令
使用Ansible查看web分组下nginx服务是否在运行
> ansible web -m shell -a "ps -ef | grep nginx"
使用Ansible简单批量安装redis
> ansible web -m shell -a "yum install redis -y"
注意复杂的软件安装,需要编写Ansible playbook配置文件,更加灵活。
原文链接:https://rumenz.com/rumenbiji/linux-ansible-quick.html
微信公众号:入门小站
- 回复【1001】获取 linux常用命令速查手册
 - 回复【10010】获取 阿里云ECS运维Linux系统诊断
 - 回复【10012】获取 Linux学习笔记【强悍总结值得一看】
 - 回复【10013】获取 shell简明教程
 











