From 7b7d266a397ac45b2c68dde01fa8bc88f172490e Mon Sep 17 00:00:00 2001 From: sky-joker Date: Mon, 7 Oct 2019 13:57:57 +0900 Subject: [PATCH] VMware: add properties option to vmware_host_facts module (#62916) * add properties option to vmware_host_facts --- ...properties_option_to_vmware_host_facts.yml | 2 + .../modules/cloud/vmware/vmware_host_facts.py | 59 ++++++++++++++++++- .../targets/vmware_host_facts/tasks/main.yml | 55 ++++++++++++++++- 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/62916-add_properties_option_to_vmware_host_facts.yml diff --git a/changelogs/fragments/62916-add_properties_option_to_vmware_host_facts.yml b/changelogs/fragments/62916-add_properties_option_to_vmware_host_facts.yml new file mode 100644 index 00000000000..bec1e16becd --- /dev/null +++ b/changelogs/fragments/62916-add_properties_option_to_vmware_host_facts.yml @@ -0,0 +1,2 @@ +minor_changes: + - vmware_host_facts - added ``properties`` and ``schema`` options. diff --git a/lib/ansible/modules/cloud/vmware/vmware_host_facts.py b/lib/ansible/modules/cloud/vmware/vmware_host_facts.py index f485a6747a3..70f58aeadb5 100644 --- a/lib/ansible/modules/cloud/vmware/vmware_host_facts.py +++ b/lib/ansible/modules/cloud/vmware/vmware_host_facts.py @@ -46,6 +46,32 @@ options: 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 ''' @@ -85,6 +111,20 @@ EXAMPLES = r''' 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''' @@ -285,18 +325,35 @@ class VMwareHostFactManager(PyVmomi): } 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__': diff --git a/test/integration/targets/vmware_host_facts/tasks/main.yml b/test/integration/targets/vmware_host_facts/tasks/main.yml index 89e5c41449f..382e5155ae6 100644 --- a/test/integration/targets/vmware_host_facts/tasks/main.yml +++ b/test/integration/targets/vmware_host_facts/tasks/main.yml @@ -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']" \ No newline at end of file + - "'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']"