Ansible自动化运维
一、远程管理
1、远程管理的方式
1)telnet
默认端口23
传输数据没有被加密
安全性差
内部远程管理使用
2)ssh
默认端口22
传输数据被加密
安全性强
适合通过互联网远程管理
3)RDP
默认端口3389
Windows系统使用
4)https
通过浏览器远程管理设备
兼容性强
2、openssh免交互式身份验证
1)openssh验证方式
账户密码:用户远程管理输入账户密码
密钥对:通过公钥和私钥结合身份验证,批量管理服务器使用
生成密钥对
ssh-keygen -t RSA
传输公钥到远程访问服务器端
ssh-copy-id -i root@192.168.100.10
客户端免交互式登录远程访问服务器
ssh root@192.168.100.20
二、ansible
1、ansible的作用和特点
1)ansible的作用
自动化运维工具
2)ansible的特点
开源
使用简单
快速上手
支持二次开发
2、ansible的角色
1)使用者
统一管理平台
2)丰富的模块
一个模块是一个功能
3)使用者
使用者可以是运维、开发或者测试人员
3、安装ansible
安装ansible
yum -y install ansible
配置ansbile客户端
vim /etc/ansible/hosts
4、测试客户端的连通性
客户端配置文件列表测试
ansible -i /etc/ansible/hosts web -m ping
加载ping模块测试
ansible web -m ping
三、ansible的应用
1、ansible的应用场景和选项
1)ansible应用场景
非固化操作
临时性批量操作
二次开发接口调用
2)ansible的选项
-v:显示详细信息
-i:加载客户端配置文件
-f:指定工作线程,默认5个
-a:指定模块参数
-m:指定模块
-M:指定自定义模块
--host-list:指定计算机列表
--private-key:指定密钥
2、ansible交互式管理
使用ansible交互式工具
ansible-console
切换组
cd web
列出客户端
list
3、shll
1)shell模块作用
远程执行Linux命令
支持重定向和管道符
使用shell模块查看sshd服务状态
ansible web -m shell -a 'netstat -anptu | grep sshd'
重定向操作
ansible web -m shell -a 'echo "centos02 192.168.100.20" >> /etc/hosts'
4、command
1)command的作用
远程指定Linux命令
不支持管道符重定向
远程切换目录
ansible web -m command -a 'chdir=/ ls ./'
5、copy
1)copy的作用
复制目录或者文件使用
修改复制数据目录
2)copy常见的选项
dest:目标文件或者目录
src:源文件或者目录
mode:修改目录文件权限
owner:修改所属用户信息
group:修改所属组信息
应用copy
ansible web -m copy -a 'src=/etc/hosts dest=/root/host01 mode=777 owner=test group=root'
5、yum模块
1)yum的作用
管理rpm程序包
自动解决依赖关系
2)常见的选项
name:指定安装程序名字
state:persent安装,latest卸载程序
enablerepo:开启源id
安装apache
ansible web -m yum -a 'name=httpd state=present'
6、service
1)service的作用
管理计算机服务
2)常见的管理选项
name:服务名字
state:started启动、stopped停止、restarted重新启动服务
enabled=yes|no:是否设置开机自动启动
runlevel:设置指定运行级别启动服务
管理apache服务
ansible web -m service -a 'name=httpd state=stopped'
7、user
1)user作用
批量化管理用户
2)选项
name:用户名字
state:present创建用户、absent删除用户
uid:指定用户ID
system=yes|no:是否为系统用户
group:指定基本组
groups:附加组
shell:指定用户是否可以登录系统
home:设置宿主目录
password:密码
remove=yes|no:是否删除用户宿主目录
comment:用户描述
创建用户
ansible web -m user -a 'name=bob system=yes shell=/bin/bash'