ts7758258 发表于 2018-1-2 22:31:45

ssh密钥分发与ansible

  笔者Q:972581034 交流群:605799367。有任何疑问可与笔者或加群交流
  当我们公司的服务器达到几十台或几百台或更高的时候,利用批量管理工具管理系统是我们要做的
  常用的批量管理工具有ansible,stalstack.
  那首先我们要实现管理机对所有服务器的免密钥登录---ssh-key
  #管理机生成密钥对
  

# ssh-keygen -t dsa             #-t指定加密的方式,默认为rsa  

  #提示生成的密钥放在/root/.ssh/id_dsa
  #提示是否给生成的密钥再加密一次,回车即可
  #让你再确认一次,回车即可。
  

# ll /root/.ssh/  
total
12
  
-rw-------. 1 root root668 Mar8 09:44>
  
-rw-r--r--. 1 root root598 Mar8 09:44>  
-rw-r--r--. 1 root root 1192 Mar7 21:41 known_hosts
  

  #非交互式生成秘钥对
  

方法1:ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1  
方法2:
echo -e "\n"|ssh-keygen -t dsa -N "" &> /dev/null  

  #第二个里程碑
  #把公钥发送到机器上
  

# ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.41  

  第一次连接要输入yes,记录主机名跟IP,然后输出对方的密码即可
  

# ssh 172.16.1.41  
Last
login: Wed Mar8 09:41:17 2017 from 10.0.0.253  

  #公钥发送过去之后,登录对方的机器不用密码了。
  #非交互式发送密码
  

yum install sshpass -y  

  #用脚本批量给服务器发送密钥
  

#!/bin/bash  

passwd=123456  
IP_ADDR
="31 41 5 51 6 7 8 120"  
.
/etc/init.d/functions  
# 一键生成密钥
  

if ! [ -f ~/.ssh/id_dsa.pub ];then  ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa >/dev/null 2>&1
  echo -e "\033[32m======Local=========\033[0m"
  action "Generate the key!"/bin/true
  
fi
  

  
# 批量发送密钥
  for i in $IP_ADDR;do
  sshpass -p$passwd ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no 172.16.1.${i}">/dev/null 2>&1
  

  if [ $? == 0 ];then
  echo -e "\033[32m=========`ssh 172.16.1.$i hostname`==========\033[0m"
  action"发送成功!!!" /bin/true
  else
  echo -e "\033[31m======172.16.1.$i=======\033[0m"
  action"发送失败!!!" /bin/false
  fi
  done
  

  开始安装ansible
  搭建yum仓库,定制rpm包是自动化运维关键内容,先保留yum安装的软件:
  

sed -i.bak 's#keepcache=0#keepcache=1#g' /etc/yum.conf  

grep keepcache /etc/yum.conf  
keepcache
=1  

  管理机m01安装ansible
  需要先安装epel源
  

##wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo  

  
yum install ansible -y
  

  所有被管理端需要安装:
  

yum install libselinux-python -y  
rpm
-qa libselinux-python  

  ansible配置文件/etc/ansible/hosts
  原创作品,转载请注明出处。http://www.cnblogs.com/Csir/
页: [1]
查看完整版本: ssh密钥分发与ansible