利用ssh-agent提升ansible使用的方便及安全性

本文写作时间:2022-02-15

本文主体思想

ansible最合理的使用方法:

1.多主机按模块用几组ssh秘钥对(保障安全)

2.ssh秘钥对的key进行加密,不同的key设置不同的密码,防止别人登录ansible主机能操作所有服务器

可参考本文做配置。在满足上面推荐的使用方法下,能像所有主机只用一个秘钥对而且key不加密一样方便而且更加安全合理。

一、用密码的弊端

1.需要把服务器的密码明文写到ansible配置文件中。有一定的安全风险,不应采用

二、 用秘钥对的情况

1、如果秘钥对的私钥没有设置密码。有一定的安全风险,不应采用

2、如果管理的所有服务器都用的是同一个秘钥对。有一定的安全风险,不应采用

3、如果管理的服务器用的是不同的秘钥对。使用起来麻烦

4、如果秘钥对私钥设置了密码。使用起来麻烦

解决方案ssh-agent

ssh-agent服务器一般都自带可以直接使用,能够解决上面麻烦的事情。

1、ssh-agent可实现多秘钥对自动匹配

直接举例:

IP 主机名 系统 说明
192.168.9.10 vm10 centos 7.9 ansible主机
192.168.9.12 vm12 centos 7.9 被ansible管理的主机,用秘钥对id_test1认证
192.168.9.13 vm13 centos 7.9 被ansible管理的主机,用秘钥对id_test2认证

操作过程(重点!!!)

'#1.生成秘钥对
[root@vm10 ~]\# ssh-keygen   注释: 生成id_test1秘钥对过程(同理生成id_test2秘钥对)
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_test1   注释: 输入生成秘钥对的路径和名字
Enter passphrase (empty for no passphrase):   注释:输入使用私钥时要输入的密码,如果不输入密码,不安全。
Enter same passphrase again:                  注释:再次输入密码
....生成秘钥对成功,生成2个文件(id_test1和id_test1.pub)

[root@vm10 ~]\# ssh-copy-id -i /root/.ssh/id_test1 192.168.9.12   注释:copy秘钥到被管理主机(vm13同理)
[email protected]'''s password:     注释:输入被管理主机的密码
....copy秘钥成功

#2.验证ssh连接,即便是有秘钥对也需要输入密码,保证了安全性。
[root@vm10 ~]\# ssh -i /root/.ssh/id_test1 192.168.9.12   注释: -i 用指定的私钥
Enter passphrase for key '/root/.ssh/id_test1':     注释:输入上面设置的passphrase密码
Last login: Tue Feb 15 16:29:30 2022 from 192.168.9.10
[root@vm12 ~]\# 
....成功连接到vm12

#3.此时的问题点:不同服务器用不同秘钥对需要用-i指定私钥;还需要输密码
比如:
[root@vm10 ~]\# ssh  -i /root/.ssh/id_test1 192.168.9.12   注释: ssh需要输密码
[root@vm10 ~]\# ssh  -i /root/.ssh/id_test2 192.168.9.13
[root@vm10 ~]\#  ansible --key-file /root/.ssh/id_test1 192.168.9.12 -m ping
Enter passphrase for key '/root/.ssh/id_test1':  注释: ansible也需要输密码,如果操作多台不同秘钥对没法玩了
192.168.9.12 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

#4.ssh-agent来啦,解决上面的问题点
具体原理可网上查(参考链接放到本文的下方)。大概意思是(非常不严谨的说):每个秘钥对都有指纹,ssh-agent算是中间商,多个秘钥对时能自动匹配到合适的;assphrase密码认证后,放到内存中,可多次使用。

步骤一,运行ssh-agent
[root@vm10 ~]\# ssh-agent bash

步骤二,添加可能用到到秘钥对并完成assphrase密码认证(后面就不用再认证了)
[root@vm10 ~]\# ssh-add /root/.ssh/id_test1
Enter passphrase for /root/.ssh/id_test1:
Identity added: /root/.ssh/id_test1 (/root/.ssh/id_test1)
[root@vm10 ~]\# ssh-add /root/.ssh/id_test2
Enter passphrase for /root/.ssh/id_test2:
Identity added: /root/.ssh/id_test2 (/root/.ssh/id_test2)

步骤三,验证结果:在多个秘钥的情况下,不管是ssh还是ansible,都可以直接操作了,不再需要指定私钥了。爽歪歪
[root@vm10 ~]\# ssh 192.168.9.12
Last login: Tue Feb 15 17:32:31 2022 from 192.168.9.10
[root@vm12 ~]\# exit
登出
Connection to 192.168.9.12 closed.
[root@vm10 ~]\# ssh 192.168.9.13
Last login: Tue Feb 15 17:33:20 2022 from 192.168.9.10
[root@vm13 ~]\# exit
登出
Connection to 192.168.9.13 closed.
[root@vm10 ~]\# ansible 192.168.9.12 -m ping
192.168.9.12 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[root@vm10 ~]\# vim /etc/ansible/hosts
[root@vm10 ~]\# tail -3 /etc/ansible/hosts
[test]
192.168.9.12
192.168.9.13
[root@vm10 ~]\# ansible test -m ping
192.168.9.13 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.9.12 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
[root@vm10 ~]\#

步骤四,清理认证信息
[root@vm10 ~]\# ssh-add -d /root/.ssh/id_test1.pub   注释:注意这里的pub文件
Identity removed: /root/.ssh/id_test1.pub (root@vm10)
[root@vm10 ~]\# ssh-add -d /root/.ssh/id_test2.pub   注释:注意这里的pub文件
Identity removed: /root/.ssh/id_test2.pub (root@vm10)
验证清理结果(要密码了)
[root@vm10 ~]\# ssh -i /root/.ssh/id_test1  192.168.9.12
Enter passphrase for key '/root/.ssh/id_test1':
[root@vm10 ~]\# ansible --key-file /root/.ssh/id_test1 192.168.9.12 -m ping
Enter passphrase for key '/root/.ssh/id_test1':

#步骤五,清理ansible到客户端的ssh长连接
# ansible默认配置下执行下面命令即可断开ansible服务器到客户端的ssh连接,保证安全性
ps -ef | grep '.ansible/cp' | grep -v grep | awk '{
    
    print $2}' | xargs -i kill {}

涉及的配置项control_path_dir(上面命令的'.ansible/cp')
原理:采用长连接提高了ansible的工作效率,(ansible空闲时间超过超时时间会自动中断链接,默认为60秒)
所以如果有ssh长链接时,即便按照步骤四清理了认证信息,立即执行ansible命令还是可以执行的(不是最安全)。因为ssh连接已经建立了
如果此时一直操作ansible,则此连接一直能用。如果空闲60秒,就连不上对应的服务器了

步骤六,退出ssh-agent 
[root@vm10 ~]\# ssh-agent -k  
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 2194 killed;
[root@vm10 ~]\# 

总结

通过利用ssh-agent、秘钥对passphrase密码设置、不同主机不同秘钥对、用完清理密码一系列操作。

保证了ansible使用的方便和安全性。

参考链接:

ssh转发代理:ssh-agent用法详解 - 骏马金龙 - 博客园 (cnblogs.com)

猜你喜欢

转载自blog.csdn.net/sinat_24354307/article/details/122949917