Repository: saltstack/salt
Pull request: salt-cloud/vmware if specified use ssh_host to deploy
Fixes #36543

### What does this PR do?
In the salt-cloud vmware provider, use ssh_host when specified to deploy salt.

### What issues does this PR fix or reference?
#36543

### Previous Behavior
Deploy salt through the first discovered IP address.

### New Behavior
If ssh_host is defined in the profile, prefer it to the discovered IP address.

### Tests written?

No

### Commits signed with GPG?

No

Please review [Salt's Contributing Guide](https://docs.saltstack.com/en/latest/topics/development/contributing.html) for best practices.

See GitHub's [page on GPG signing](https://help.github.com/articles/signing-commits-using-gpg/) for more information about signing commits with GPG.


Problem statement:
Salt Cloud / vmware: Allow 'ssh_host' in VM profile
### Description of Issue/Question

In my situation, I'm using Salt Cloud with VMWare to create VMs in vCenter. Some VMs need to have two NICs defined, but due to business reasons, only the IP on "Network adapter 1" (eth0) will be functional on first boot. Unfortunately, Salt Cloud (or its vmware module) simply queries vCenter for the VM's IPs and takes the first one it gets back. For whatever reason, this is consistenly "Network adapter 2" (eth1), which isn't functional yet. Salt Cloud then tries to connect to the not-connectable IP to do the deploy, and eventually times out and fails.
If this process would pass along the 'ssh_host' value for Salt Cloud to eventually connect to, I could get around this whole problem.
### Example

Here's an example dictionary of what I'd _like_ to be able to pass in to CloudClient.

```
        vmdict = {
            'names': [vmname],
            'provider': myprovider,
            'clonefrom': mytemplate.name,
            'cluster': mycluster,
            'datacenter': mydatacenter,
            'datastore': myname,
            'ssh_host': eth0_IP,
            'ssh_username': mytemplate.username,
            'password': mytemplate.password,
            etc, etc,
        }
```

The ssh_host value would be new. @nmadhok said this is something he might be able to look at.
### Versions Report

salt-cloud 2016.3.2 (Boron)

Hints from issue discussion:
@benrolsen Thanks for filing this request and thank you to @nmadhok for volunteering to take a look at this one!

I have a similar request, but the use case is a bit different.
In my setup the deployed vm ip addresses are all internal to the cluster. Communication to the outside world is through NAT and port forwarding. So, I would like to make sure that I can define the ssh host/port to any arbitrary ip/port and not bind it to a specific nic on the guest.
Same here - the situation is slightly different, but ends up in the same feature request.

We have a few base images for salt cloud. `eth0` is always the interface that should be used by salt-cloud to connect to using ssh. There is always a second NIC which should not be used for the spin up. Usually the first NIC returned by VMWare is the `eth0`, but for one of the machines the `open-vm-tools` decide to answer with the IP of `eth1` which results in an error.

I already tracked down that the ordering is made by the `open-vm-tools` and I can not see any reason why it is ordered in another way in our special case. In vSphere the VM also shows me the IP address of `eth1` in the overview.

Being able to configure the `ssh_host` in the profile should solve this. No longer relying on the ordering made by vmware makes sense to me.
Huh, just came across this. What @avbehar mentioned would be fantastic. We have a VMWare NSX environment here and the only way I'm able to bootstrap the machines is by creating a saltstack instance inside the environment (which has to be created outside first, and then moved in).

Some way of specifying the IP to use for connection would be fantastic, even though slightly less automated than grabbing the reported IPs from vmware.

Commit message: salt-cloud/vmware if specified use ssh_host to deploy

Fixes #36543

Patch to review:
diff --git a/salt/cloud/clouds/vmware.py b/salt/cloud/clouds/vmware.py
index c04cf52dc97d..022d807cf8db 100644
--- a/salt/cloud/clouds/vmware.py
+++ b/salt/cloud/clouds/vmware.py
@@ -2803,7 +2803,9 @@ def create(vm_):
             # ssh or smb using ip and install salt only if deploy is True
             if deploy:
                 vm_['key_filename'] = key_filename
-                vm_['ssh_host'] = ip
+                # if specified, prefer ssh_host to the discovered ip address
+                vm_['ssh_host'] = vm_.get('ssh_host', ip)
+                log.info("[ {0} ] Deploying to {1}".format(vm_name, vm_['ssh_host']))
 
                 out = __utils__['cloud.bootstrap'](vm_, __opts__)
 


Code review comments:
diff_hunk: @@ -2803,7 +2803,9 @@ def create(vm_):
             # ssh or smb using ip and install salt only if deploy is True
             if deploy:
                 vm_['key_filename'] = key_filename
-                vm_['ssh_host'] = ip
+                # if specified, prefer ssh_host to the discovered ip address
+                vm_['ssh_host'] = vm_.get('ssh_host', ip)
path: salt/cloud/clouds/vmware.py
text: @user1:
why not just do
```
if 'ssh_host' not in vm_:
    vm_['ssh_host'] = ip
```

@author:
Indeed, changed. Thanks