From d6716f6ee1403d5ca443c348730b6712056faeca Mon Sep 17 00:00:00 2001 From: Rickard von Essen Date: Tue, 1 Mar 2016 19:35:48 +0100 Subject: [PATCH] Ansible: don't use deprecated ssh options when ver above 2.0 Ansible 2.0 deprecated ansible_ssh_user, ansible_ssh_port, ansible_ssh_host instead use ansible_user, ansible_port, and ansible_host in the inventory file. Closes #3275 --- provisioner/ansible/provisioner.go | 45 ++++++++++++++++++++++--- provisioner/ansible/provisioner_test.go | 32 ++++++++++++++---- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/provisioner/ansible/provisioner.go b/provisioner/ansible/provisioner.go index 7fa93f997..b2bb84db3 100644 --- a/provisioner/ansible/provisioner.go +++ b/provisioner/ansible/provisioner.go @@ -16,6 +16,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "strconv" "strings" "sync" @@ -53,9 +54,11 @@ type Config struct { } type Provisioner struct { - config Config - adapter *adapter - done chan struct{} + config Config + adapter *adapter + done chan struct{} + ansibleVersion string + ansibleMajVersion uint } func (p *Provisioner) Prepare(raws ...interface{}) error { @@ -111,12 +114,43 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { p.config.LocalPort = "0" } + err = p.getVersion() + if err != nil { + errs = packer.MultiErrorAppend(errs, err) + } + if errs != nil && len(errs.Errors) > 0 { return errs } return nil } +func (p *Provisioner) getVersion() error { + out, err := exec.Command(p.config.Command, "--version").Output() + if err != nil { + return err + } + + versionRe := regexp.MustCompile(`\w (\d+\.\d+[.\d+]*)`) + matches := versionRe.FindStringSubmatch(string(out)) + if matches == nil { + return fmt.Errorf( + "Could not find %s version in output:\n%s", p.config.Command, string(out)) + } + + version := matches[1] + log.Printf("%s version: %s", p.config.Command, version) + p.ansibleVersion = version + + majVer, err := strconv.ParseUint(strings.Split(version, ".")[0], 10, 0) + if err != nil { + return fmt.Errorf("Could not parse major version from \"%s\".", version) + } + p.ansibleMajVersion = uint(majVer) + + return nil +} + func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { ui.Say("Provisioning with Ansible...") @@ -206,7 +240,10 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { } defer os.Remove(tf.Name()) - 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_host=127.0.0.1 ansible_user=packer-ansible ansible_port=%s\n", p.config.HostAlias, 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) + } w := bufio.NewWriter(tf) w.WriteString(host) diff --git a/provisioner/ansible/provisioner_test.go b/provisioner/ansible/provisioner_test.go index 6fac4b8af..1abbdf1a3 100644 --- a/provisioner/ansible/provisioner_test.go +++ b/provisioner/ansible/provisioner_test.go @@ -6,13 +6,28 @@ import ( "io" "io/ioutil" "os" + "path" "testing" "github.com/mitchellh/packer/packer" ) -func testConfig() map[string]interface{} { +// Be sure to remove the Ansible stub file in each test with: +// defer os.Remove(config["command"].(string)) +func testConfig(t *testing.T) map[string]interface{} { m := make(map[string]interface{}) + wd, err := os.Getwd() + if err != nil { + t.Fatalf("err: %s", err) + } + ansible_stub := path.Join(wd, "packer-ansible-stub.sh") + + err = ioutil.WriteFile(ansible_stub, []byte("#!/usr/bin/env bash\necho ansible 1.6.0"), 0777) + if err != nil { + t.Fatalf("err: %s", err) + } + m["command"] = ansible_stub + return m } @@ -26,7 +41,8 @@ func TestProvisioner_Impl(t *testing.T) { func TestProvisionerPrepare_Defaults(t *testing.T) { var p Provisioner - config := testConfig() + config := testConfig(t) + defer os.Remove(config["command"].(string)) err := p.Prepare(config) if err == nil { @@ -62,7 +78,8 @@ func TestProvisionerPrepare_Defaults(t *testing.T) { func TestProvisionerPrepare_PlaybookFile(t *testing.T) { var p Provisioner - config := testConfig() + config := testConfig(t) + defer os.Remove(config["command"].(string)) hostkey_file, err := ioutil.TempFile("", "hostkey") if err != nil { @@ -99,7 +116,8 @@ func TestProvisionerPrepare_PlaybookFile(t *testing.T) { func TestProvisionerPrepare_HostKeyFile(t *testing.T) { var p Provisioner - config := testConfig() + config := testConfig(t) + defer os.Remove(config["command"].(string)) publickey_file, err := ioutil.TempFile("", "publickey") if err != nil { @@ -143,7 +161,8 @@ func TestProvisionerPrepare_HostKeyFile(t *testing.T) { func TestProvisionerPrepare_AuthorizedKeyFile(t *testing.T) { var p Provisioner - config := testConfig() + config := testConfig(t) + defer os.Remove(config["command"].(string)) hostkey_file, err := ioutil.TempFile("", "hostkey") if err != nil { @@ -187,7 +206,8 @@ func TestProvisionerPrepare_AuthorizedKeyFile(t *testing.T) { func TestProvisionerPrepare_LocalPort(t *testing.T) { var p Provisioner - config := testConfig() + config := testConfig(t) + defer os.Remove(config["command"].(string)) hostkey_file, err := ioutil.TempFile("", "hostkey") if err != nil {