From 7369841e63218c3d6014db09e1023d31a05361d9 Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Fri, 26 Feb 2016 21:50:50 +0100 Subject: [PATCH 1/2] Ansible: add the support for specifying ansible_user Closes #3273 --- provisioner/ansible/provisioner.go | 19 ++++++++++++++++--- scripts/build.sh | 1 + .../docs/provisioners/ansible.html.markdown | 3 +++ 3 files changed, 20 insertions(+), 3 deletions(-) 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. From d59844f0d199aabe2178f54631deef96279792e1 Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Fri, 26 Feb 2016 22:16:22 +0100 Subject: [PATCH 2/2] Determine current user without CGO. --- provisioner/ansible/provisioner.go | 11 ++++------- scripts/build.sh | 1 - 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/provisioner/ansible/provisioner.go b/provisioner/ansible/provisioner.go index 22546f672..e38e4bc08 100644 --- a/provisioner/ansible/provisioner.go +++ b/provisioner/ansible/provisioner.go @@ -15,7 +15,6 @@ import ( "net" "os" "os/exec" - "os/user" "path/filepath" "regexp" "strconv" @@ -122,12 +121,10 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { } if p.config.User == "" { - u, err := user.Current() - if err != nil { - errs = packer.MultiErrorAppend(errs, err) - } else { - p.config.User = u.Username - } + p.config.User = os.Getenv("USER") + } + if p.config.User == "" { + errs = packer.MultiErrorAppend(errs, fmt.Errorf("user: could not determine current user from environment.")) } if errs != nil && len(errs.Errors) > 0 { diff --git a/scripts/build.sh b/scripts/build.sh index 415c64732..343ea04cf 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -35,7 +35,6 @@ mkdir -p bin/ echo "==> Building..." set +e gox \ - -cgo \ -os="${XC_OS}" \ -arch="${XC_ARCH}" \ -ldflags "-X main.GitCommit ${GIT_COMMIT}${GIT_DIRTY}" \