Ansible: add the support for specifying ansible_user

Closes #3273
This commit is contained in:
Rickard von Essen 2016-02-26 21:50:50 +01:00
parent 96ac0d0e90
commit 7369841e63
3 changed files with 20 additions and 3 deletions

View File

@ -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)

View File

@ -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}" \

View File

@ -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.