I am working on building a VirtualBox VM with type "virtualbox-iso" and OpenSUSE 42.3 as the guest OS.I am specifying host and port that the ssh communicator should use during the build but it looks like packer is ignoring the port specification.
I am overwriting the default settings for host and port in my "builders" section. This is an excerpt of my json file:
"builders": [
{
"type": "virtualbox-iso",
"communicator": "ssh",
"ssh_host": "192.168.1.5",
"ssh_port": "22",
"ssh_username": "some_user",
"ssh_password": "some_password",
"ssh_timeout": "20m",
"ssh_handshake_attempts": "1000",
Packer is unable to connect to the VM because packer is ignoring the port I am providing with "ssh_port". This is the debug output (enabled with PACKER_LOG=1):
2019/06/10 15:10:10 packer: 2019/06/10 15:10:10 [INFO] Waiting 1s
2019/06/10 15:10:11 ui: ==> opensuse-master-box: Using ssh communicator to connect: 192.168.1.5
2019/06/10 15:10:11 packer: 2019/06/10 15:10:11 [INFO] Waiting for SSH, up to timeout: 20m0s
2019/06/10 15:10:11 ui: ==> opensuse-master-box: Waiting for SSH to become available...
2019/06/10 15:10:26 packer: 2019/06/10 15:10:26 [DEBUG] TCP connection to SSH ip/port failed: dial tcp 192.168.1.5:4240: i/o timeout
2019/06/10 15:10:31 packer: 2019/06/10 15:10:31 [DEBUG] TCP connection to SSH ip/port failed: dial tcp 192.168.1.5:4240: connect: connection refused
2019/06/10 15:10:36 packer: 2019/06/10 15:10:36 [DEBUG] TCP connection to SSH ip/port failed: dial tcp 192.168.1.5:4240: connect: connection refused
2019/06/10 15:10:41 packer: 2019/06/10 15:10:41 [DEBUG] TCP connection to SSH ip/port failed: dial tcp 192.168.1.5:4240: connect: connection refused
Is this expected behavior or am I doing something wrong?
This is because of how VirtualBox NAT networks work. From the host you can't reach the guest VM directly. Packer solves this by setting up port forwarding rule. A random port between ssh_host_port_min and ssh_host_port_max is forwarded to the guest VMs ssh_port.
If you want to turn this of set ssh_skip_nat_mapping to true
, but then you have to ensure that you have a network setup where Packer can reach the guest.