【问题标题】:Ansible Module Failure – Unknown CauseAnsible 模块故障 - 未知原因
【发布时间】:2017-08-16 00:42:39
【问题描述】:

概要

我在尝试同时运行标准 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


原帖

我见过很多情况,人们似乎有我的确切问题,但最终总是略有不同。无论如何,我尝试解决方案无济于事。

我在我认为是 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

我正在与之通信的主机正在运行 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。请参阅他的帖子,了解为什么这是一个问题。


一些附加信息

在安装了 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 行进行自己的评估。

【问题讨论】:

    标签: pip ansible yum rhel6


    【解决方案1】:

    您在 root 帐户下运行 ansiblesudo 并尝试使用 root 帐户中的 ssh-keys(不存在?)。
    当您尝试ssh 命令时,您将在您当前的用户帐户下运行它并使用另一个访问密钥。

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

    【讨论】:

    • 感谢您的回复。当我在没有 sudo 的情况下运行它时它也不起作用。当我早上回到办公室时,我将发布一个不带 sudo 的命令结果的更新。
    • 这种情况下的错误与Permission Denied完全不同。您应该尝试禁用ControlMaster:将[ssh_connection] 部分添加到ansible.cfg 并在其中添加空ssh_args =
    • 查看更新 2 了解您的推荐结果和一些其他信息
    • 要保留远程文件,您可以使用 ANSIBLE_KEEP_REMOTE_FILES=1 env set 调用 ansible。但这肯定是另一个问题......我认为我对 cme​​ts 的回答应该被接受。
    • 请见谅,但你的问题的标题是Permission Denied When Running Ansible,我已经回答了你为什么会出现这个错误——不要使用sudo。然后给你建议如何克服没有sudossh_connect: needpriv 错误 - 不要使用 SSH 多路复用。现在你有新的问题,我猜,过时的 Python 和未发布的 Ansible 版本。这毕竟不是持续的故障排除站点,而是问答站点。
    猜你喜欢
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 2018-11-29
    • 1970-01-01
    • 2021-04-14
    • 2012-02-15
    相关资源
    最近更新 更多