Skip to content

Commit

Permalink
Implemented SSH User option. rics3n#5
Browse files Browse the repository at this point in the history
  • Loading branch information
uphy committed Apr 20, 2018
1 parent 622c8e3 commit a2c17db
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func main() {
Value: defaultPlaybook,
EnvVar: "PLUGIN_PLAYBOOK",
},
cli.StringFlag{
Name: "ssh-user",
Usage: "ssh user to access remote hosts",
EnvVar: "SSH_USER,PLUGIN_SSH_USER",
},
cli.StringFlag{
Name: "ssh-key",
Usage: "ssh key to access remote hosts",
Expand Down Expand Up @@ -74,6 +79,7 @@ func run(c *cli.Context) error {
Inventories: c.StringSlice("inventories"),
Playbook: c.String("playbook"),
SSHKey: c.String("ssh-key"),
SSHUser: c.String("ssh-user"),
},
}

Expand Down
13 changes: 7 additions & 6 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type (
Inventories []string
Playbook string
SSHKey string
SSHUser string
}

// Plugin defines the Ansible plugin parameters.
Expand Down Expand Up @@ -73,13 +74,13 @@ func (p Plugin) Exec() error {
}

func command(build Build, config Config, inventory string) *exec.Cmd {

args := []string{
commandEnvVars(build),
"-i",
filepath.Join(build.Path, config.InventoryPath, inventory),
filepath.Join(build.Path, config.Playbook),
var args []string
args = append(args, commandEnvVars(build))
if config.SSHUser != "" {
args = append(args, "-u", config.SSHUser)
}
args = append(args, "-i", filepath.Join(build.Path, config.InventoryPath, inventory))
args = append(args, filepath.Join(build.Path, config.Playbook))
return exec.Command(ansibleBin, args...)
}

Expand Down
37 changes: 37 additions & 0 deletions plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"strings"
"testing"
)

func TestCommand(t *testing.T) {
// without user
if command := strings.Join(command(Build{
Path: "path/to/playbookdir",
SHA: "123456789ABCDEF",
Tag: "TAGNAME",
}, Config{
InventoryPath: "inventory-path",
Inventories: []string{"inventory1", "inventory2"},
Playbook: "playbook.yml",
SSHKey: "",
}, "inventory").Args, " "); command != "/usr/bin/ansible-playbook -e ansible_ssh_private_key_file=/root/.ssh/id_rsa -e commit_sha=123456789ABCDEF -e commit_tag=TAGNAME -i path/to/playbookdir/inventory-path/inventory path/to/playbookdir/playbook.yml" {
t.Error("unexpected command: ", command)
}

// with user
if command := strings.Join(command(Build{
Path: "path/to/playbookdir",
SHA: "123456789ABCDEF",
Tag: "TAGNAME",
}, Config{
InventoryPath: "inventory-path",
Inventories: []string{"inventory1", "inventory2"},
Playbook: "playbook.yml",
RemoteUser: "user1",
SSHKey: "",
}, "inventory").Args, " "); command != "/usr/bin/ansible-playbook -e ansible_ssh_private_key_file=/root/.ssh/id_rsa -e commit_sha=123456789ABCDEF -e commit_tag=TAGNAME -u user1 -i path/to/playbookdir/inventory-path/inventory path/to/playbookdir/playbook.yml" {
t.Error("unexpected command: ", command)
}
}

0 comments on commit a2c17db

Please sign in to comment.