环境准备:
m01管理服务器(外网:10.0.0.61;内网:172.16.1.61)
backup(外网:10.0.0.41;内网:172.16.1.41)
nfs01(外网:10.0.0.31;内网:172.16.1.31)
web01(外网:10.0.0.7;内网:172.16.1.7)
一、SSH秘钥认证部署补充※※※
1.1 涉及到的命令及参数
ssh-keygen:生成秘钥
参数
-t:指定秘钥类型
-f:指定秘钥的位置
-P:命令行指定密码
ssh-copy-id:分发秘钥
参数
-i:指定公钥的位置,默认当前用户~/.ssh/id_dsa.pub
for循环
1.2非交互式创建秘钥和非交互式分发公钥
1>#非交互式(一键)创建秘钥
ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ''
[root@m01 ~]# ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ''
Generating public/private dsa key pair.
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
SHA256:sd/stqyQYAZqW6ekxsp5N6egCpWmOaXcSGAPsMFKvCI root@m01
The key's randomart image is:
+---[DSA 1024]----+
|= |
|.* |
|=o. . . |
|E.oo . o |
|o.B.o = S |
|oXo= = . o o |
|*o=o. o . o |
|o+o..o . . o. |
|++. ..+ .o+. |
+----[SHA256]-----+
[root@m01 ~]#
2>#非交互式(一键)分发公钥
sshpass -p123456 ssh-copy-id -oStrictHostKeyChecking=no 172.16.1.41
sshpass为ssh提供密码,-p参数指定明文密码;sshpass命令需要自己安装,安装命令为:yum install -y sshpass
[root@m01 ~]# sshpass -p123456 ssh-copy-id -oStrictHostKeyChecking=no 172.16.1.41
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_dsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
Number of key(s) added: 1
Now try logging into the machine, with: "ssh -o 'StrictHostKeyChecking=no' '172.16.1.41'"
and check to make sure that only the key(s) you wanted were added.
[root@m01 ~]#
3>#检查公钥是否分发成功
[root@m01 ~]# ssh 172.16.1.41 hostname
backup
[root@m01 ~]#
1.3 批量分发公钥
使用for循环即可
for item in 7 31 41
do
ip=172.16.1.$item
sshpass -p123456 ssh-copy-id -oStrictHostKeyChecking=no $ip
done
1.4 编写一个脚本,执行脚本实现“非交互式创建秘钥”和“非交互式分发秘钥”
[root@m01 ~]# cat /server/scripts/keygen.sh
#!/bin/bash
. /etc/profile
#create keygen
ssh-keygen -t dsa -f ~/.ssh/id_dsa -P ''
#piliang fen fa miyao
for item in 5 6 7 8 31 41 51
do
ip=172.16.1.$item
sshpass -p123456 ssh-copy-id -oStrictHostKeyChecking=no $ip
done
#检查脚本执行的是否有问题
[root@m01 ~]# ansible all -m command -a 'hostname'
172.16.1.31 | CHANGED | rc=0 >>
nfs01
172.16.1.7 | CHANGED | rc=0 >>
web01
172.16.1.41 | CHANGED | rc=0 >>
backup
[root@m01 ~]#
二、ansible入门(在m01上操作)※※※
批量安装、批量配置、批量分发
目前有相同功能的有ansible、saltstack、puppet(了解)
相同点:批量管理、都是基于Python开发
不同点:ansible不需要安装客户端、saltstack需要安装客户端
安装软件:yum install -y ansible
2.1 ansible使用
注:使用ansible,先在管理机m01上先配置好秘钥认证
2.1.1 ansible相关文件
[root@m01 ~]# rpm -ql ansible|grep -v /usr/
/etc/ansible
/etc/ansible/ansible.cfg \\配置文件(被管理的服务器列表)
/etc/ansible/hosts \\主机清(ansible inventory)
/etc/ansible/roles
2.1.2 ansible相关的命令及参数
首先在/etc/ansible/hosts进行配置
[root@m01 ~]# tail -3 /etc/ansible/hosts
[oldboy]
172.16.1.41
172.16.1.31
172.16.1.7
[root@m01 ~]#
1>格式:
注:主机名称是指/etc/ansible/hosts
2>参数:
-m:指定模块
ping 检查客户端是否存活
command:命令模式;只能执行简单命令,不支持特殊符号(默认命令模块)-----可省略
[root@m01 ~]# ansible oldboy -m ping
172.16.1.41 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
172.16.1.7 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
[root@m01 ~]#
-a:模块中命令或参数
[root@m01 ~]# ansible oldboy -m command -a 'hostname'
172.16.1.31 | CHANGED | rc=0 >>
nfs01
172.16.1.7 | CHANGED | rc=0 >>
web01
172.16.1.41 | CHANGED | rc=0 >>
backup
[root@m01 ~]# ansible oldboy -a 'hostname'
172.16.1.41 | CHANGED | rc=0 >>
backup
172.16.1.31 | CHANGED | rc=0 >>
nfs01
172.16.1.7 | CHANGED | rc=0 >>
web01
[root@m01 ~]#
2.1.3 颜色
绿色:OK 代表对方服务器没有发送修改
黄色:OK 表示对方服务器修改内容(修改成功)
红色:报错
紫色:警告
三、AD-Hoc模式※※※
ansible的两种模式
AD-Hoc模式:临时命令,不会保存
playbook模式
ansible查看帮助方法
帮助查询
ansible-doc copy:查询指定某个模块参数用法信息
ansible-doc -l:查看所有模块说明信息
官网查询帮助
https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module
参数上带required表示这个参数必须要有
3.1 AD-Hoc模式的常用模块※※※
1>command(命令模块)----默认模块,可以省略
[root@m01 ~]# ansible oldboy -m command -a 'hostname'
172.16.1.31 | CHANGED | rc=0 >>
nfs01
172.16.1.7 | CHANGED | rc=0 >>
web01
172.16.1.41 | CHANGED | rc=0 >>
backup
2>shell(命令模块):支持特殊符号、支持脚本
\\\批量更新backup、web01、nfs01三台主机的时间
[root@m01 ~]# ansible all -m shell -a 'ntpdate ntp1.aliyun.com'
172.16.1.41 | CHANGED | rc=0 >>
28 May 20:29:20 ntpdate[10115]: step time server 120.25.115.20 offset 2.786290 sec
172.16.1.31 | CHANGED | rc=0 >>
28 May 20:29:20 ntpdate[9550]: step time server 120.25.115.20 offset 2.787557 sec
172.16.1.7 | CHANGED | rc=0 >>
28 May 20:29:21 ntpdate[9685]: step time server 120.25.115.20 offset 2.787166 sec
[root@m01 ~]#
\\\检查backup、web01、nfs01三台主机的时间
[root@m01 ~]# ansible all -m shell -a 'date'
172.16.1.31 | CHANGED | rc=0 >>
Tue May 28 20:30:38 CST 2019
172.16.1.7 | CHANGED | rc=0 >>
Tue May 28 20:30:38 CST 2019
172.16.1.41 | CHANGED | rc=0 >>
Tue May 28 20:30:38 CST 2019
[root@m01 ~]#
3>copy(复制模块)
参数:
src:源,文件从哪里来
dest:目标,复制到远程哪里去
owner:所有者
group:用户组
mode:权限
backup:是否备份,对远端已有的数据
content:向被批量推送的文件中添加内容,与src冲突,只能二选一
\\将管理机/etc/hostname推送到backup、web01、nfs01三台主机的/tmp/下
[root@m01 ~]# ansible all -m copy -a 'src=/etc/hostname dest=/tmp/hostname'
\\检查是否成功
[root@m01 ~]# ansible all -m shell -a 'cat /tmp/hostname'
172.16.1.41 | CHANGED | rc=0 >>
m01
172.16.1.7 | CHANGED | rc=0 >>
m01
172.16.1.31 | CHANGED | rc=0 >>
m01
[root@m01 ~]#
4>scripts(脚本模块):先把脚本传输到服务器,然后执行
[root@m01 ~]# #ansible all -m script -a "/server/scripts/yum.sh"
[root@m01 ~]# ansible all -a 'rpm -qa ipvsadm'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If you need to use command because
yum, dnf or zypper is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message. \\就是告诉你安装命令yum也可以支持
172.16.1.41 | CHANGED | rc=0 >>
ipvsadm-1.27-7.el7.x86_64
172.16.1.7 | CHANGED | rc=0 >>
ipvsadm-1.27-7.el7.x86_64
5>yum(软件安装模块)
参数
name:指定要安装的软件包名称
state:指定使用yum的方法
present:安装
absent:关闭或删除
latest:更新
\\\批量更新backup、web01、nfs01这三台机器的tree命令
[root@m01 ~]# ansible all -m yum -a "name=tree state=latest"
6>file(文件配置模块):相当于touch、mkdir、ln、rm,不支持通配符
参数:
path:指定路径或文件名**
state:指定文件创建的类型
directory:在远端创建目录
touch:在远端创建文件**
link:link或hard表示创建链接文件
absent:删除文件或目录
mode:设置文件或目录权限
owner:设置文件或目录属主信息
group:设置文件或目录属组信息
\\\批量创建目录
[root@m01 ~]# ansible all -m file -a 'path=/tmp/a state=directory mode=555 owner=root group=root '
\\\批量创建文件
[root@m01 ~]# ansible all -m file -a "path=/data/a/b/c/oldboy.txt state=touch"
7>serivce(服务模块):关闭、开启、重启
参数
name:指定要启动服务的名称
state:指定服务启动状态
started:启动服务
stopped:停止服务
restarted:重启服务
reloaded:重载服务
enabled:开机自启(yes或no)
ansible all -m service -a 'name=crond state=started enabled=yes'
8>group(组模块)
name:指定创建的组名
gid:指定组的gid
state
absent:移除远端主机的组
present:创建远端主机的组(默认)
\\\创建oldgirl的用户组
[root@m01 ~]# ansible all -m group -a 'name=oldgirl gid=888'
9>user(用户模块)
参数
name
uid
group
shell:指定命令解释器
create_home:是否创建家目录
\\\创建用户oldgirl指定uid和gid 888,不创建家目录也不允许登陆
ansible all -m user -a 'name=oldgirl uid=888 group=oldgirl shell=/sbin/nologin create_home=no '
\\\检查
[root@m01 ~]# ansible all -m shell -a 'grep "oldgirl" /etc/passwd'
172.16.1.41 | CHANGED | rc=0 >>
oldgirl:x:888:888::/home/oldgirl:/sbin/nologin
172.16.1.31 | CHANGED | rc=0 >>
oldgirl:x:888:888::/home/oldgirl:/sbin/nologin
172.16.1.7 | CHANGED | rc=0 >>
oldgirl:x:888:888::/home/oldgirl:/sbin/nologin
[root@m01 ~]#