首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过via服务器调用ansible-playbook

通过via服务器调用ansible-playbook
EN

Stack Overflow用户
提问于 2017-11-22 01:39:57
回答 1查看 351关注 0票数 0

我使用的是运行ubuntu16.04的虚拟机,并且已经在上面安装了ansible v2.4和apache2。我创建了一个cgi脚本'ansible_call.py‘,它使用子进程模块,特别是

代码语言:javascript
复制
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向the服务发出请求。

代码语言:javascript
复制
curl localhost/cgi-bin/ansible-call.py

但是,我得到了以下错误

代码语言:javascript
复制
<!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>

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

代码语言:javascript
复制
[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对我来说是个谜,有谁知道吗?

EN

回答 1

Stack Overflow用户

发布于 2017-11-22 02:27:11

请检查ansible "executable"以获取可能的状态代码:

以下是一些摘录:

代码语言:javascript
复制
    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表明您有失败的主机。您可能希望捕获流程异常:

代码语言:javascript
复制
    try:
        res = check_output(cmd)
    except CalledProcessError as e:
        print 'Error occurred: (Code {}) {}'.format(e.returncode, e.output)
        res = None
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47419339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档