为什么我无法使用Ansible ping我的测试服务器?

3

我想运行《Ansible实战》一书中的示例。

我的playbooks目录如下:

ls
ansible.cfg  hosts  ubuntu-bionic-18.04-cloudimg-console.log  Vagrantfile

主机文件

testserver ansible_host=127.0.0.1 ansible_port=2222

ansible.cfg

[defaults]
inventory = hosts
remote_user = vagrant
private_key_file = .vagrant/machines/default/virtualbox/private_key
host_key_checking = False

Vagrantfile

   config.vm.box = "ubuntu/bionic64"

当我尝试使用ping命令时

ansible testserver -m ping

I got

testserver | FAILED! => {
    "changed": false, 
    "module_stderr": "Shared connection to 127.0.0.1 closed.\r\n", 
    "module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n", 
    "msg": "MODULE FAILURE", 
    "rc": 127
}

我可以毫无问题地使用ssh

ssh vagrant@127.0.0.1 -p 2222 -i /home/miki/playbooks/.vagrant/machines/default/virtualbox/private_key
Welcome to Ubuntu 18.04.2 LTS (GNU/Linux 4.15.0-50-generic x86_64)


  System information as of Tue May 21 06:39:46 UTC 2019

  System load:  0.0               Processes:             108
  Usage of /:   18.5% of 9.63GB   Users logged in:       0
  Memory usage: 17%               IP address for enp0s3: 10.0.2.15
  Swap usage:   0%





Last login: Tue May 21 06:32:13 2019 from 10.0.2.2

为什么Ansible ping不起作用?

尝试将以下内容添加到hosts文件中 ansible_python_interpreter=/usr/bin/python3 - Kavyesh Shah
3个回答

4

从错误消息中

"module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n", 

看起来远程主机没有安装Python。
引用自需求文档
“在被管理的节点上,你需要一种通信方式,通常使用ssh。默认情况下使用sftp。如果不可用,可以在ansible.cfg中切换到scp。您还需要Python 2(版本2.6或更高)或Python 3(版本3.5或更高)。”
Ansible需要在远程主机中存在Python。
此外,关于模块的用法,它与ping shell命令不同。 尝试在远程主机上安装Python(手动或使用raw模块),然后重新运行脚本。

3

我也遇到了相同的错误。然后发现了这个:

"module_stdout": "/bin/sh: 1: /usr/bin/python: not found\r\n",

由于本地主机上已经安装了Python,而远程主机上没有安装Python,所以只需在远程主机上安装Python即可。然后发现问题已经解决了!!!


2
这是因为你混淆了Ansible的ping模块和终端中的经典ICMP ping命令,它们并不相等。来自上面链接的内容:

这不是ICMP ping,这只是一个需要远程节点上安装Python的微不足道的测试模块。

由于上述混淆,你误解了运行playbook时收到的明显错误消息:
第一条:

共享连接127.0.0.1已关闭

...这意味着首先打开了一个连接,你的主机是可达的。
第二条:

/bin/sh: 1: /usr/bin/python: 找不到文件或目录

...这意味着未安装或未在默认路径中找到Ansible所需的Python。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接