From cc3dadf16e3df330e8daf3f3d0d1c3607f513d74 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Sun, 29 Jul 2018 14:51:14 +0530 Subject: [PATCH] VMware: new module : vmware_about_facts (#43146) Signed-off-by: Abhijeet Kasurde --- .../cloud/vmware/vmware_about_facts.py | 124 ++++++++++++++++++ .../targets/vmware_about_facts/aliases | 2 + .../targets/vmware_about_facts/tasks/main.yml | 89 +++++++++++++ 3 files changed, 215 insertions(+) create mode 100644 lib/ansible/modules/cloud/vmware/vmware_about_facts.py create mode 100644 test/integration/targets/vmware_about_facts/aliases create mode 100644 test/integration/targets/vmware_about_facts/tasks/main.yml diff --git a/lib/ansible/modules/cloud/vmware/vmware_about_facts.py b/lib/ansible/modules/cloud/vmware/vmware_about_facts.py new file mode 100644 index 0000000000..45cc3e7c98 --- /dev/null +++ b/lib/ansible/modules/cloud/vmware/vmware_about_facts.py @@ -0,0 +1,124 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# +# Copyright: (c) 2018, Abhijeet Kasurde +# +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} + +DOCUMENTATION = r''' +--- +module: vmware_about_facts +short_description: Provides information about VMware server to which user is connecting to +description: +- This module can be used to gather information about VMware server to which user is trying to connect. +version_added: 2.7 +author: +- Abhijeet Kasurde (@Akasurde) +notes: +- Tested on vSphere 6.5 +requirements: +- python >= 2.6 +- PyVmomi +extends_documentation_fragment: vmware.documentation +''' + +EXAMPLES = r''' +- name: Provide information about vCenter + vmware_about_facts: + hostname: '{{ vcenter_hostname }}' + username: '{{ vcenter_username }}' + password: '{{ vcenter_password }}' + register: vcenter_about_info + +- name: Provide information about a standalone ESXi server + vmware_about_facts: + hostname: '{{ esxi_hostname }}' + username: '{{ esxi_username }}' + password: '{{ esxu_password }}' + register: esxi_about_info +''' + +RETURN = r''' +about_facts: + description: + - dict about VMware server + returned: success + type: string + sample: + { + "api_type": "VirtualCenter", + "api_version": "6.5", + "build": "5973321", + "instance_uuid": "dbed6e0c-bd88-4ef6-b594-21283e1c677f", + "license_product_name": "VMware VirtualCenter Server", + "license_product_version": "6.0", + "locale_build": "000", + "locale_version": "INTL", + "os_type": "darwin-amd64", + "product_full_name": "VMware vCenter Server 6.5.0 build-5973321", + "product_line_id": "vpx", + "product_name": "VMware vCenter Server (govmomi simulator)", + "vendor": "VMware, Inc.", + "version": "6.5.0" + } +''' + +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.vmware import vmware_argument_spec, PyVmomi + + +class VmwareAboutManager(PyVmomi): + def __init__(self, module): + super(VmwareAboutManager, self).__init__(module) + + def gather_about_facts(self): + + if not self.content: + self.module.exit_json(changed=False, about_facts=dict()) + + about = self.content.about + + self.module.exit_json( + changed=False, + about_facts=dict( + product_name=about.name, + product_full_name=about.fullName, + vendor=about.vendor, + version=about.version, + build=about.build, + locale_version=about.localeVersion, + locale_build=about.localeBuild, + os_type=about.osType, + product_line_id=about.productLineId, + api_type=about.apiType, + api_version=about.apiVersion, + instance_uuid=about.instanceUuid, + license_product_name=about.licenseProductName, + license_product_version=about.licenseProductVersion, + ) + ) + + +def main(): + argument_spec = vmware_argument_spec() + + module = AnsibleModule( + argument_spec=argument_spec, + supports_check_mode=True, + ) + + vmware_about_facts_mgr = VmwareAboutManager(module) + vmware_about_facts_mgr.gather_about_facts() + + +if __name__ == "__main__": + main() diff --git a/test/integration/targets/vmware_about_facts/aliases b/test/integration/targets/vmware_about_facts/aliases new file mode 100644 index 0000000000..32dd9d5345 --- /dev/null +++ b/test/integration/targets/vmware_about_facts/aliases @@ -0,0 +1,2 @@ +shippable/vcenter/group1 +cloud/vcenter diff --git a/test/integration/targets/vmware_about_facts/tasks/main.yml b/test/integration/targets/vmware_about_facts/tasks/main.yml new file mode 100644 index 0000000000..d791b2ec72 --- /dev/null +++ b/test/integration/targets/vmware_about_facts/tasks/main.yml @@ -0,0 +1,89 @@ +# Test code for the vmware_about_facts module. +# Copyright: (c) 2018, Abhijeet Kasurde +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +- name: store the vcenter container ip + set_fact: + vcsim: "{{ lookup('env', 'vcenter_host') }}" + +- debug: var=vcsim + +- name: Wait for Flask controller to come up online + wait_for: + host: "{{ vcsim }}" + port: 5000 + state: started + +- name: kill vcsim + uri: + url: http://{{ vcsim }}:5000/killall + +- name: start vcsim + uri: + url: http://{{ vcsim }}:5000/spawn?cluster=2 + register: vcsim_instance + +- debug: + var: vcsim_instance + +- name: Wait for vcsim server to come up online + wait_for: + host: "{{ vcsim }}" + port: 443 + state: started + + +- name: Get Details about VMware vCenter Server + vmware_about_facts: + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance.json.username }}" + password: "{{ vcsim_instance.json.password }}" + validate_certs: no + register: about + +- assert: + that: + - about.about_facts["{{ item }}"] is defined + with_items: + - api_type + - api_version + - build + - instance_uuid + - license_product_name + - license_product_version + - locale_build + - locale_version + - os_type + - product_full_name + - product_line_id + - product_name + - vendor + - version + +- name: Get Details about VMware vCenter Server in check mode + vmware_about_facts: + hostname: "{{ vcsim }}" + username: "{{ vcsim_instance.json.username }}" + password: "{{ vcsim_instance.json.password }}" + validate_certs: no + register: about + check_mode: yes + +- assert: + that: + - about.about_facts["{{ item }}"] is defined + with_items: + - api_type + - api_version + - build + - instance_uuid + - license_product_name + - license_product_version + - locale_build + - locale_version + - os_type + - product_full_name + - product_line_id + - product_name + - vendor + - version