2016-08-12 5 views
1

ansibleプレイブック内のタスクが失敗し、実行に失敗した場合、どのようにキャプチャできますか?ファブリックでエラーが発生する

私はansible-playbookコマンドをラップするためにfabricを使用しています。これは私が現在使っているものです。

command = local(
    "sudo ansible-playbook %s --extra-vars \"project=%s role=%s env=%s version=%s enterprise=%s\"" 
    " --private-key ../inventory/keys/%s" % 
    (playbook, project, role, environment, version, enterprise, enterprise), capture=True) 
if command: 
    if command.stderr: 
     print(cyan(command.stderr)) 
    else: 
     print(green(command.stdout)) 
     print(blue("\nConfigured %s [%s] in %s stack" % 
        (project, role, environment))) 
else: 
    print(red("\nError configuring %s cloudformation resource stack:::: %s" 
       % role, command.stderr)) 

わかりましたとおり、コマンドがあり、出力が取り込まれます。 この問題は、ansibleがホストなどに接続できない場合など、ansible-playbookコマンドが失敗した場合にのみ発生します。ただし、taskが失敗した場合は発生しません。それが起こると、私のコードはコマンドが実際に正常に完了したと考えています。以下の実施例; (これはcommand.stderrによって捕獲されていない)

TASK [example.cloudformation : Launch cloudformation template] **************** 
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TemplateSyntaxError: expected name or number 
fatal: [127.0.0.1]: FAILED! => {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""} 
    to retry, use: --limit @example_stack_launch.retry 

私は、これはansible callbacksに関係している推測しているが、それらを公開して使用する方法がわかりません。

答えて

0

可能な解決策の1つは、stdout_callback=jsonを設定することです。
この場合、すべての不可能なプレイブックの出力は、機械可読のjson形式であるため、エラーがないかどうかチェックできます。

関連する問題