在受管节点上强制执行非OS Python解释器时,Playbook卡住了

时间:2019-04-05 11:42:54

标签: python ansible

我正在尝试自动为托管RHEL 5.6的应用程序之一进行部署。操作系统默认随附Python 2.4.3。 我的Ansible控制节点在具有Python 2.7.5的RHEL 7.2上与2.7.5一起运行。

我在受管节点上安装了Python 2.7.5的另一个位置,并强迫Ansible使用新的Python,但现在它卡住了,没有产生任何输出。

当我尝试运行剧本时,它失败并显示以下错误:

fatal: [<hostname>]: FAILED! => {
    "msg": "Failed to get information on remote file (/secure/ShellScript/Params/SFT_Folder.txt): Shared connection to <IP Address> closed.\r\n"

详细输出提示使用/ usr / bin / python

的问题

我在受管节点上的其他位置安装了Python 2.7.5,并在主机文件中添加了ansible_python_interpreter = / opt / python2.7 / bin / python2.7

这将详细错误更改为共享库,同时使最终错误消息保持不变:

<IP Address> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=batchusr -o ConnectTimeout=10 -o ControlPath=/home/ansible/.ansible/cp/ce68d6977c -tt <IP Address> '/bin/sh -c '"'"'/opt/python2.7/bin/python2.7 /home/batchusr/.ansible/tmp/ansible-tmp-1554464098.34-28789780698815/AnsiballZ_stat.py && sleep 0'"'"''
<10.30.18.6> (127, '/opt/python2.7/bin/python2.7: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory\r\n', 'Shared connection to <IP Address> closed.\r\n')
fatal: [ibmsitlsft02]: FAILED! => {
    "msg": "Failed to get information on remote file (/secure/ShellScript/Params/SFT_Folder.txt): Shared connection to <IP Address> closed.\r\n"

因此,我创建了一个包含以下内容的可执行python文件,更改了解释器的位置,然后又赋予了它

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/python2.7/lib
/opt/python2.7/bin/python2.7

现在,它只是停留在以下步骤中,不再产生任何输出或日志。

<IP Address> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=batchusr -o ConnectTimeout=10 -o ControlPath=/home/ansible/.ansible/cp/ce68d6977c -tt <IP Address> '/bin/sh -c '"'"'/opt/python2.7/python /home/batchusr/.ansible/tmp/ansible-tmp-1554464252.78-244901400191906/AnsiballZ_stat.py && sleep 0'"'"''

ansible-playbook -i inventories/hosts sft_deploy_main.yml -vvv

预期输出:

PLAY RECAP **************************************************************************************************************************************************
<hostname>                 : ok=1    changed=0    unreachable=0    failed=0
localhost                  : ok=1    changed=0    unreachable=0    failed=0

实际输出: 不前进

3 个答案:

答案 0 :(得分:1)

最近的问题是由您编写的包装程序引起的。

您尝试用以下命令替换python可执行文件:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/python2.7/lib
/opt/python2.7/bin/python2.7

这有两个问题:

  1. 您真的应该从#!/bin/sh开始,而不要依靠后备行为。
  2. 您应该使用exec /opt/python2.7/bin/python2.7而不是让shell进程挂住,并且
  3. 最重要的是,您丢弃了python命令的所有参数。您需要更改:

    /opt/python2.7/bin/python2.7
    

    收件人:

    /opt/python2.7/bin/python2.7 "$@"
    

在编写包装程序时,运行时:

/path/to/wrapper myfile.py

实际上,您将启动一个交互式Python解释器,而不是运行myfile.py,这就是您的剧本运行停止的原因。

退一步,比使用包装器更好的解决方案是适当地配置/etc/ld.so.conf,以便/opt/python2.7/bin/python2.7可以运行而无需设置LD_LIBRARY_PATH

答案 1 :(得分:1)

我认为问题在于您的可执行文件不接受传递给它的参数。如果您尝试将可执行文件替换为:

#!/bin/bash
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/python2.7/lib
/opt/python2.7/bin/python2.7 "$@"

还要确保在该文件上设置了执行位。

答案 2 :(得分:0)

根据Larsks和MassPikeMike的建议,我现在将受管节点上的python文件修改为以下内容,并且现在可以正常使用:

# cat /opt/python2.7/python
#!/bin/sh
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/python2.7/lib
exec /opt/python2.7/bin/python2.7 "$@"