Python自动化运维:Ansible安装与SSH无密码配置详解
本文详细介绍Ansible在Linux环境下的安装与基础配置流程。通过yum源快速部署主控端,利用hosts文件定义目标主机,并使用ping模块验证连通性。针对SSH密码输入繁琐的问题,深入解析使用ssh-keygen生成密钥及ssh-copy-id分发公钥的方法,实现高效的无密码批量管理。
Ansible安装与基础配置指南
本文详细介绍了Python自动化运维中Ansible的安装与配置方法,旨在为读者提供清晰的部署参考。
一、核心要点
Ansible采用Agentless架构,仅需在管理节点(主控端)部署环境即可。建议通过yum软件源进行安装,以确保依赖关系的完整性和版本稳定性。
Ansible主控端安装与测试
二、Ansible安装步骤
安装过程仅需在主服务器(即主控端)执行,无需在被控节点安装任何客户端软件。
[root@localhost dev]# yum install ansible -y
三、连通性测试
1. 修改主控机上的配置文件 /etc/ansible/hosts,添加目标主机信息。
## green.example.com
## blue.example.com
192.168.0.101
192.168.0.102
[webservers]
## alpha.example.org
## beta.example.org
192.168.0.101
192.168.0.102
2. 执行以下测试命令。
利用ping模块测试主机连通性,分别对单台主机及主机组进行ping操作。若返回SUCCESS状态,则表明Ansible安装及基础测试成功。
[root@localhost ansible]# ansible 192.168.0.101 -m ping -k
SSH password:
192.168.0.101 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@localhost ansible]# ansible webservers -m ping -k
SSH password:
192.168.0.102 | FAILED! => {
"msg": "Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host's fingerprint to your known_hosts file to manage this host."
}
192.168.0.101 | SUCCESS => {
"changed": false,
"ping": "pong"
}
3. 注意事项
由于主控端与被控主机之间尚未配置SSH证书信任关系,执行ansible命令时需添加-k参数以交互式输入root(默认)账号密码。在提示“SSH password:”时输入正确密码即可完成验证。
SSH无密码访问配置详解
四、配置Linux主机SSH无密码访问
1. 核心思路
为避免每次下发指令时手动输入目标主机密码,配置SSH无密码认证是最佳实践。推荐使用ssh-keygen生成密钥对,并结合ssh-copy-id工具快速下发公钥,从而实现自动化批量管理。
第一步:配置目标设备的密钥认证支持。
[root@localhost home]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:9/pGNxnQVWAWpss7PYtJcUDyHsCexgYY6NGWy/oOhTg root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| o.+ .o ..*++|
| o = . .=.=. |
| . + . + .=. |
| ...o *o +. |
| E ... So. = .o |
| ... . ..=+ |
| .. .=.o. |
| .. o.+ o |
| .. .o+ . |
+----[SHA256]-----+
私钥文件默认存储路径为“~/.ssh/id_rsa”。
第二步:将公钥文件id_rsa.pub同步至目标主机。推荐使用ssh-copy-id工具进行公钥拷贝,该命令能自动处理权限设置并追加公钥至authorized_keys文件。
[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.0.102
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.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
Kernel \r on an \m
root@192.168.0.102's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.0.102'"
and check to make sure that only the key(s) you wanted were added.
更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述的Ansible安装与配置步骤能对您的Python程序设计与自动化运维工作有所帮助。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









