ansible的作用和特点
ansible的作用
自动化运维工具
ansible的特点
开源
使用简单
快速上手
支持二次开发
ansible的角色
使用者 统一管理平台
丰富的模块
一个模块是一个功能
使用者
使用者可以是运维、开发或者测试人员
生成密钥对
[root@centos01 ~]# ssh-keygen -t RSA
传输密钥
[root@centos01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.20
修改yum源
[root@centos01 ~]# vim /etc/yum.repos.d/local.repo
清除缓存
[root@centos01 ~]# yum clean all
安装ansible
[root@centos01 ~]# yum -y install ansible
配置ansbile客户端
[root@centos01 ~]# vim /etc/ansible/hosts
4、测试客户端的连通性
客户端配置文件列表测试
[root@centos01 ~]# ansible -i /etc/ansible/hosts web -m ping
加载ping模块测试
[root@centos01 ~]# ansible web -m ping
1、ansible的应用场景和选项
1)ansible应用场景
非固化操作
临时性批量操作
二次开发接口调用
2)ansible的选项
-v:显示详细信息
-i:加载客户端配置文件
-f:指定工作线程,默认5个
-a:指定模块参数
-m:指定模块
-M:指定自定义模块
--host-list:指定计算机列表
--private-key:指定密钥
1)使用ansible交互式工具
[root@centos01 ~]# ansible-console
切换组
root@all (2)[f:5]$ cd web
列出客户端
root@web (2)[f:5]$ list
shell模块作用
远程执行Linux命令
支持重定向和管道符
使用shell模块查看sshd服务状态
[root@centos01 ~]# ansible web -m shell -a 'netstat -anptu | grep sshd'
重定向操作
[root@centos01 ~]# ansible web -m shell -a 'echo "centos02 192.168.100.20" >> /etc/hosts'
查看
[root@centos02 ~]# cat /etc/hosts
command的作用
远程指定Linux命令
不支持管道符重定向
远程切换目录 (切换到根目录列表)
[root@centos01 ~]# ansible web -m command -a 'chdir=/ ls ./'
copy的作用
复制目录或者文件使用
修改复制数据目录
copy常见的选项
dest:目标文件或者目录
src:源文件或者目录
mode:修改目录文件权限
owner:修改所属用户信息
group:修改所属组信息
应用copy
[root@centos01 ~]# ansible web -m copy -a 'src=/etc/hosts dest=/root/host01 mode=777 owner=test group=root'
yum的作用
管理rpm程序包
自动解决依赖关系
常见的选项
name:指定安装程序名字
state:persent安装,latest卸载程序
enablerepo:开启源id
批量化挂载镜像
[root@centos01 ~]# ansible web -m shell -a 'mount /dev/cdrom /mnt'
安装apache
[root@centos01 ~]# ansible web -m yum -a 'name=httpd state=present'
启动http
[root@centos01 ~]# ansible web -m shell -a 'systemctl start httpd'
[root@centos01 ~]# ansible web -m shell -a 'systemctl enable httpd'
监听端口
[root@centos01 ~]# ansible web -m shell -a 'netstat -anptu | grep 80'
service的作用
管理计算机服务
常见的管理选项
name:服务名字
state:started启动、stopped停止、restarted重新启动服务
enabled=yes|no:是否设置开机自动启动
runlevel:设置指定运行级别启动服务
管理apache服务(停止服务)
[root@centos01 ~]# ansible web -m service -a 'name=httpd state=stopped'
user
user作用
批量化管理用户
选项
name:用户名字
state:present创建用户、absent删除用户
uid:指定用户ID
system=yes|no:是否为系统用户
group:指定基本组
groups:附加组
shell:指定用户是否可以登录系统
home:设置宿主目录
password:密码
remove=yes|no:是否删除用户宿主目录
comment:用户描述
创建用户
[root@centos01 ~]# ansible web -m user -a 'name=bob system=yes shell=/bin/bash'