linux Ansible安装完毕后,测试报连接主机显示报错

一、问题描述

  1. 当安装完毕Ansible之后,一般需要以SSH的方式连接到需要进行管理的目标主机进行测试。但是报了以下问题:
[root@inmshgis24 ~]# ansible 10.130.0.33 -m ping 
10.130.0.33 | UNREACHABLE! => {

"changed": false, 
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", 
"unreachable": true

}

二、 原因分析

  1. 当我们将10.130.0.33主机作为管理主机,但在管理主机上并未添加目标节点(即需要管理的节点)的ssh认证信息。

三、解决步骤

  1. 管理主机节点生成SSH-KEY
[root@inmshgis24 ~]# ssh-keygen

注意:检查~.ssh/目录下是否出现生成的ssh密钥文件:id_rsa及id_rsa.pub

  1. 添加目标主机节点的SSH认证信息
[root@inmshgis24 ~]# ssh-copy-id root@目标节点IP

说明:
(1)root:是在目标节点上登录的用户;
(2)@:符号后面接目标节点IP即可;
(3)提示:输入目标节点root用户密码,输入即可;
(4)添加认证信息后,目标节点主机的~/.ssh/目录下将会出现一个authorized_keys文件,里面包含了ansible管理主机节点的公钥信息,可以检查一下是否存在。

  1. 确定目标主机节点的SSH认证信息都已正确添加
  2. 目标主机节点的~/.ssh/目录下存在管理节点的公钥信息后,再执行之前出错的ansible ping指令:
[root@inmshgis24 ~]# vi/etc/ansible/hosts
[test]
10.130.0.33
[root@inmshgis24 ~]# ansible 10.130.0.33 -m ping  

linux Ansible安装完毕后,测试报连接主机显示报错_第1张图片
补充:

  1. 期间在ssh-copy-id root@目标节点IP时,报了这个错误:
    bash: ssh-copy-id: command not found
  2. 解决如下:
[root@inmshgis24 ~]# yum -y install openssh-clients #或许你对python的版本进行过升级(python2.6 -> python2.7),可能会报以下的错误( No module named yum)
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   No module named yum

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
2.7.5 (default, Aug  4 2017, 00:39:18) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

[说明] 造成yum出错的根本原因:
你改变了python2的版本,yum是要依赖python的,只要你动了(重装、删除等等)都会造成这个错误。
解决办法:(此处我是对python的版本由2.6升级到2.7)

[root@inmNODEsh05 ansible-2.4]# vim /usr/bin/yum #!/usr/bin/python   --->   #!/usr/bin/python2.6即可 
#!/usr/bin/python2.6
import sys
try:
    import yum
except ImportError:
    print >> sys.stderr, """\
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   %s

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
%s

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq
  
""" % (sys.exc_value, sys.version)
    sys.exit(1)

sys.path.insert(0, '/usr/share/yum-cli')
try:
    import yummain
    yummain.user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt, e:
    print >> sys.stderr, "\n\nExiting on user cancel."
    sys.exit(1)

[注意] 如果原本依赖的python版本没了,那就只能重新安装yum了;
看一下yum是否已经可以正常使用喽!

[root@inmNODEsh05 ansible-2.4]# yum
Loaded plugins: fastestmirror, security
You need to give some command
Usage: yum [options] COMMAND

List of Commands:

check          Check for problems in the rpmdb
check-update   Check for available package updates
clean          Remove cached data
deplist        List a package's dependencies
distribution-synchronization Synchronize installed packages to the latest available versions
downgrade      downgrade a package
erase          Remove a package or packages from your system
groupinfo      Display details about a package group
groupinstall   Install the packages in a group on your system
grouplist      List available package groups
groupremove    Remove the packages in a group from your system
help           Display a helpful usage message
history        Display, or use, the transaction history
info           Display details about a package or group of packages
install        Install a package or packages on your system
list           List a package or groups of packages
load-transaction load a saved transaction from filename
makecache      Generate the metadata cache
provides       Find what package provides the given value
reinstall      reinstall a package
repolist       Display the configured software repositories
resolvedep     Determine which package provides the given dependency
search         Search package details for the given string
shell          Run an interactive yum shell
update         Update a package or packages on your system
update-minimal Works like update, but goes to the 'newest' package match which fixes a problem that affects your system
updateinfo     Acts on repository update information
upgrade        Update packages taking obsoletes into account
version        Display a version for the machine and/or available repos.
[root@inmNODEsh05 ansible-2.4]# yum -y install openssh-clients
Loaded plugins: fastestmirror, security
Determining fastest mirrors
base                                                                                                                                                                         | 4.0 kB     00:00     
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package openssh-clients.x86_64 0:5.3p1-94.el6 will be installed
--> Processing Dependency: openssh = 5.3p1-94.el6 for package: openssh-clients-5.3p1-94.el6.x86_64
--> Running transaction check
---> Package openssh.x86_64 0:5.3p1-94.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================================================================================================================================================
 Package                                              Arch                                        Version                                           Repository                                 Size
====================================================================================================================================================================================================
Installing:
 openssh-clients                                      x86_64                                      5.3p1-94.el6                                      base                                      402 k
Installing for dependencies:
 openssh                                              x86_64                                      5.3p1-94.el6                                      base                                      258 k

Transaction Summary
====================================================================================================================================================================================================
Install       2 Package(s)

Total download size: 660 k
Installed size: 1.9 M
Downloading Packages:
(1/2): openssh-clients-5.3p1-94.el6.x86_64.rpm                                                                                                                               | 402 kB     00:00     
(2/2): openssh-5.3p1-94.el6.x86_64.rpm                                                                                                                                       | 258 kB     00:00     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                                16 MB/s | 660 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
** Found 109 pre-existing rpmdb problem(s), 'yum check' output follows:
PyYAML-3.10-11.el7.x86_64 has missing requires of libyaml-0.so.2()(64bit)
git-1.7.1-3.el6_4.1.x86_64 has missing requires of openssh-clients
glibc-2.17-260.el7_6.3.x86_64 has missing requires of glibc-common = ('0', '2.17', '260.el7_6.3')
......
webkitgtk-1.4.3-9.el6_6.i686 has missing requires of libz.so.1
  Installing : openssh-5.3p1-94.el6.x86_64                                                                                                                                                      1/2 
warning: /etc/ssh/moduli created as /etc/ssh/moduli.rpmnew
  Installing : openssh-clients-5.3p1-94.el6.x86_64                                                                                                                                              2/2 
warning: /etc/ssh/ssh_config created as /etc/ssh/ssh_config.rpmnew
  Verifying  : openssh-clients-5.3p1-94.el6.x86_64                                                                                                                                              1/2 
  Verifying  : openssh-5.3p1-94.el6.x86_64                                                                                                                                                      2/2 

Installed:
  openssh-clients.x86_64 0:5.3p1-94.el6                                                                                                                                                             

Dependency Installed:
  openssh.x86_64 0:5.3p1-94.el6                                                                                                                                                                     

Complete!

你可能感兴趣的:(运维管理,Ansible)