-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use AnsiblePlaybookErrorEnrich to provide extra details on ansible-pl…
…aybook errors
- Loading branch information
Showing
8 changed files
with
138 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package playbook | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"syscall" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
// TODO: error management | ||
// AnsiblePlaybookErrorCodeGeneralError is the error code for a general error | ||
AnsiblePlaybookErrorCodeGeneralError = 1 | ||
// AnsiblePlaybookErrorCodeOneOrMoreHostFailed is the error code for a one or more host failed | ||
AnsiblePlaybookErrorCodeOneOrMoreHostFailed = 2 | ||
// AnsiblePlaybookErrorCodeOneOrMoreHostUnreachable is the error code for a one or more host unreachable | ||
AnsiblePlaybookErrorCodeOneOrMoreHostUnreachable = 3 | ||
// AnsiblePlaybookErrorCodeParserError is the error code for a parser error | ||
AnsiblePlaybookErrorCodeParserError = 4 | ||
// AnsiblePlaybookErrorCodeBadOrIncompleteOptions is the error code for a bad or incomplete options | ||
AnsiblePlaybookErrorCodeBadOrIncompleteOptions = 5 | ||
// AnsiblePlaybookErrorCodeUserInterruptedExecution is the error code for a user interrupted execution | ||
AnsiblePlaybookErrorCodeUserInterruptedExecution = 99 | ||
// AnsiblePlaybookErrorCodeUnexpectedError is the error code for a unexpected error | ||
AnsiblePlaybookErrorCodeUnexpectedError = 250 | ||
|
||
// AnsiblePlaybookErrorMessageGeneralError is the error message for a general error | ||
AnsiblePlaybookErrorMessageGeneralError = "ansible-playbook error: general error" | ||
// AnsiblePlaybookErrorMessageOneOrMoreHostFailed is the error message for a one or more host failed | ||
AnsiblePlaybookErrorMessageOneOrMoreHostFailed = "ansible-playbook error: one or more host failed" | ||
// AnsiblePlaybookErrorMessageOneOrMoreHostUnreachable is the error message for a one or more host unreachable | ||
AnsiblePlaybookErrorMessageOneOrMoreHostUnreachable = "ansible-playbook error: one or more host unreachable" | ||
// AnsiblePlaybookErrorMessageParserError is the error message for a parser error | ||
AnsiblePlaybookErrorMessageParserError = "ansible-playbook error: parser error" | ||
// AnsiblePlaybookErrorMessageBadOrIncompleteOptions is the error message for a bad or incomplete options | ||
AnsiblePlaybookErrorMessageBadOrIncompleteOptions = "ansible-playbook error: bad or incomplete options" | ||
// AnsiblePlaybookErrorMessageUserInterruptedExecution is the error message for a user interrupted execution | ||
AnsiblePlaybookErrorMessageUserInterruptedExecution = "ansible-playbook error: user interrupted execution" | ||
// AnsiblePlaybookErrorMessageUnexpectedError is the error message for a unexpected error | ||
AnsiblePlaybookErrorMessageUnexpectedError = "ansible-playbook error: unexpected error" | ||
) | ||
|
||
type AnsiblePlaybookErrorEnrich struct{} | ||
|
||
// NewAnsiblePlaybookErrorEnrich creates a new AnsiblePlaybookErrorEnrich instance | ||
func NewAnsiblePlaybookErrorEnrich() *AnsiblePlaybookErrorEnrich { | ||
return &AnsiblePlaybookErrorEnrich{} | ||
} | ||
|
||
func (e *AnsiblePlaybookErrorEnrich) Enrich(err error) error { | ||
|
||
errorMessage := "" | ||
|
||
exitError, exists := err.(*exec.ExitError) | ||
|
||
if exists { | ||
ws := exitError.Sys().(syscall.WaitStatus) | ||
switch ws.ExitStatus() { | ||
case AnsiblePlaybookErrorCodeGeneralError: | ||
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageGeneralError, errorMessage) | ||
case AnsiblePlaybookErrorCodeOneOrMoreHostFailed: | ||
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageOneOrMoreHostFailed, errorMessage) | ||
case AnsiblePlaybookErrorCodeOneOrMoreHostUnreachable: | ||
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageOneOrMoreHostUnreachable, errorMessage) | ||
case AnsiblePlaybookErrorCodeParserError: | ||
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageParserError, errorMessage) | ||
case AnsiblePlaybookErrorCodeBadOrIncompleteOptions: | ||
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageBadOrIncompleteOptions, errorMessage) | ||
case AnsiblePlaybookErrorCodeUserInterruptedExecution: | ||
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageUserInterruptedExecution, errorMessage) | ||
case AnsiblePlaybookErrorCodeUnexpectedError: | ||
errorMessage = fmt.Sprintf("%s\n\n%s", AnsiblePlaybookErrorMessageUnexpectedError, errorMessage) | ||
} | ||
} | ||
|
||
return errors.Wrap(err, errorMessage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package playbook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters