【问题标题】:Ansible ping not workingAnsible ping 不工作
【发布时间】:2018-05-24 11:36:55
【问题描述】:

下面是我的主机文件/etc/ansible/hosts

root@ubuntu:/home/dasitha# cat /etc/ansible/hosts
[web]
10.0.0.112 ansible_user=root

[local]
127.0.0.1 ansible_user=root

当我在下面执行ansible all -m ping -vv 时会抛出错误。

ansible 2.5.3
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.6 (default, Jun 22 2015, 18:00:18) [GCC 4.8.2]
Using /etc/ansible/ansible.cfg as config file
META: ran handlers

    10.0.0.112 | UNREACHABLE! => {
        "changed": false,
        "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).\r\n",
        "unreachable": true
    }
    127.0.0.1 | UNREACHABLE! => {
        "changed": false,
        "msg": "Failed to connect to the host via ssh: Permission denied (publickey,password).\r\n",
        "unreachable": true
    }

【问题讨论】:

  • 这是一个 ssh 问题而不是 ansible 问题。你能从你的本地机器ssh root@10.0.0.112 ssh 到 10.0.0.112 吗?
  • 检查 2 台机器上的 sshd 日志。如果幸运的话,您可能会看到一个清晰的错误描述:systemctl status sshd
  • @tux ssh root@10.0.0.112 root@10.0.0.112's password: Welcome to Ubuntu 14.04.5 LTS (GNU/Linux 4.4.0-31-generic i686)it 提示输入密码。当我输入密码时。工作正常
  • @ilias-sp 这是我收到的唯一日志条目May 24 17:32:35 ubuntuClient sshd[2389]: Connection closed by 10.0.0.111 [preauth]
  • @Daz 好的,所以这不是 ssh 问题(必须排除这一点)。现在用输出写一个潜在的答案

标签: ansible ansible-2.x ansible-template


【解决方案1】:

由于 ssh 在 ansible 控制器和远程机器之间工作,这可能是由于 ssh 由于身份验证方式失败而拒绝登录。您可能还没有为无密码登录交换 ssh 密钥,并且您的命令也没有提供密码。

要在 ad-hoc 命令中提供密码,例如您尝试过的命令,请在末尾传递 --ask-pass 标志:

[ilias@optima-ansible ~]$ ansible greenhat -m ping --ask-pass
SSH password: 
greenhat | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
[ilias@optima-ansible ~]$

没有标志:

[ilias@optima-ansible ~]$ ansible greenhat -m ping
greenhat | UNREACHABLE! => {
    "changed": false, 
    "msg": "Failed to connect to the host via ssh: ilias@greenhat: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n", 
    "unreachable": true
}
[ilias@optima-ansible ~]$ 

希望对你有帮助

【讨论】:

  • PS:如果您要管理多个主机,并且其中一些主机的密码不同,那么上述方法不适合您。我建议交换 ssh 密钥。也许this 对你有用。
  • 是的,我有多个登录。你能指导我吗?
  • 我已经移除了一台主机来进行初始化测试。但它最终得到了类似下面的东西。 root@ubuntu:/home/dasitha# ansible greenhat -m ping --ask-pass SSH password: [WARNING]: Could not match supplied host pattern, ignoring: greenhat [WARNING]: No hosts matched, nothing to do
  • greenhat 是我环境中的主机名,您应该使用 all 或您环境中的任何单个主机 :)
  • 操作!我的错。这工作正常。谢谢 :) 你能指导我如何在服务器之间交换密钥吗?
【解决方案2】:

Ansible 假设您使用 SSH 的无密码(基于密钥)登录。如果您坚持使用密码,请将 --ask-pass ( -k ) 标志添加到 Ansible 命令中(您可能还需要安装 sshpass 包才能使其工作)。

$ sudo apt-get install sshpass
ansible server-group -m ping -k -u username
SSH password:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    相关资源
    最近更新 更多