Ansible 模块故障 – 未知原因

Ansible Module Failure – Unknown Cause

概要

我在尝试 运行 标准 ansible all -m pingansible all -a "pwd" 时收到以下错误(第二个命令只是为了防止 ping 模块出现问题):

..."module_stdout": "  File \"/tmp/ansible_C2IrV6/ansible_module_command.py\", line 183\r\n    out = b''\r\n            ^\r\nSyntaxError: invalid syntax\r\n",...

我的问题是,由于通过 pip 安装,我以某种方式 运行 安装了一个未发布的 Ansible 版本 (2.4.0)。这与我的 yum 安装 (2.3.1.0) 冲突,并且与我当前的 Python 版本 (2.6.6) 不兼容。

我的解决方案是卸载这两个版本以确保我的系统上不再有 ansible。从那里,我使用 yum 将 ansible 重新安装到我知道兼容的版本 (2.3.1.0)。我还读到可以使用 pip 来指定版本:

pip install ansible==<version>

安装不同版本有更多细节here


原创POST

我见过很多情况,人们似乎遇到了我的问题,但结果总是略有不同。无论如何,我尝试了这些解决方案都无济于事。

我正在 运行在我认为是 RHEL6 的平台上安装 Ansible 2.4.0:

$ uname -a
Linux <server address> 2.6.32-642.11.1.el6.x86_64 #1 SMP Wed Oct 26 10:25:23 EDT 2016 x86_64 x86_64 x86_64 GNU/Linux

我正在与之通信的主机是 运行ning RHEL5.

当我运行这个命令时:

$ sudo ansible all -a "pwd" -vvvv

我得到以下结果:

Verbose Ansible Output

从上面的输出中提取 ssh 命令:

$ ssh -vvv -C -o ControlMaster=auto -o ControlPersist=60s -o 'IdentityFile="/root/.ssh/id_rsa"' -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o 'ControlPath=~/.ansible/cp' user@hostDestination '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''

结果:

Extracted Ansible SSH Command Output

根据上面的结果,似乎该命令建立了成功的连接,所以我不明白为什么 Ansible 会给我 Permission Denied

编辑: 感谢@KonstantinSuvorov,我能够更准确地确定我的问题——我似乎将模块故障归因于命令的结果 Permission Denied 因为我使用 sudo 得到了最好的回应。请参阅他的 post 以了解为什么这是一个问题。


一些附加信息

在安装了Ansible的服务器上,我有超级用户权限。在目标主机上我没有。

通过 SSH 正常连接到目标主机工作得很好,反之亦然。

以下是我们在工作中成功登录到我们的任何服务器后看到的内容,所以如果不熟悉,请不要惊慌:

    ***************************************************************
    *                                                             *
    * Do not attempt to log on unless you are an authorized user. *
    *                                                             *
    * You must have a valid Network account.                      *
    *                                                             *
    ***************************************************************

如果有必要,我会在明天回到办公室时提供我的ansible.cfg

如果这属于 SuperUser、ServerFault 或其他地方,请原谅我。


更新

运行 没有 sudo 的命令:

ansible all -a "/bin/pwd" -vvvv

结果:

Verbose Ansible Command Without Sudo


更新 2

在我的 ansible.cfg 中将 ssh_args= 置于 [ssh_connection] 下之后:

ansible all -a "pwd"

结果:

hostDestination | FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "\n***************************************************************\n*                                                             *\n* Do not attempt to log on unless you are an authorized user. *\n*                                                             *\n* You must have a valid Network account.                      *\n*                                                             *\n***************************************************************\n\nConnection to hostDestination closed.\r\n",
    "module_stdout": "  File \"/tmp/ansible_C2IrV6/ansible_module_command.py\", line 183\r\n    out = b''\r\n            ^\r\nSyntaxError: invalid syntax\r\n",
    "msg": "MODULE FAILURE",
    "rc": 0
}

注意: 运行 使用 Ansible 的 raw 模块的命令成功:

ansible all -m raw -a "pwd"

结果:

hostDestination | SUCCESS | rc=0 >>
/home/user

***************************************************************
*                                                             *
* Do not attempt to log on unless you are an authorized user. *
*                                                             *
* You must have a valid Network account.                      *
*                                                             *
***************************************************************

Shared connection to hostDestination closed.

当我查看常规命令的详细输出 (-vvvv) 时,我看到正在使用以下模块:

Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/basic.py
Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/_text.py
Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/parsing/convert_bool.py
Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/parsing/__init__.py
Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/pycompat24.py
Using module_utils file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/module_utils/six/__init__.py
Using module file /usr/lib/python2.6/site-packages/ansible-2.4.0-py2.6.egg/ansible/modules/commands/command.py

我觉得一个或多个模块可能是问题所在,因为上面输出中的最后一行类似于失败命令输出中的以下行:

... /tmp/ansible_C2IrV6/ansible_module_command.py\ ...

看起来 Python 解释器对该文件中的空二进制字符串的语法有疑问。不幸的是,Ansible 在 运行 命令执行后立即删除了文件——阻止我查看第 183 行来进行自己的评估。

您 运行 ansible 在 root 帐户下使用 sudo 并且它尝试使用来自 root 帐户的 ssh 密钥(不存在?)。
当您尝试 ssh 命令时,您 运行 它在您当前的用户帐户下并使用另一个访问密钥。

我相信你没有理由在这里使用sudo