安装ansible
首先生成密钥对
[root@centos ~]# ssh-keygen -t RSA
将密钥对发送到20和30
[root@centos ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.20
[root@centos ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.30
删除系统自带的yum源
[root@centos ~]# rm -rf /etc/yum.repos.d/C*
挂载ansible光盘
[root@centos ~]# mount /dev/cdrom /mnt
修改yum源将挂载点修改为/mnt/ansiblerepo/ansiblerepo
[root@centos yum.repos.d]# vim /etc/yum.repos.d/local.repo
保存退出之后删除yum缓存
[root@centos ~]# yum clean all
然后用yum安装ansible
[root@centos ~]# yum -y install ansible
配置ansbile的客户端
[root@centos01 ~]# vim /etc/ansible/hosts
测试客户端的连接性
客户端配置文件列表测试 :
[root@centos ~]# ansible -i /etc/ansible/hosts web -m ping
加载ping模块测试 :
[root@centos ~]# ansible web -m ping
ansible的应用
1、ansible的应用场景和选项
1)ansible应用场景 非固化操作 临时性批量操作 二次开发接口调用
2)ansible的选项
-v:显示详细信息
-i:加载客户端配置文件
-f:指定工作线程,默认5个
-a:指定模块参数
-m:指定模块
-M:指定自定义模块
--host-list:指定计算机列表
--private-key:指定密钥
shell模块的作用:
1.远程执行Linux命令
2.支持重定向和管道符
使用shell模块查看sshd服务状态
command的作用 :
远程指定Linux命令 不支持管道符重定向
远程切换目录 : [root@centos ~]# ansible web -m command -a 'chdir=/ ls ./'
copy的作用: 复制目录或者文件使用 修改复制数据目录
copy常见的选项
dest:目标文件或者目录
src:源文件或者目录
mode:修改目录文件权限
owner:修改所属用户信息
group:修改所属组信息
yum模块
yum的作用:
管理rpm程序包
自动解决依赖关系
常见的选项:
name:指定安装程序名字
state:persent安装
latest卸载程序
enablerepo:开启源id
安装apache : [root@centos01 ~]# ansible web -m yum -a 'name=httpd state=present'
service 模块
service的作用:
管理计算机服务
常见的管理选项:
name:服务名字
state:started启动、stopped停止、
restarted重新启动服务
enabled=yes|no:是否设置开机自动启动
runlevel:设置指定运行级别启动服务
user模块
user作用 :
批量化管理用户
选项
name:用户名字
state:present创建用户、absent删除用户
uid:指定用户ID
system=yes|no:是否为系统用户
group:指定基本组
groups:附加组
shell:指定用户是否可以登录系统
home:设置宿主目录
password:密码
remove=yes|no:是否删除用户宿主目录
comment:用户描述
创建用户:
[root@centos ~]# ansible web -m user -a 'name=bob system=yes shell=/bin/bash'