diff --git a/provisioner/ansible/provisioner.go b/provisioner/ansible/provisioner.go index b2bb84db3..22546f672 100644 --- a/provisioner/ansible/provisioner.go +++ b/provisioner/ansible/provisioner.go @@ -15,6 +15,7 @@ import ( "net" "os" "os/exec" + "os/user" "path/filepath" "regexp" "strconv" @@ -46,6 +47,7 @@ type Config struct { Groups []string `mapstructure:"groups"` EmptyGroups []string `mapstructure:"empty_groups"` HostAlias string `mapstructure:"host_alias"` + User string `mapstructure:"user"` LocalPort string `mapstructure:"local_port"` SSHHostKeyFile string `mapstructure:"ssh_host_key_file"` SSHAuthorizedKeyFile string `mapstructure:"ssh_authorized_key_file"` @@ -119,6 +121,15 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { errs = packer.MultiErrorAppend(errs, err) } + if p.config.User == "" { + u, err := user.Current() + if err != nil { + errs = packer.MultiErrorAppend(errs, err) + } else { + p.config.User = u.Username + } + } + if errs != nil && len(errs.Errors) > 0 { return errs } @@ -167,7 +178,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { keyChecker := ssh.CertChecker{ UserKeyFallback: func(conn ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) { - if user := conn.User(); user != "packer-ansible" { + if user := conn.User(); user != p.config.User { ui.Say(fmt.Sprintf("%s is not a valid user", user)) return nil, errors.New("authentication failed") } @@ -240,9 +251,11 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { } defer os.Remove(tf.Name()) - host := fmt.Sprintf("%s ansible_host=127.0.0.1 ansible_user=packer-ansible ansible_port=%s\n", p.config.HostAlias, p.config.LocalPort) + host := fmt.Sprintf("%s ansible_host=127.0.0.1 ansible_user=%s ansible_port=%s\n", + p.config.HostAlias, p.config.User, p.config.LocalPort) if p.ansibleMajVersion < 2 { - host = fmt.Sprintf("%s ansible_ssh_host=127.0.0.1 ansible_ssh_user=packer-ansible ansible_ssh_port=%s\n", p.config.HostAlias, p.config.LocalPort) + host = fmt.Sprintf("%s ansible_ssh_host=127.0.0.1 ansible_ssh_user=%s ansible_ssh_port=%s\n", + p.config.HostAlias, p.config.User, p.config.LocalPort) } w := bufio.NewWriter(tf) diff --git a/scripts/build.sh b/scripts/build.sh index 343ea04cf..415c64732 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -35,6 +35,7 @@ mkdir -p bin/ echo "==> Building..." set +e gox \ + -cgo \ -os="${XC_OS}" \ -arch="${XC_ARCH}" \ -ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \ diff --git a/website/source/docs/provisioners/ansible.html.markdown b/website/source/docs/provisioners/ansible.html.markdown index 19dd6a761..bb0b26cea 100644 --- a/website/source/docs/provisioners/ansible.html.markdown +++ b/website/source/docs/provisioners/ansible.html.markdown @@ -90,6 +90,9 @@ Optional Parameters: "ansible_env_vars": [ "ANSIBLE_HOST_KEY_CHECKING=False", "ANSIBLE_SSH_ARGS='-o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s'", "ANSIBLE_NOCOLOR=True" ] ``` +- `user` (string) - The `ansible_user` to use. Defaults to the user running + packer. + ## Limitations The `ansible` provisioner does not support SCP to transfer files.