Merge pull request #3291 from rickard-von-essen/issue-3275

Ansible: don't use deprecated ssh options when ver above 2.0
This commit is contained in:
Rickard von Essen 2016-03-07 13:32:00 +01:00
commit 96ac0d0e90
2 changed files with 67 additions and 10 deletions

View File

@ -16,6 +16,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
@ -56,6 +57,8 @@ type Provisioner 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)

View File

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