From 75480247207eb17de78c65dcbd6ad74072d533f8 Mon Sep 17 00:00:00 2001 From: Andrey Chernih Date: Tue, 21 Mar 2017 20:26:41 -0700 Subject: [PATCH] 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 --- provisioner/ansible/provisioner.go | 3 ++- provisioner/ansible/provisioner_test.go | 13 +++++++++++++ provisioner/ansible/test-fixtures/exit1 | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100755 provisioner/ansible/test-fixtures/exit1 diff --git a/provisioner/ansible/provisioner.go b/provisioner/ansible/provisioner.go index b57edd829..642d99bd8 100644 --- a/provisioner/ansible/provisioner.go +++ b/provisioner/ansible/provisioner.go @@ -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+]*)`) diff --git a/provisioner/ansible/provisioner_test.go b/provisioner/ansible/provisioner_test.go index 12a0f4e9b..13042c5e1 100644 --- a/provisioner/ansible/provisioner_test.go +++ b/provisioner/ansible/provisioner_test.go @@ -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") diff --git a/provisioner/ansible/test-fixtures/exit1 b/provisioner/ansible/test-fixtures/exit1 new file mode 100755 index 000000000..2bb8d868b --- /dev/null +++ b/provisioner/ansible/test-fixtures/exit1 @@ -0,0 +1,3 @@ +#!/bin/sh + +exit 1