diff --git a/provisioner/ansible-local/provisioner.go b/provisioner/ansible-local/provisioner.go index 098f67ef1..bd4559388 100644 --- a/provisioner/ansible-local/provisioner.go +++ b/provisioner/ansible-local/provisioner.go @@ -114,8 +114,6 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { err = validateFileConfig(p.config.PlaybookFile, "playbook_file", true) if err != nil { errs = packer.MultiErrorAppend(errs, err) - } else { - p.playbookFiles = append(p.playbookFiles, p.config.PlaybookFile) } } @@ -202,7 +200,14 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { } } - if err := p.provisionPlaybookFiles(ui, comm); err != nil { + if p.config.PlaybookFile != "" { + ui.Message("Uploading main Playbook file...") + src := p.config.PlaybookFile + dst := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(src))) + if err := p.uploadFile(ui, comm, dst, src); err != nil { + return fmt.Errorf("Error uploading main playbook: %s", err) + } + } else if err := p.provisionPlaybookFiles(ui, comm); err != nil { return err } @@ -384,6 +389,13 @@ func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator) err } } + if p.config.PlaybookFile != "" { + playbookFile := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.PlaybookFile))) + if err := p.executeAnsiblePlaybook(ui, comm, playbookFile, extraArgs, inventory); err != nil { + return err + } + } + for _, playbookFile := range p.playbookFiles { playbookFile = filepath.ToSlash(filepath.Join(p.config.StagingDir, playbookFile)) if err := p.executeAnsiblePlaybook(ui, comm, playbookFile, extraArgs, inventory); err != nil { diff --git a/provisioner/ansible-local/provisioner_test.go b/provisioner/ansible-local/provisioner_test.go index 4370b060c..2f6174f3e 100644 --- a/provisioner/ansible-local/provisioner_test.go +++ b/provisioner/ansible-local/provisioner_test.go @@ -118,58 +118,6 @@ func TestProvisionerPrepare_PlaybookFiles(t *testing.T) { } } -func TestProvisionerProvision_PlaybookFile(t *testing.T) { - var p Provisioner - config := testConfig() - - playbook := createTempFile("") - defer os.Remove(playbook) - - config["playbook_file"] = playbook - err := p.Prepare(config) - if err != nil { - t.Fatalf("err: %s", err) - } - - comm := &communicatorMock{} - if err := p.Provision(&uiStub{}, comm); err != nil { - t.Fatalf("err: %s", err) - } - - assertPlaybooksUploaded(comm, []string{playbook}) - assertPlaybooksExecuted(comm, []string{playbook}) -} - -func TestProvisionerProvision_PlaybookFileWithPlaybookDir(t *testing.T) { - var p Provisioner - config := testConfig() - - playbook_dir, err := ioutil.TempDir("", "") - if err != nil { - t.Fatalf("Failed to create playbook_dir: %s", err) - } - defer os.RemoveAll(playbook_dir) - playbook := createTempFile(playbook_dir) - - playbookName := filepath.Base(playbook) - playbookInPlaybookDir := strings.TrimPrefix(playbook, playbook_dir) - - config["playbook_file"] = playbook - config["playbook_dir"] = playbook_dir - err = p.Prepare(config) - if err != nil { - t.Fatalf("err: %s", err) - } - - comm := &communicatorMock{} - if err := p.Provision(&uiStub{}, comm); err != nil { - t.Fatalf("err: %s", err) - } - - assertPlaybooksNotUploaded(comm, []string{playbookName}) - assertPlaybooksExecuted(comm, []string{playbookInPlaybookDir}) -} - func TestProvisionerProvision_PlaybookFiles(t *testing.T) { var p Provisioner config := testConfig() @@ -413,6 +361,7 @@ func testProvisionerProvisionDockerWithPlaybookFiles(t *testing.T, templateStrin if err != nil { t.Fatalf("Error preparing download: %s", err) } + defer os.Remove("hello_world") // Add hooks so the provisioners run during the build hooks := map[string][]packer.Hook{} @@ -431,7 +380,6 @@ func testProvisionerProvisionDockerWithPlaybookFiles(t *testing.T, templateStrin t.Fatalf("Error running build %s", err) } defer artifact.Destroy() - defer os.Remove("hello_world") actualContent, err := ioutil.ReadFile("hello_world") if err != nil { @@ -461,6 +409,7 @@ func assertPlaybooksExecuted(comm *communicatorMock, playbooks []string) { } func assertPlaybooksUploaded(comm *communicatorMock, playbooks []string) { + fmt.Println(comm.uploadDestination) uploadIndex := 0 for _, playbook := range playbooks { playbook = filepath.ToSlash(playbook)