Commit 8cdb6865 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

Merge pull request #1562 from bhcleek/ansible-local-inventory-best-practice

provisioners/ansible-local: always use an inventory file
parents ad3b9840 afa499be
...@@ -2,11 +2,13 @@ package ansiblelocal ...@@ -2,11 +2,13 @@ package ansiblelocal
import ( import (
"fmt" "fmt"
"github.com/mitchellh/packer/common" "io/ioutil"
"github.com/mitchellh/packer/packer"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/mitchellh/packer/common"
"github.com/mitchellh/packer/packer"
) )
const DefaultStagingDir = "/tmp/packer-provisioner-ansible-local" const DefaultStagingDir = "/tmp/packer-provisioner-ansible-local"
...@@ -188,14 +190,30 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { ...@@ -188,14 +190,30 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error {
return fmt.Errorf("Error uploading main playbook: %s", err) return fmt.Errorf("Error uploading main playbook: %s", err)
} }
if len(p.config.InventoryFile) > 0 { if len(p.config.InventoryFile) == 0 {
tf, err := ioutil.TempFile("", "packer-provisioner-ansible-local")
if err != nil {
return fmt.Errorf("Error preparing inventory file: %s", err)
}
defer os.Remove(tf.Name())
_, err = tf.Write([]byte("127.0.0.1"))
if err != nil {
tf.Close()
return fmt.Errorf("Error preparing inventory file: %s", err)
}
tf.Close()
p.config.InventoryFile = tf.Name()
defer func() {
p.config.InventoryFile = ""
}()
}
ui.Message("Uploading inventory file...") ui.Message("Uploading inventory file...")
src := p.config.InventoryFile src = p.config.InventoryFile
dst := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(src))) dst = filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(src)))
if err := p.uploadFile(ui, comm, dst, src); err != nil { if err := p.uploadFile(ui, comm, dst, src); err != nil {
return fmt.Errorf("Error uploading inventory file: %s", err) return fmt.Errorf("Error uploading inventory file: %s", err)
} }
}
if len(p.config.GroupVars) > 0 { if len(p.config.GroupVars) > 0 {
ui.Message("Uploading group_vars directory...") ui.Message("Uploading group_vars directory...")
...@@ -253,14 +271,7 @@ func (p *Provisioner) Cancel() { ...@@ -253,14 +271,7 @@ func (p *Provisioner) Cancel() {
func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator) error { func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator) error {
playbook := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.PlaybookFile))) playbook := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.PlaybookFile)))
inventory := filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.InventoryFile)))
// The inventory must be set to "127.0.0.1,". The comma is important
// as its the only way to override the ansible inventory when dealing
// with a single host.
inventory := "\"127.0.0.1,\""
if len(p.config.InventoryFile) > 0 {
inventory = filepath.ToSlash(filepath.Join(p.config.StagingDir, filepath.Base(p.config.InventoryFile)))
}
extraArgs := "" extraArgs := ""
if len(p.config.ExtraArguments) > 0 { if len(p.config.ExtraArguments) > 0 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment