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

VMware: add properties option to vmware_host_facts module #62916

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- vmware_host_facts - added ``properties`` and ``schema`` options.
59 changes: 58 additions & 1 deletion lib/ansible/modules/cloud/vmware/vmware_host_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,32 @@
type: bool
required: False
version_added: 2.9
schema:
description:
- Specify the output schema desired.
- The 'summary' output schema is the legacy output from the module
- The 'vsphere' output schema is the vSphere API class definition
which requires pyvmomi>6.7.1
choices: ['summary', 'vsphere']
default: 'summary'
type: str
version_added: '2.10'
properties:
description:
- Specify the properties to retrieve.
- If not specified, all properties are retrieved (deeply).
- Results are returned in a structure identical to the vsphere API.
- 'Example:'
- ' properties: ['
- ' "hardware.memorySize",'
- ' "hardware.cpuInfo.numCpuCores",'
- ' "config.product.apiVersion",'
- ' "overallStatus"'
- ' ]'
- Only valid when C(schema) is C(vsphere).
type: list
required: False
version_added: '2.10'
extends_documentation_fragment: vmware.documentation
'''

Expand Down Expand Up @@ -85,6 +111,20 @@
register: host_facts
- set_fact:
cluster_uuid: "{{ host_facts['ansible_facts']['vsan_cluster_uuid'] }}"

- name: Gather some info from a host using the vSphere API output schema
vmware_host_facts:
hostname: "{{ vcenter_server }}"
username: "{{ esxi_username }}"
password: "{{ esxi_password }}"
esxi_hostname: "{{ esxi_hostname }}"
schema: vsphere
properties:
- hardware.memorySize
- hardware.cpuInfo.numCpuCores
- config.product.apiVersion
- overallStatus
register: host_facts
'''

RETURN = r'''
Expand Down Expand Up @@ -285,18 +325,35 @@ def get_system_facts(self):
}
return facts

def properties_facts(self):
ansible_facts = self.to_json(self.host, self.params.get('properties'))
if self.params.get('show_tag'):
vmware_client = VmwareRestClient(self.module)
tag_info = {
'tags': vmware_client.get_tags_for_hostsystem(hostsystem_mid=self.host._moId)
}
ansible_facts.update(tag_info)

self.module.exit_json(changed=False, ansible_facts=ansible_facts)


def main():
argument_spec = vmware_argument_spec()
argument_spec.update(
esxi_hostname=dict(type='str', required=False),
show_tag=dict(type='bool', default=False),
schema=dict(type='str', choices=['summary', 'vsphere'], default='summary'),
properties=dict(type='list')
)
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)

vm_host_manager = VMwareHostFactManager(module)
vm_host_manager.all_facts()

if module.params['schema'] == 'summary':
vm_host_manager.all_facts()
else:
vm_host_manager.properties_facts()


if __name__ == '__main__':
Expand Down
55 changes: 54 additions & 1 deletion test/integration/targets/vmware_host_facts/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,57 @@
- "'ansible_hostname' in facts['ansible_facts']"
- "'ansible_processor' in facts['ansible_facts']"
- "'ansible_in_maintenance_mode' in facts['ansible_facts']"
- "'ansible_uptime' in facts['ansible_facts']"
- "'ansible_uptime' in facts['ansible_facts']"

- name: get host properties facts through a vcenter
vmware_host_facts:
validate_certs: False
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
esxi_hostname: '{{ esxi1 }}'
schema: vsphere
properties:
- hardware.memorySize
- hardware.cpuInfo.numCpuCores
- config.product.apiVersion
- overallStatus
register: facts
- debug: var=facts
- name: verify some data,like ansible_processor
assert:
that:
- "'hardware' in facts['ansible_facts']"
- "'config' in facts['ansible_facts']"
- "'overallStatus' in facts['ansible_facts']"
- "'memorySize' in facts['ansible_facts']['hardware']"
- "'cpuInfo' in facts['ansible_facts']['hardware']"
- "'numCpuCores' in facts['ansible_facts']['hardware']['cpuInfo']"
- "'product' in facts['ansible_facts']['config']"
- "'apiVersion' in facts['ansible_facts']['config']['product']"

- name: get host properties facts through from a host
vmware_host_facts:
validate_certs: False
hostname: '{{ esxi1 }}'
username: '{{ esxi_user }}'
password: '{{ esxi_password }}'
schema: vsphere
properties:
- hardware.memorySize
- hardware.cpuInfo.numCpuCores
- config.product.apiVersion
- overallStatus
register: facts
- debug: var=facts
- name: verify some data,like ansible_processor
assert:
that:
- "'hardware' in facts['ansible_facts']"
- "'config' in facts['ansible_facts']"
- "'overallStatus' in facts['ansible_facts']"
- "'memorySize' in facts['ansible_facts']['hardware']"
- "'cpuInfo' in facts['ansible_facts']['hardware']"
- "'numCpuCores' in facts['ansible_facts']['hardware']['cpuInfo']"
- "'product' in facts['ansible_facts']['config']"
- "'apiVersion' in facts['ansible_facts']['config']['product']"