Commit ec1adec0 authored by jamie brim's avatar jamie brim Committed by Jamie Brim

provisioner/ansible: configurable command / args

allow users to provide a replacement command / extra args to the
ansible-local provisioner.

this is useful, for example, to unbuffer output when using the
docker builder, or to modify ansible behavior, like enabling
verbose mode, injecting variables, or changing paths.
parent 1a57e389
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"github.com/mitchellh/packer/packer" "github.com/mitchellh/packer/packer"
"os" "os"
"path/filepath" "path/filepath"
"strings"
) )
const DefaultStagingDir = "/tmp/packer-provisioner-ansible-local" const DefaultStagingDir = "/tmp/packer-provisioner-ansible-local"
...@@ -26,6 +27,12 @@ type Config struct { ...@@ -26,6 +27,12 @@ type Config struct {
// The directory where files will be uploaded. Packer requires write // The directory where files will be uploaded. Packer requires write
// permissions in this directory. // permissions in this directory.
StagingDir string `mapstructure:"staging_directory"` StagingDir string `mapstructure:"staging_directory"`
// The command to run ansible
Command string
// Extra options to pass to the ansible command
ExtraArguments []string `mapstructure:"extra_arguments"`
} }
type Provisioner struct { type Provisioner struct {
...@@ -48,14 +55,20 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { ...@@ -48,14 +55,20 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
// Accumulate any errors // Accumulate any errors
errs := common.CheckUnusedConfig(md) errs := common.CheckUnusedConfig(md)
// Defaults
if p.config.StagingDir == "" { if p.config.StagingDir == "" {
p.config.StagingDir = DefaultStagingDir p.config.StagingDir = DefaultStagingDir
} }
if p.config.Command == "" {
p.config.Command = "ansible-playbook"
}
// Templates // Templates
templates := map[string]*string{ templates := map[string]*string{
"playbook_file": &p.config.PlaybookFile, "playbook_file": &p.config.PlaybookFile,
"staging_dir": &p.config.StagingDir, "staging_dir": &p.config.StagingDir,
"command": &p.config.Command,
} }
for n, ptr := range templates { for n, ptr := range templates {
...@@ -70,6 +83,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { ...@@ -70,6 +83,7 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
sliceTemplates := map[string][]string{ sliceTemplates := map[string][]string{
"playbook_paths": p.config.PlaybookPaths, "playbook_paths": p.config.PlaybookPaths,
"role_paths": p.config.RolePaths, "role_paths": p.config.RolePaths,
"extra_arguments": p.config.ExtraArguments,
} }
for n, slice := range sliceTemplates { for n, slice := range sliceTemplates {
...@@ -161,7 +175,13 @@ func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator) err ...@@ -161,7 +175,13 @@ func (p *Provisioner) executeAnsible(ui packer.Ui, comm packer.Communicator) err
// The inventory must be set to "127.0.0.1,". The comma is important // 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 // as its the only way to override the ansible inventory when dealing
// with a single host. // with a single host.
command := fmt.Sprintf("ansible-playbook %s -c local -i %s", playbook, `"127.0.0.1,"`) var command string
if len(p.config.ExtraArguments) > 0 {
command = fmt.Sprintf("%s %s %s -c local -i \"127.0.0.1,\"", p.config.Command,
playbook, strings.Join(p.config.ExtraArguments, " "))
} else {
command = fmt.Sprintf("%s %s -c local -i \"127.0.0.1,\"", p.config.Command, playbook)
}
ui.Message(fmt.Sprintf("Executing Ansible: %s", command)) ui.Message(fmt.Sprintf("Executing Ansible: %s", command))
cmd := &packer.RemoteCmd{ cmd := &packer.RemoteCmd{
......
...@@ -34,6 +34,11 @@ Required: ...@@ -34,6 +34,11 @@ Required:
Optional: Optional:
* `command` (string) - The command to invoke ansible. Defaults to "ansible-playbook".
* `extra_arguments` (array of strings) - An array of extra arguments to pass to the
ansible command. By default, this is empty.
* `playbook_paths` (array of strings) - An array of paths to playbook files on * `playbook_paths` (array of strings) - An array of paths to playbook files on
your local system. These will be uploaded to the remote machine under your local system. These will be uploaded to the remote machine under
`staging_directory`/playbooks. By default, this is empty. `staging_directory`/playbooks. By default, this is empty.
......
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