通过网络服务器调用ansible-playbook

时间:2017-11-21 17:39:57

标签: python apache ansible

我正在使用运行ubuntu 16.04的VM,并在其上安装了ansible v2.4和apache2。我创建了一个使用子进程模块的cgi脚本'ansible_call.py',特别是

import os
import subprocess


print 'Content-type:text/html /r/n/r/n'

with open(os.devnull, 'w') as devnull:
    result = subprocess.check_output(['/usr/bin/ansible-
playbook','/vagrant/playbooks/sample.yml'], stderr=devnull)

print result

然后我使用curl向webservice发出请求

curl localhost/cgi-bin/ansible-call.py

但是我收到以下错误

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at 
webmaster@localhost to inform them of the time this error occurred,
and the actions you performed just before this error.</p>  
<p>More information about this error may be available
in the server error log.</p>
<hr>
<address>Apache/2.4.18 (Ubuntu) Server at localhost Port 80</address>
</body></html>

我查看了日志,发现了以下内容

[Tue Nov 21 17:34:50.192002 2017] [cgi:error] [pid 3142:tid 140333400639232] [client 127.0.0.1:37534] AH01215: subprocess.CalledProcessError: Command '['/usr/bin/ansible-playbook', '/vagrant/playbooks/sample.yml']' returned non-zero exit status 2: /usr/lib/cgi-bin/ansible-call.py

python脚本本身运行正常。退出状态2对我来说是一个谜,有没有人有任何想法?

1 个答案:

答案 0 :(得分:0)

请检查ansible "executable"是否有可能的状态代码:

一些摘录:

    except AnsibleOptionsError as e:
        ...
        exit_code = 5
    except AnsibleParserError as e:
        ...
        exit_code = 4
# TQM takes care of these, but leaving comment to reserve the exit codes
#    except AnsibleHostUnreachable as e:
#        ...
#        exit_code = 3
#    except AnsibleHostFailed as e:
#        ...
#        exit_code = 2
    except AnsibleError as e:
        ...
        exit_code = 1
    except KeyboardInterrupt:
        ...
        exit_code = 99
    except Exception as e:
        ...
        exit_code = 250

因此退出代码2表明您的主机出现故障。您可能希望捕获进程异常:

    try:
        res = check_output(cmd)
    except CalledProcessError as e:
        print 'Error occurred: (Code {}) {}'.format(e.returncode, e.output)
        res = None