ansible相关介绍、功能用途和特点
批量操作服务器(几十台、几百台、甚至上千台),ansible就是批量操作服务器的工具
应用场景
批量部署服务:比如说几十台服务器都需要安装nginx服务,不可能手工一台台安装,这时候就可以使用ansible进行批量的分发文件,批量的部署这个服务。
配置文件更新:比如多台服务器的配置文件都需要更新的时候,也可以使用ansible
代码上线:比如多台服务器需要发版代码上线,也可以使用ansible
批量创建用户、批量创建目录:比如多台服务器进行用户的创建,目录的创建等等
Ansible的主要功能
- 批量执行远程命令,可以对N多台主机同时进行命令的执行。
- 批量配置软件服务,可以进行自动化的方式配置和管理服务。
- 实现软件开发功能,jumpserver堡垒机底层就是使用ansible来实现的自动化批量管理。
- 编排高级的IT任务,ansible的playbook是一门编程语言,可以用力啊描绘一套IT剧本架构,完成复杂的任务。类似shell脚本
- 通过roles角色定义,可以自定义部署某一个角色任务或者某一些角色任务。
ansible的配置文件
[root@localhost ~]# rpm -qc ansible
/etc/ansible/ansible.cfg #主配置文件
/etc/ansible/hosts #主机清单文件
ansible的安装和基本配置
#ansile的安装
#.rpm安装(一般使用yum安装)(推荐使用)
wget -O /etc/yum.repos.d/epel.repo http:mirrors.aliyun.com/repo/epel-7.repo
yum -y install ansible
#或者
yum install epel-release #依赖工具epel源
yum -y install ansible
#确认ansible的版本号
ansible --version
#模块调用测试
ansible localhost -m ping
#ansible的基本配置
vim /etc/ansible/hosts #主机清单文件
#主机清单文件解析
## blue.example.com## 192.168.100.1## 192.168.100.10
# Ex 2: A collection of hosts belonging to the 'webservers' group
## [webservers] #Webserver模块组名字[xxx]
## alpha.example.org
## beta.example.org
## 192.168.1.100 #批量的服务器IP,代表IP100和110两台服务器
## 192.168.1.110
# If you have multiple hosts following a pattern you can specify# them like this:
## www[001:006].example.com
# Ex 3: A collection of database servers in the 'dbservers' group
##[dbservers] #数据库模块组名字
##
## db01.intranet.mydomain.net
## db02.intranet.mydomain.net## 10.25.1.56 #批量的数据库,代表56和57两台服务器
## 10.25.1.57
# Here's another example of host ranges, this time there are noleading os:
## db-[99:101]-node.example.com#修改ansible的配置文件
vim /etc/ansible/ansible.cfg
host_key_checking=False #禁用每次执行ansible命令检查ssh key host,默认是注释,开启即可
log_path = /var/log/ansible.log #开启日志记录,默认注释,开启即可
[accelerate]
accelerate_port= 5099 #加速连接端口,默认注释,开启即可,也可以改变端口号,此处没改
#accelerate timeout = 30
#accelerate connect timeout = 5.0
# The daemon timeout is measured in minutes. This time is measured