ansible配置private key来免密码登录

最近在作自动化部署发布.读了一下ansible的代码和工作流.

需要部署一些免密码登录的操作. 记录下

现在有两台机器, 一台服务器A安装了ansible, 一台是服务器B需要被操作的.

随便建立一个项目文件夹.

为了方便管理, 我这样划分的项目 
在conf里面放所有的配置, 包括hosts和ansible.cfg, 然后作一个软链接到最外面. ansible.cfg的优先级将是当前目录最高 
aansible  tree
在ssh_keys里面, 存放着很多服务器的私钥. 注意是私钥. 
ansible用了paramiko库.

ansible.cfg 配置如下. 注意ssh_connection下面的contrl_path=./ssh_keys, 不指定的话会一直报group-readable or world-readable and thus insecure

 

推荐:Hadoop集群中的ssh无密码登录配置

       说明:在hadoop集群配置过程中需要配置ssh无密码登录,这一步不是必须的,有很多人都误为认为必须要配置ssh无密码登录,这种理解是不正确的,但为什么平

 

[defaults]
inventory       = ./conf/hosts
pull_interval   = 15
sudo_user       = root
transport       = paramiko
module_lang     = C
host_key_checking = False
sudo_exe        = sudo
# SSH timeout
timeout         = 30
#remote_user = root
#remote_port = 22
remote_tmp      = $HOME/.ansible/tmp

[ssh_connection]
# if True, make ansible use scp if the connection type is ssh, default is sftp
scp_if_ssh      = True
#sftp_batch_mode= False
control_path = ./ssh_keys

去服务器B上生成密钥对,

#ssh root@服务器B
#ssh-keygen 如果要重命名可以自己指定, 回车后生成密钥对
# echo ~/.ssh/id_rsa.pub >> known_hosts 这里把生成的公钥放入known_hosts
如果自己配置了ssh_config, 关闭了known_hosts, 可能就需要写进~/.authorized_keys里面去了.

把服务器B上的私钥回传到ansible执行的服务器A的ssh_keys里, 命名为 
服务器B_ip_.key

给ssh_keys文件夹授权为700, 一定要700, 其它的都会报错.

建立hosts文件里面指定server, 每个server一行.这里我测试就写一行. 
[test_server] 
10.0.1.5 ansible_ssh_private_key_file=ssh_keys/10.0.1.5.key ansible_ssh_user=root

现在回到服务器A的这个ansible文件夹下测试: 
ansible test_server -m shell -a ‘ls -la /etc/hostname’

10.0.1.5 | success | rc=0 >>
-rw-r--r-- 1 root root 15 Jun  4  2012 /etc/hostname

无密码成功.

 

推荐:Ubuntu和CentOS配置SSH无密码登录

一、安装ssh ubuntu中使用命令: $sudo apt-get install openssh-server openssh-client centos中使用命令: $sudo yum install openssh-server openssh-client

最近在作自动化部署发布.读了一下ansible的代码和工作流. 需要部署一些免密码登录的操作. 记录下 现在有两台机器, 一台服务器A安装了ansible, 一台是服务器B需要被操作的. 随便建立一个项目文件

转载于:https://my.oschina.net/javagg/blog/3051887

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值