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

Why I could not Ansible ping my testerver?

我正在尝试 运行 Ansible Up 和 运行 书中的示例。

我的剧本目录

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

我得到了

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 不起作用?

来自错误信息

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

似乎远程主机没有安装 python。

引用自requiremet docs

On the managed nodes, you need a way to communicate, which is normally ssh. By default this uses sftp. If that’s not available, you can switch to scp in ansible.cfg. You also need Python 2 (version 2.6 or later) or Python 3 (version 3.5 or later).

Ansible 需要 python 才能出现在远程主机中。

另外,关于ping模块的使用,it's not the same as ping shell command.

尝试在远程主机中安装 python(手动或使用 raw 模块)然后 re-run 脚本。

这只是因为您在终端中混淆了 ansible ping 模块和经典的 ICMP ping 命令,它们并不等效。从上面link

This is NOT ICMP ping, this is just a trivial test module that requires Python on the remote-node.

由于上述混淆,您是 miss-interpreting 您在 运行 剧本时收到的明确错误消息:

第一个

Shared connection to 127.0.0.1 closed

...这意味着连接已首次打开并且您的主机可以访问

第二

/bin/sh: 1: /usr/bin/python: not found

... 这意味着 python(ansible 需要)未安装或不在默认路径中。

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

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

因为python安装在本地主机上,而python没有安装在远程主机上,只是在远程主机上安装了python。发现问题解决了!!!