第1章 ssh+key实现基于密钥连接(ansible使用前提)
说明:
ansible其功能实现基于SSH远程连接服务
使用ansible需要首先实现ssh密钥连接
1.1 部署ssh key
1.1.1 第一个里程碑: 创建密钥对
复制代码
ssh-keygen
-t 指定密钥类型 rsa1 dsa(常用) ecdsa
语法:
SYNOPSIS
ssh-keygen [-q] [-b bits] -t type [-N new_passphrase] [-C comment]
[-f output_keyfile]
ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]
ssh-keygen -i [-f input_keyfile]
ssh-keygen -e [-f input_keyfile]
ssh-keygen -y [-f input_keyfile]
ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]
ssh-keygen -l [-f input_keyfile]
ssh-keygen -B [-f input_keyfile]
ssh-keygen -D pkcs11
ssh-keygen -F hostname [-f known_hosts_file] [-l]
ssh-keygen -H [-f known_hosts_file]
ssh-keygen -R hostname [-f known_hosts_file]
ssh-keygen -r hostname [-f input_keyfile] [-g]
ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]
ssh-keygen -T output_file -f input_file [-v] [-a num_trials]
[-W generator]
ssh-keygen [-n] [-D smartcard]
ssh-keygen -s ca_key -I certificate_identity [-h] [-Z principals]
[-O option] [-V validity_interval] [-z serial_number] file ...
ssh-keygen -L [-f input_keyfile]
复制代码
创建密钥的过程
复制代码
[root@m01 ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa): #私钥创建后保存的路径
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): #私钥需不需进行加密,设置密码
Enter same passphrase again: #私钥需不需进行加密,再次输入密码确认
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:
31:4a:4f:9f:97:b0:b6:ca:4c:53:78:70:89:83:5f:16 root@m01
The key's randomart image is:
+--[ DSA 1024]----+
| E |
| . . o |
| o B * |
| . = @ + . |
| . S B o |
| + o |
| o . |
| + o |
| + |
+-----------------+
复制代码
创建出来的文件:
[root@m01 ~]# ll /root/.ssh/
total 8
-rw------- 1 root root 668 Oct 17 18:55 id_dsa #创建出来的私钥
-rw-r--r-- 1 root root 598 Oct 17 18:55 id_dsa.pub #创建出来的公钥
1.1.2 第二个里程碑: 分发公钥文件
[root@m01 ~]# man ssh-copy-id
ssh-copy-id - install your public key in a remote machine’s autho-rized_keys
注意:密钥分发命令属于openssh-clients软件包
[root@nfs01 ~]# rpm -qf `which ssh-copy-id`
openssh-clients-5.3p1-122.el6.x86_64
语法格式
复制代码
ssh-copy-id [-i [identity_file]] [user@]machine
-i 指定要分发的公钥文件以及路径信息
[user@] 以什么用户身份进行分发
machine 将公钥分发到哪台主机上,远程主机IP地址
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.41
The authenticity of host '172.16.1.41 (172.16.1.41)' can't be established.
RSA key fingerprint is d3:41:bb:0d:43:88:da:a3:2c:e8:36:91:11:c9:e4:9c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.41' (RSA) to the list of known hosts.
root@172.16.1.41's password:
Now try logging into the machine, with "ssh 'root@172.16.1.41'", and check in
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
复制代码
1.1.3 第三个里程碑: 基于密钥登陆测试
[root@m01 ~]# ssh 172.16.1.41
Last login: Tue Oct 17 18:38:47 2017 from 10.0.0.1
[root@backup ~]#
基于密钥登陆方式成功↑
[root@m01 ~]# ssh root@172.16.1.41 "hostname -i"
172.16.1.41
不用的登陆到远程主机直接执行命令,返回输出结果↑
说明:
管理主机一旦创建好秘钥对文件,给多个主机分发公钥时,公钥文件相同
1.1.4 ssh服务分发公钥实质执行过程
①. 管理服务器创建私钥和公钥(密钥对)
②. 将公钥文件远程传送复制到被管理服务器相应用户~/.ssh/id_dsa.pub下,并修改.ssh目录权限为700
③. 修改公钥文件文件名称为authorized_keys,授权权限为600
④. 利用ssh服务配置文件的配置参数,进行识别公钥文件authorized_keys
⑤. 进而实现基于密钥远程登录服务器(免密码登录/非交互方式登录)
1.2 默认端口号不是22,如何分发公钥
1.2.1 查询ssh-copy-id命令可以得知这是个脚本文件
[root@m01 ~]# file `which ssh-copy-id `
/usr/bin/ssh-copy-id: POSIX shell script text executable
看看脚本内容发现传输方式
[root@m01 ~]# cat `which ssh-copy-id`|grep ssh
ssh $1 "exec sh -c 'cd; umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys && (test -x /sbin/restorecon && /sbin/restorecon .ssh .ssh/authorized_keys >/dev/null 2>&1 || true)'" || exit 1
说明:
1、切换用户到家目录下,临时设置umask值
2、判断客户端相应用户中有没有.ssh目录,如果没有.ssh 目录就进行创建
3、将管理端公钥文件内容添加到客户端~./ssh/authorized_keys, 默认authorized_keys文件不存在,需要创建,文件权限600
1.2.2 实现非22端口的分发
方法一: 修改脚本内容
ssh -p52113 $1 "exec sh -c 'cd; umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys && (test -x /sbin/restorecon && /sbin/restorecon .ssh .ssh/authorized_keys >/dev/null 2>&1 || true)'" || exit 1
说明:根据命令脚本,修改$1传参信息,从而实现根据ssh不同端口传送公钥文件
方法二:将传入的参数上添加上端口信息(推荐)
复制代码
[root@m01 scripts]# ssh-copy-id -i /root/.ssh/id_dsa.pub "-p 52113 znix@172.16.1.250"
Now try logging into the machine, with "ssh '-p 52113 znix@172.16.1.250'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
复制代码
1.2.3 关于 /usr/bin/ssh-copy-id 脚本中 $1的说明
1.2.3.1 编写脚本shift
复制代码
[root@m01 scripts]# cat shift.sh
#!/bin/bash
until [ $# -eq 0 ]
do
echo $*
shift
done
复制代码
测试
复制代码
[root@m01 scripts]# sh shift.sh 1 2 3 4 5 6
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
复制代码
说明:
shift命令用于对参数的移动(左移),通常用于在不知道传入参数个数的情况下依次遍历每个参数然后进行相应处理(常见于Linux中各种程序的启动脚本)。
1
ssh-copy-id -i /root/.ssh/id_dsa.pub "-p 52113 znix@172.16.1.250"
由于/usr/bin/ssh-copy-id 脚本中前面使用了两个shift 所有原本该为的参数变为了3的参数变为了1.
复制代码
if [ "-i" = "$1" ]; then
shift
# check if we have 2 parameters left, if so the first is the new ID file
if [ -n "$2" ]; then
if expr "$1" : ".*\.pub" > /dev/null ; then
ID_FILE="$1"
else
ID_FILE="$1.pub"
fi
shift # and this should leave $1 as the target name
fi
else
复制代码
1.3 实现自动分发公钥,远程管理多台主机
1.3.1 【预备知识】shell中三种循环
复制代码
#for 循环
for n in (1..100)
do
xxx
done
#while循环:循环条件为真时,一直循环;为假时,停止循环
while [ture]
do
xxx
done
#until 循环: 循环条件为假时,一直循环;为真时,停止循环
until [ture]
do
xxx
done
复制代码
1.3.2 实现自动分发公钥,远程管理多台主机的阻碍因素?
01.创建秘钥对需要进行交互
a.需要确认秘钥保存路径
b.需要确认密码信息
02.分发公钥时需要进行交互
a.需要进行确认yes|no
b.第一次分发公钥需要进行密码认证
1.3.3 解决阻碍因素
1.自动保存路径,并且不密码
ssh-keygen -t rsa -f ~/.ssh/id_rsa -N "" -q
参数说明:
1
2
3
4
5
6
7
8
9
10
-f filename Specifies the filename of the key file.
指定密钥文件保存的路径信息(免交互)
-P passphrase Provides the (old) passphrase.
提供一个密码信息
-N new_passphrase Provides the new passphrase.
-P -N 都是免交互方式指定密码信息
-q 安静的 不输出信息,减少信息输出
2.解决分发公钥时需要进行的交互
sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub " root@172.16.1.$ip -o StrictHostKeyChecking=no "
参数说明:
1
2
-o option 选择 (man 手册中可以查到有很多选项)
StrictHostKeyChecking=no 对询问的回应(不进行对密钥检查)
要实现免密码,需要一款软件 sshpass 该软件就是为ssh提供密码使用的
[root@m01 ~]# yum install sshpass -y
注意:密码与 -p之间不能有空格
1.3.4 最终批量分发脚本内容
复制代码
[root@m01 scripts]# vim ssh-key.sh
#!/bin/bash
. /etc/rc.d/init.d/functions
# 创建密钥
\rm ~/.ssh/id_rsa* -f
ssh-keygen -t rsa -f ~/.ssh/id_rsa -N "" -q
# 分发公钥
for ip in 31 41 8
do
sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub " root@172.16.1.$ip -o StrictHostKeyChecking=no " &>/dev/null
if [ $? -eq 0 ];then
action "fenfa 172.16.1.$ip" /bin/true
else
action "fenfa 172.16.1.$ip" /bin/false
fi
echo ""
done
复制代码
脚本执行效果:
[root@m01 scripts]# sh ssh-key.sh
fenfa 172.16.1.31 [ OK ]
fenfa 172.16.1.41 [ OK ]
fenfa 172.16.1.8 [ OK ]
说明:
脚本中引用 . /etc/rc.d/init.d/functions 函数,可以显示执行结果的判断。
使用if语句进行判断,action 执行相应的动作。true/false
1.3.5 实现基于密钥的批量管理脚本
复制代码
[root@m01 scripts]# vim piliang_guanli.sh
#!/bin/bash
CMD=$1
for ip in 8 31 41
do
echo ========host 172.16.1.$ip=======
ssh root@172.16.1.$ip "$CMD"
echo ============END===============
echo ""
done
复制代码
脚本执行效果:
复制代码
[root@m01 scripts]# sh piliang_guanli.sh date
======172.16.1.8======
Thu Oct 19 16:25:08 CST 2017
=========END=============
======172.16.1.31======
Thu Oct 19 16:25:08 CST 2017
=========END=============
======172.16.1.41======
Thu Oct 19 16:25:08 CST 2017
=========END=============
复制代码
基于密钥登陆方式,分发的公钥文件会识别用户信息,所以能够实现免密码批量管理。
第2章 ansible软件介绍
python 语言是运维人员必须会的语言
ansible 是一个基于python 开发的自动化运维工具
其功能实现基于ssh远程连接服务
ansible 可以实现批量系统配置,批量软件部署,批量文件拷贝,批量运行命令等功能
除了ansible之外,还有saltstack 等批量管理软件
2.1 自动化批量管理方式说明
2.1.1 ssh+key方式的说明
免密码登录验证是单向的,方向从私钥(钥匙) >==> 公钥(锁)
SSH免密码登录基于用户的,最好不要跨不同的用户
SSH连接慢解决;即修改sshd_config配罝文件参数信息
批量分发1000台初始都需要输入一次密码,并且第一次连接要确认(expect/sshpass)
expect批量管理服务器参考 http://oldboy.blog.51cto.com/2561410/1206238
2.1.2 企业级生产场景批量管理-自动化管理方案
①.最简单/最常用/最强大的选择是ssh key+shell/pssh方案,一般中小型企业会用(50-100台以下规模企业)
a.利用ssh key执行命令,并将命令放在脚本里面
b.利用ssh key执行命令,将命令放在脚本里面,并加上相应循环语句或判断语句
②.sina cfengine/puppet较早的批量管理工具;现在基本上没有企业用
③.门户级别比较流行的,puppet批量管理工具(复杂/笨重)
④.saltstack批量管理工具;特点:简单,功能强大(配罝复杂>---赶集网/小米/一些CDN公司 批量管理路线:ssh key-->cfengine-->puppet-->saltstack/ansible
PS:使用ansible软件的前提是ssh key公钥分发充成
2.1.3 如何完成成集群规模架构一键自动化实现(步骤说明)
①.1台服务器先配置好(kickstart,cobbler无人值守安装)。高级实现云计算(按需分配,动态调整)(openstack,kvm)
②.linux基本优化,包括ssh服务(可以自动化实现)。
创建密钥信息(自动化免交互创建)
ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa >/dev/null 2>&1
进行批量分发密钥(sshpass,expect自动化实现)
⑤.ansible软件安装(可以自动化实现)
⑥.网络服务自动化安装(ansible实现)
(搭建yum仓库,定制rpm包)
2.2 ansible软件特点概述
l 不需要单独安装客户端(no agents),基于系统自带的sshd服务,sshd就相当于ansible的客户端
l 不需要服务端(no sever)
l 需要依靠大量的模块实现批量管理
l 配置文件 /etc/ansible/ansible.cfg (前期不用配置)
ansible软件相关参考链接信息
http://docs.ansible.com/ansible/intro_installation.html
http://www.ansible.com.cn/
http://docs.ansible.com/modules_by_category.html
http://www.ansible.cn/docs/
2.2.1 ansible软件中查看模块相关信息方法
复制代码
[root@m01 ~]# ansible-doc -l
列出所有模块信息
[root@m01 ~]# ansible-doc -s cron
参看指定模块的帮助
复制代码
2.3 部署ansible软件
2.3.1 第一个里里程碑:部署ssh+key免密码登录方式
参见第一章内容
2.3.2 第二个里程碑:被管理端安装ansible相关管理软件
[root@m01 ~]# yum install libselinux-python -y
该软件是用来对selinux进行设置的,确保即使服务器selinux服务开启,依旧能够通过ansible 软件管理。
2.3.3 第三个里程碑:管理端安装ansible软件,配置hosts文件
[root@m01 ~]# yum install ansible -y
软件安装完成,进行修改ansible下的hosts文件,注意文件的路径
复制代码
[root@m01 ~]# vim /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.41
172.16.1.8
复制代码
文件信息说明:
1.中括号中的名字代表组名
2.主机(hosts)部分可以使用域名、主机名、IP地址表示;一般此类配置中多使用IP地址;
3.组名下的主机地址就是ansible可以管理的地址
至此ansible 服务就部署完成 ↑
2.4 查看ansible软件相关信息
2.4.1 ansible实践部署地址规划
服务器名称
网卡eth0
网卡eth1
用途说明
m01
10.0.0.61
172.16.1.61
批量管理服务器
nfs01
10.0.0.31
172.16.1.31
nfs共享储存服务器
backup
10.0.0.41
172.16.1.41
rsync备份服务器
web01
10.0.0.8
172.16.1.8
web服务器
说明:无特殊情况,子网掩码为255.255.255.0
2.4.2 ansible软件的版本信息
复制代码
[root@m01 ~]# ansible --version
ansible 2.3.2.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
复制代码
2.4.3 软件目前主要会用到的文件
[root@m01 ~]# rpm -ql ansible
/etc/ansible/hosts #定义anisble软件可以管理的主机信息
/usr/bin/ansible #ansible执行命令
/usr/bin/ansible-playboot # ansible执行剧本命令
2.4.4 /etc/ansible下的文件
复制代码
[root@m01 ansible]# ll
total 28
-rw-r--r-- 1 root root 18066 Sep 6 06:38 ansible.cfg #ansible配置文件
-rw-r--r-- 1 root root 1016 Sep 6 06:38 hosts #定义ansible可以管理的主机信息
drwxr-xr-x 2 root root 4096 Sep 6 06:38 roles #主要在自动化的时候部署多台主机时使用
复制代码
2.5 ansible软件的使用/参数
2.5.1 ansible远程批量执行命令
语法:
1
2
3
4
5
ansible oldboy -a "uptime"
ansible oldboy -m command -a "uptime"
ansible 定义的组/单个ip/域名/all -m command -a "uptime"
说明:-m 指定使用的模块
-a 指定使用模块中相应的命令参数
命令参数只能是基本命令,并不支持管道操作
all 为hosts文件中的组全部管理
图2-1 ansible命令语法格式示意图
2.5.2 未分发公钥如何实现远程管理主机及指定ansible端口信息
配置hosts文件时配置上密码
复制代码
vim /etc/ansible/hosts
[oldboy]
172.16.1.31:52113 ansible_ssh_user=root ansible_ssh_pass=123456
172.16.1.41
172.16.1.8
复制代码
IP:端口 用户 密码
1
2
[znix]
www.znix.top:52113 ansible_ssh_user=znix
指定端口 用户名
测试修改端口后的结果 使用ping 模块
复制代码
[root@m01 ~]# ansible znix -m ping
www.znix.top | SUCCESS => {
"changed": false,
"ping": "pong"
}
复制代码
2.6 ansible软件常用参数表
命令参数
参数说明
-m MODULE_NAME
-module-name=MODULE_NAME
module name to execute (default=command)
相应名称的模块被执行(默认模块为command );
-m后边是模块的名宇
-a MODULE_ARGS
-args=MODULE_ARGS
module arguments
模块参数信息
-a后面是要执行的命令;也可以写个ip ,针对台机器来执行命令
-C, -checks
don't make any changes, instead, try to predict some of the changes that may
occurs
不做任何改变;反而,只是尝试预言些可能出现的改变
-syntax-checks
perform a syntax check on the playbook, but do not execute ii*>
执行语法检查在剧本上,但是并不执行剧本
2.6.1 ansible命令执行结果色彩说明:
绿色:表示没有发生任何改变
红色:执行命令操作出现异常
黄色:执行命令后,对受控主机产生影响,发生了配置改变
第3章 ansible中的模块说明
3.1 ping 模块:测试连通性
复制代码
[root@m01 ~]# ansible all -m ping
172.16.1.8 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.41 | SUCCESS => {
"changed": false,
"ping": "pong"
}
172.16.1.31 | SUCCESS => {
"changed": false,
"ping": "pong"
}
复制代码
连接正常返回 pong 通过帮助信息可以获得 ↓
通过 ansible-doc -v ping 可以获得该模块的说明
ansible-doc -s file 参看模块的具体信息
复制代码
[root@m01 ~]# ansible-doc -v ping
Using /etc/ansible/ansible.cfg as config file
> PING (/usr/lib/python2.6/site-packages/ansible/modules/system/ping.py)
A trivial test module, this module always returns `pong' on successful contact. It does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to verify the ability to login and that a usable python is configured. This is NOT ICMP ping, this is just a trivial test module.
复制代码
3.2 command 模块 默认