More verbose error message when ansible-playbook fails

I've spent 1 hour today debugging why packer does not want to work with
ansible. It turns out `ansible-playbook` command was returning non-zero
exit status because of the file system permission problem.

Output before change:

    % packer build rabbitmq.json
    amazon-ebs output will be in this color.

    1 error(s) occurred:

    * exit status 1

Output after change:

    amazon-ebs output will be in this color.

    1 error(s) occurred:

    * Error running "ansible-playbook --version": exit status 1
This commit is contained in:
Andrey Chernih 2017-03-21 20:26:41 -07:00
parent fa0ef21b1b
commit 7548024720
3 changed files with 18 additions and 1 deletions

View File

@ -144,7 +144,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
func (p *Provisioner) getVersion() error {
out, err := exec.Command(p.config.Command, "--version").Output()
if err != nil {
return err
return fmt.Errorf(
"Error running \"%s --version\": %s", p.config.Command, err.Error())
}
versionRe := regexp.MustCompile(`\w (\d+\.\d+[.\d+]*)`)

View File

@ -8,6 +8,7 @@ import (
"io/ioutil"
"os"
"path"
"strings"
"testing"
"github.com/mitchellh/packer/packer"
@ -258,6 +259,18 @@ func TestAnsibleGetVersion(t *testing.T) {
}
}
func TestAnsibleGetVersionError(t *testing.T) {
var p Provisioner
p.config.Command = "./test-fixtures/exit1"
err := p.getVersion()
if err == nil {
t.Fatal("Should return error")
}
if !strings.Contains(err.Error(), "./test-fixtures/exit1 --version") {
t.Fatal("Error message should include command name")
}
}
func TestAnsibleLongMessages(t *testing.T) {
if os.Getenv("PACKER_ACC") == "" {
t.Skip("This test is only run with PACKER_ACC=1 and it requires Ansible to be installed")

View File

@ -0,0 +1,3 @@
#!/bin/sh
exit 1