From bf3c29432697f94a9cc6d216bc751380d2084284 Mon Sep 17 00:00:00 2001 From: "Billie H. Cleek" Date: Thu, 28 Jan 2016 22:22:12 -0800 Subject: [PATCH] listen on system chosen port Change the default for LocalPort to "0", so that the listener will be a system chosen port. --- provisioner/ansible/provisioner.go | 34 +++++++++++++------ .../docs/provisioners/ansible.html.markdown | 10 +++--- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/provisioner/ansible/provisioner.go b/provisioner/ansible/provisioner.go index 5eaa08c12..6efb255c2 100644 --- a/provisioner/ansible/provisioner.go +++ b/provisioner/ansible/provisioner.go @@ -92,6 +92,8 @@ func (p *Provisioner) Prepare(raws ...interface{}) error { if _, err := strconv.ParseUint(p.config.LocalPort, 10, 16); err != nil { errs = packer.MultiErrorAppend(errs, fmt.Errorf("local_port: %s must be a valid port", p.config.LocalPort)) } + } else { + p.config.LocalPort = "0" } if errs != nil && len(errs.Errors) > 0 { @@ -149,19 +151,28 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { config.AddHostKey(private) localListener, err := func() (net.Listener, error) { - port, _ := strconv.ParseUint(p.config.LocalPort, 10, 16) - if port == 0 { - port = 2200 + port, err := strconv.ParseUint(p.config.LocalPort, 10, 16) + if err != nil { + return nil, err } - for i := 0; i < 10; i++ { - port++ - l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port)) - if err == nil { - p.config.LocalPort = strconv.FormatUint(port, 10) - return l, nil - } - ui.Say(err.Error()) + tries := 1 + if port != 0 { + tries = 10 + } + for i := 0; i < tries; i++ { + l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port)) + port++ + if err != nil { + ui.Say(err.Error()) + continue + } + _, p.config.LocalPort, err = net.SplitHostPort(l.Addr().String()) + if err != nil { + ui.Say(err.Error()) + continue + } + return l, nil } return nil, errors.New("Error setting up SSH proxy connection") }() @@ -205,6 +216,7 @@ func (p *Provisioner) Provision(ui packer.Ui, comm packer.Communicator) error { } return nil + } func (p *Provisioner) Cancel() { diff --git a/website/source/docs/provisioners/ansible.html.markdown b/website/source/docs/provisioners/ansible.html.markdown index e91f55f7a..67b14e383 100644 --- a/website/source/docs/provisioners/ansible.html.markdown +++ b/website/source/docs/provisioners/ansible.html.markdown @@ -51,10 +51,12 @@ Required Parameters: Optional Parameters: -- `local_port` (string) - The port on which to - attempt to listen for SSH connections. This value is a starting point. - The provisioner will attempt listen for SSH connections on the first - available of ten ports, starting at `local_port`. The default value is 2200. +- `local_port` (string) - The port on which to attempt to listen for SSH + connections. This value is a starting point. The provisioner will attempt + listen for SSH connections on the first available of ten ports, starting at + `local_port`. When `local_port` is missing or empty, ansible-provisioner will + listen on a system-chosen port. + - `sftp_command` (string) - The command to run on the machine to handle the SFTP protocol that Ansible will use to transfer files. The command should