ansible-collections / community.vmware

Ansible Collection for VMware
GNU General Public License v3.0
349 stars 338 forks source link

vmware_portgroup, "hosts" or "cluster" required when logging into ESXi #2032

Open springer268 opened 8 months ago

springer268 commented 8 months ago

Using an example with the same params as the first vmware_portgroup examples page

- name: Add Management Network VM Portgroup
  community.vmware.vmware_portgroup:
    hostname: "{{ esxi_hostname }}"
    username: "{{ esxi_username }}"
    password: "{{ esxi_password }}"
    switch: "{{ vswitch_name }}"
    portgroup: "{{ portgroup_name }}"
    vlan_id: "{{ vlan_id }}"
  delegate_to: localhost

And logging into ESXi directly This returns

fatal: [localhost]: FAILED! => {"changed": false, "msg": "one of the following is required: cluster_name, hosts"}

This behavior makes sense for vCenter, but not for ESXi, and does not follow the example in the docs.

mbx64 commented 6 months ago

As a workaround (until fixed) you can add esxi_hostname: foobar, literally, as the name will be ignored when connecting to ESXi:

- name: Add Management Network VM Portgroup
  community.vmware.vmware_portgroup:
    hostname: "{{ esxi_hostname }}"
    username: "{{ esxi_username }}"
    password: "{{ esxi_password }}"
    esxi_hostname: foobar
    switch: "{{ vswitch_name }}"
    portgroup: "{{ portgroup_name }}"
    vlan_id: "{{ vlan_id }}"
  delegate_to: localhost

Btw. esxi_hostname is an alias for hosts.

ihumster commented 6 months ago

Yep, this is documentation issues.

Module community.vmware.vmware_portgroup need one of two required parameters - cluster_name or hosts. Even if you work with standalone esxi, you must specify this esxi in hosts.

- name: Add Management Network VM Portgroup
  community.vmware.vmware_portgroup:
    hostname: "{{ esxi_hostname }}"
    username: "{{ esxi_username }}"
    password: "{{ esxi_password }}"
    hosts:
      - "{{ esxi_hostname }}"
    switch: "{{ vswitch_name }}"
    portgroup: "{{ portgroup_name }}"
    vlan_id: "{{ vlan_id }}"
  delegate_to: localhost
mbx64 commented 1 month ago

@ihumster As I said before, it does not matter which name I provide for esxi_hostname/hosts (standalone). So it should not be mandatory for standalone hosts. Thus, not just a documentation problem, imho. The useless requirement should be lifted.

ihumster commented 1 month ago

@mbx64 It seems to you that the requirement is useless because you are working with standalone esxi. But to work through vCenter, you also need to specify the host(s) or cluster. And at the same time, your parameters in hostaname and hosts will not match

mbx64 commented 1 month ago

@ihumster Therefore I propose something like:

if vcenter then
    require hosts/hostname
else
    do not require hosts/hostname
endif

This seems to be the only module that has this problem. So maybe the other modules have already the code to distinguish these cases.

ihumster commented 1 month ago

Paramters hostname, username and password are parameters for connecting to a particular system. The cluster_name and hosts parameters are targeting parameters. When connecting to vCenter, any of these two parameters are required and mutually exclusive. Connecting to standalone esxi is a special case.

The only problem I see is that the first two examples don't match reality. @mariolenz FYI