Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add datacenter and URL to the output of "govc vm.info" command. #2855

Open
bschonec opened this issue May 26, 2022 · 10 comments
Open

Add datacenter and URL to the output of "govc vm.info" command. #2855

bschonec opened this issue May 26, 2022 · 10 comments

Comments

@bschonec
Copy link

bschonec commented May 26, 2022

Use Case:

I'm using Ansible to find a VM that could be in one of many datacenters or VMWare installations so that I can then create or delete a VM snapshot. Looping around a list, I do a:

./govc vm.info -u {{ url }} -dc={{ datacenter}} -json my_VM_to_find

for each item in my list. govc then returns a hash with "VirtualMachines" value of either null or the information about the VM.

Unfortunately, the datacenter and URL used in the Ansible command are lost in the resulting output making it very difficult to determine where the VM was found.

It would be extremely helpful to return the URL and datacenter in the JSON output of vm.info.

Example-ish Ansible code:

vmware:
  - name: Los Angeles DC
    url:  la.vmware.example.com
    datacenters:
      - downtown
      - uptown
      - midtown
  - name: New York DC
    url:  ny.vmware.example.com
    datacenters:
      - manhattan
      - bronx
      - chinatown

# Ansible task which does a loop over all the datacenters in Los Angeles and then all the datacenters in New York.
# The resulting variable, 'govc' contains six elements.  If the VM was found, one of those elements contains the information
# about the VM and the other five elements contain "{"VirtualMachines":null}".
- name: Query VCenter for the host names
  command: "./govc -u {{ item.0.url }} -dc='{{ item.1 }}' -json=true {{ inventory_hostname_short }}"
  args:
    chdir: "{{ playbook_dir }}"
  with_subelements:
    - "{{ vmware }}"
    - datacenters
  register: govc

At this point, I have a six-element variable that contains either the found VM or "{"VirtualMachines":null}". Unfortunately, the URL and datacenter information are lost and I have no easy way to determine where that VM is.

@github-actions
Copy link
Contributor

Howdy 🖐   bschonec ! Thank you for your interest in this project. We value your feedback and will respond soon.

If you want to contribute to this project, please make yourself familiar with the CONTRIBUTION guidelines.

@embano1
Copy link
Contributor

embano1 commented May 30, 2022

Unfortunately, the datacenter and URL used in the Ansible command are lost in the resulting output

Am I understanding correctly that you lose the Ansible context (variables) after executing the govc command? Not sure why that is the case, but that is Ansible specific then.

It would be extremely helpful to return the URL and datacenter in the JSON output of vm.info

This is harder than it sounds, unfortunately. The vCenter inventory is a hierarchical tree structure. There is no "direct" reference between a VirtualMachine managed object and a datacenter managed object.

IMHO One would have to traverse the inventory upwards from the VirtualMachine to retrieve the corresponding datacenter in your case.

cc/ @dougm in case he has a better option.

@bschonec
Copy link
Author

bschonec commented May 31, 2022

The URL and the datacenter are provided to govc on the command line. When then VM is found, why not just spit out those two (or all) command-line values in the JSON output?

@embano1
Copy link
Contributor

embano1 commented May 31, 2022

I'm still not 100% clear on the request since those parameters are known to the caller anyways. Any matching response from govc will be based on these parameters. So why do you see the need to change the code and make it part of the API if those values are known upfront?

@github-actions
Copy link
Contributor

This issue is stale because it has been open for 90 days with no
activity. It will automatically close after 30 more days of
inactivity. Mark as fresh by adding the comment /remove-lifecycle stale.

@bschonec
Copy link
Author

In order to find a VM one needs to know which datacenter to search. In my case, I have multiple datacenters in my infrastructure so I have to search all of them so that I know I've searched everywhere. Take a look at the vmware dictionary (above).

Ansible needs to query each datacenter with the govc binary via a loop in code. If the VM isn't found then the next datacenter(s) need(s) to be queried until the VM is found. You're left with an array of dicts that MIGHT contain the VM found. You then need to loop around all of the elements in the array to see if the VM is found. If the VM is found in the element of the array then it would be nice to have the information you need to reconnect to the VMware infrastructure in order to do work on that VM (in my case, take a snapshot).

@atc0005
Copy link
Contributor

atc0005 commented Sep 28, 2022

/remove-lifecycle stale

@github-actions
Copy link
Contributor

This issue is stale because it has been open for 90 days with no
activity. It will automatically close after 30 more days of
inactivity. Mark as fresh by adding the comment /remove-lifecycle stale.

@bschonec
Copy link
Author

bschonec commented Jan 9, 2023

I'm trying to keep this request alive because it's not possible in ansible to 'remember' the datacenter when using a loop. There's no functionality to provide "If vm is found then set a variable and break out of a loop". It would seem to me that'd it be pretty simple to return the datacenter when the VM is found.

@github-actions
Copy link
Contributor

This issue is stale because it has been open for 90 days with no
activity. It will automatically close after 30 more days of
inactivity. Mark as fresh by adding the comment /remove-lifecycle stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants