| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2020, sky-joker |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
--- |
| 14 |
module: vmware_host_logbundle_info |
| 15 |
short_description: Gathers manifest info for logbundle |
| 16 |
description: |
| 17 |
- This module can be used to gather manifest information for logbundle from ESXi. |
| 18 |
author: |
| 19 |
- sky-joker (@sky-joker) |
| 20 |
options: |
| 21 |
esxi_hostname: |
| 22 |
description: |
| 23 |
- Name of the host system to fetch the manifests for logbundle. |
| 24 |
type: str |
| 25 |
required: true |
| 26 |
extends_documentation_fragment: |
| 27 |
- vmware.vmware.base_options |
| 28 |
''' |
| 29 |
|
| 30 |
EXAMPLES = r''' |
| 31 |
- name: fetch the manifests for logbundle from ESXi |
| 32 |
community.vmware.vmware_host_logbundle_info: |
| 33 |
hostname: "{{ vcenter_hostname }}" |
| 34 |
username: "{{ vcenter_username }}" |
| 35 |
password: "{{ vcenter_password }}" |
| 36 |
esxi_hostname: "{{ esxi_hostname }}" |
| 37 |
register: fetch_manifests_result |
| 38 |
''' |
| 39 |
|
| 40 |
RETURN = r''' |
| 41 |
manifests: |
| 42 |
description: list of dictionary of manifest information for logbundle |
| 43 |
returned: always |
| 44 |
type: list |
| 45 |
sample: |
| 46 |
[ |
| 47 |
{ |
| 48 |
"enabled": "true", |
| 49 |
"group": "System", |
| 50 |
"id": "System:Base", |
| 51 |
"name": "Base", |
| 52 |
"vmOnly": "false" |
| 53 |
}, |
| 54 |
{ |
| 55 |
"enabled": "false", |
| 56 |
"group": "System", |
| 57 |
"id": "System:BaseMinmal", |
| 58 |
"name": "BaseMinmal", |
| 59 |
"vmOnly": "false" |
| 60 |
}, |
| 61 |
{ |
| 62 |
"enabled": "true", |
| 63 |
"group": "Fcd", |
| 64 |
"id": "Fcd:Catalog", |
| 65 |
"name": "Catalog", |
| 66 |
"vmOnly": "false" |
| 67 |
}, |
| 68 |
{ |
| 69 |
"enabled": "false", |
| 70 |
"group": "VirtualMachines", |
| 71 |
"id": "VirtualMachines:CoreDumpHung", |
| 72 |
"name": "CoreDumpHung", |
| 73 |
"vmOnly": "true" |
| 74 |
}, |
| 75 |
{ |
| 76 |
"enabled": "true", |
| 77 |
"group": "System", |
| 78 |
"id": "System:CoreDumps", |
| 79 |
"name": "CoreDumps", |
| 80 |
"vmOnly": "false" |
| 81 |
} |
| 82 |
] |
| 83 |
''' |
| 84 |
|
| 85 |
try: |
| 86 |
from pyVmomi import vim |
| 87 |
except ImportError: |
| 88 |
pass |
| 89 |
|
| 90 |
from ansible_collections.community.vmware.plugins.module_utils.vmware import PyVmomi |
| 91 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import base_argument_spec |
| 92 |
from ansible.module_utils.basic import AnsibleModule |
| 93 |
from ansible.module_utils.urls import fetch_url |
| 94 |
import xml.etree.ElementTree as ET |
| 95 |
|
| 96 |
|
| 97 |
class VMwareHostLogbundleInfo(PyVmomi): |
| 98 |
def __init__(self, module): |
| 99 |
super(VMwareHostLogbundleInfo, self).__init__(module) |
| 100 |
self.esxi_hostname = self.params['esxi_hostname'] |
| 101 |
|
| 102 |
def generate_req_headers(self, url): |
| 103 |
# get ticket |
| 104 |
req = vim.SessionManager.HttpServiceRequestSpec(method='httpGet', url=url) |
| 105 |
ticket = self.content.sessionManager.AcquireGenericServiceTicket(req) |
| 106 |
|
| 107 |
headers = { |
| 108 |
'Content-Type': 'application/octet-stream', |
| 109 |
'Cookie': 'vmware_cgi_ticket=%s' % ticket.id |
| 110 |
} |
| 111 |
|
| 112 |
return headers |
| 113 |
|
| 114 |
def get_listmanifests(self): |
| 115 |
url = 'https://' + self.esxi_hostname + '/cgi-bin/vm-support.cgi?listmanifests=1' |
| 116 |
headers = self.generate_req_headers(url) |
| 117 |
|
| 118 |
try: |
| 119 |
resp, info = fetch_url(self.module, method='GET', headers=headers, url=url) |
| 120 |
manifest_list = ET.fromstring(resp.read()) |
| 121 |
manifests = [] |
| 122 |
for manifest in manifest_list[0]: |
| 123 |
manifests.append(manifest.attrib) |
| 124 |
|
| 125 |
self.module.exit_json(changed=False, manifests=manifests) |
| 126 |
except Exception as e: |
| 127 |
self.module.fail_json(msg="Failed to fetch manifests from %s: %s" % (url, e)) |
| 128 |
|
| 129 |
|
| 130 |
def main(): |
| 131 |
argument_spec = base_argument_spec() |
| 132 |
argument_spec.update( |
| 133 |
esxi_hostname=dict(type='str', required=True) |
| 134 |
) |
| 135 |
|
| 136 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 137 |
|
| 138 |
vmware_host_logbundle_info_mgr = VMwareHostLogbundleInfo(module) |
| 139 |
vmware_host_logbundle_info_mgr.get_listmanifests() |
| 140 |
|
| 141 |
|
| 142 |
if __name__ == "__main__": |
| 143 |
main() |