| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# Copyright: (c) 2019-2020, Ansible Project |
| 5 |
# Copyright: (c) 2019-2020, Naveenkumar G P <[email protected]> |
| 6 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 7 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 8 |
|
| 9 |
from __future__ import absolute_import, division, print_function |
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: vmware_vc_infraprofile_info |
| 16 |
short_description: List and Export VMware vCenter infra profile configs. |
| 17 |
description: |
| 18 |
- Module to manage VMware vCenter infra profile configs. |
| 19 |
- vCenter infra profile Library feature is introduced in vSphere 7.0 version, so this module is not supported in the earlier versions of vSphere. |
| 20 |
author: |
| 21 |
- Naveenkumar G P (@ngp) |
| 22 |
requirements: |
| 23 |
- vSphere Automation SDK |
| 24 |
options: |
| 25 |
decryption_key: |
| 26 |
description: |
| 27 |
- decryption_key argument for while doing import profile task as of now its not taken into account form API team. |
| 28 |
type: str |
| 29 |
required: false |
| 30 |
encryption_key: |
| 31 |
description: |
| 32 |
- encryption_key argument for while doing import profile task as of now its not taken into account form API team. |
| 33 |
type: str |
| 34 |
required: false |
| 35 |
api: |
| 36 |
description: |
| 37 |
- API which needs to be executed |
| 38 |
type: str |
| 39 |
required: false |
| 40 |
choices: [ export, import, list, validate ] |
| 41 |
config_path: |
| 42 |
description: |
| 43 |
- Config file path which contains infra profile config JSON data, supports both relative and absolute path. |
| 44 |
- This parameter is required only when V(import),V(validate) APIs are being used. |
| 45 |
type: str |
| 46 |
required: false |
| 47 |
profiles: |
| 48 |
description: |
| 49 |
- A list of profile names to be exported, imported, and validated. |
| 50 |
- This parameter is not required while running for List API, not for V(export),V(import) and V(validate). |
| 51 |
type: str |
| 52 |
required: false |
| 53 |
description: |
| 54 |
description: |
| 55 |
- Description of about encryption or decryption key. |
| 56 |
type: str |
| 57 |
required: false |
| 58 |
extends_documentation_fragment: |
| 59 |
- vmware.vmware.base_options |
| 60 |
- vmware.vmware.additional_rest_options |
| 61 |
''' |
| 62 |
|
| 63 |
EXAMPLES = r''' |
| 64 |
- name: Get information about VC infraprofile |
| 65 |
vmware_vc_infraprofile_info: |
| 66 |
hostname: '{{ vcenter_hostname }}' |
| 67 |
username: '{{ vcenter_username }}' |
| 68 |
password: '{{ vcenter_password }}' |
| 69 |
delegate_to: localhost |
| 70 |
|
| 71 |
- name: export vCenter appliance infra profile config |
| 72 |
vmware_vc_infraprofile_info: |
| 73 |
hostname: '{{ vcenter_hostname }}' |
| 74 |
username: '{{ vcenter_username }}' |
| 75 |
password: '{{ vcenter_password }}' |
| 76 |
api: "export" |
| 77 |
profiles: "ApplianceManagement" |
| 78 |
delegate_to: localhost |
| 79 |
|
| 80 |
- name: validate vCenter appliance infra profile config |
| 81 |
vmware_vc_infraprofile_info: |
| 82 |
hostname: '{{ vcenter_hostname }}' |
| 83 |
username: '{{ vcenter_username }}' |
| 84 |
password: '{{ vcenter_password }}' |
| 85 |
api: "validate" |
| 86 |
profiles: "ApplianceManagement" |
| 87 |
config_path: "export.json" |
| 88 |
|
| 89 |
- name: import vCenter appliance infra profile config |
| 90 |
vmware_vc_infraprofile_info: |
| 91 |
hostname: '{{ vcenter_hostname }}' |
| 92 |
username: '{{ vcenter_username }}' |
| 93 |
password: '{{ vcenter_password }}' |
| 94 |
api: "import" |
| 95 |
profiles: "ApplianceManagement" |
| 96 |
config_path: "import.json" |
| 97 |
delegate_to: localhost |
| 98 |
''' |
| 99 |
|
| 100 |
RETURN = r''' |
| 101 |
list_infra: |
| 102 |
description: A list of infra configs, |
| 103 |
returned: on success with API as "list" |
| 104 |
type: list |
| 105 |
"sample": [ |
| 106 |
{ |
| 107 |
"info": "ApplianceManagement", |
| 108 |
"name": "ApplianceManagement" |
| 109 |
}, |
| 110 |
{ |
| 111 |
"info": "ApplianceNetwork", |
| 112 |
"name": "ApplianceNetwork" |
| 113 |
}, |
| 114 |
{ |
| 115 |
"info": "Authentication & Authorization Management", |
| 116 |
"name": "AuthManagement" |
| 117 |
} |
| 118 |
] |
| 119 |
|
| 120 |
export_infra: |
| 121 |
description: A message about the exported file |
| 122 |
returned: On success with API set as "export" |
| 123 |
type: dict |
| 124 |
sample: { |
| 125 |
"export_config_json":"json exported to file" |
| 126 |
} |
| 127 |
|
| 128 |
validate_infra: |
| 129 |
description: A message about validate on exported file |
| 130 |
returned: On success with API set as "validate" |
| 131 |
type: dict |
| 132 |
"sample": { |
| 133 |
"changed": false, |
| 134 |
"failed": false, |
| 135 |
"status": "VALID" |
| 136 |
} |
| 137 |
|
| 138 |
import_profile: |
| 139 |
description: A message about import on import_profile spec |
| 140 |
returned: On success with API set as "import" |
| 141 |
type: dict |
| 142 |
"sample": { |
| 143 |
"changed": true, |
| 144 |
"failed": false, |
| 145 |
"status": "0.0" |
| 146 |
} |
| 147 |
''' |
| 148 |
|
| 149 |
from ansible.module_utils.basic import AnsibleModule |
| 150 |
from ansible_collections.community.vmware.plugins.module_utils.vmware_rest_client import VmwareRestClient |
| 151 |
from ansible_collections.vmware.vmware.plugins.module_utils.argument_spec import rest_compatible_argument_spec |
| 152 |
import json |
| 153 |
import time |
| 154 |
|
| 155 |
|
| 156 |
class VcenterProfile(VmwareRestClient): |
| 157 |
|
| 158 |
def __init__(self, module): |
| 159 |
super(VcenterProfile, self).__init__(module) |
| 160 |
self.config_path = self.params['config_path'] |
| 161 |
|
| 162 |
def list_vc_infraprofile_configs(self): |
| 163 |
profile_configs_list = self.api_client.appliance.infraprofile.Configs.list() |
| 164 |
config_list = [] |
| 165 |
for x in profile_configs_list: |
| 166 |
config_list.append({'info': x.info, 'name': x.name}) |
| 167 |
self.module.exit_json(changed=False, infra_configs_list=config_list) |
| 168 |
|
| 169 |
def get_profile_spec(self): |
| 170 |
infra = self.api_client.appliance.infraprofile.Configs |
| 171 |
profiles = {} |
| 172 |
profiles = self.params['profiles'].split(",") |
| 173 |
profile_spec = infra.ProfilesSpec(encryption_key="encryption_key", description="description", profiles=set(profiles)) |
| 174 |
return profile_spec |
| 175 |
|
| 176 |
def vc_export_profile_task(self): |
| 177 |
profile_spec = self.get_profile_spec() |
| 178 |
infra = self.api_client.appliance.infraprofile.Configs |
| 179 |
config_json = infra.export(spec=profile_spec) |
| 180 |
if self.config_path is None: |
| 181 |
self.config_path = self.params.get('api') + ".json" |
| 182 |
parsed = json.loads(config_json) |
| 183 |
with open(self.config_path, 'w', encoding='utf-8') as outfile: |
| 184 |
json.dump(parsed, outfile, ensure_ascii=False, indent=2) |
| 185 |
self.module.exit_json(changed=False, export_config_json=config_json) |
| 186 |
|
| 187 |
def read_profile(self): |
| 188 |
with open(self.config_path, "r") as file: |
| 189 |
return file.read() |
| 190 |
|
| 191 |
def get_import_profile_spec(self): |
| 192 |
infra = self.api_client.appliance.infraprofile.Configs |
| 193 |
config_spec = self.read_profile() |
| 194 |
profile_spec = self.get_profile_spec() |
| 195 |
import_profile_spec = infra.ImportProfileSpec(config_spec=config_spec, profile_spec=profile_spec) |
| 196 |
return import_profile_spec |
| 197 |
|
| 198 |
def vc_import_profile_task(self): |
| 199 |
infra = self.api_client.appliance.infraprofile.Configs |
| 200 |
import_profile_spec = self.get_import_profile_spec() |
| 201 |
import_task = infra.import_profile_task(import_profile_spec) |
| 202 |
self.wait_for_task(import_task) |
| 203 |
if "SUCCEEDED" == import_task.get_info().status: |
| 204 |
self.module.exit_json(changed=True, status=import_task.get_info().result.value) |
| 205 |
self.module.fail_json(msg='Failed to import profile status:"%s" ' % import_task.get_info().status) |
| 206 |
|
| 207 |
def vc_validate_profile_task(self): |
| 208 |
infra = self.api_client.appliance.infraprofile.Configs |
| 209 |
import_profile_spec = self.get_import_profile_spec() |
| 210 |
validate_task = infra.validate_task(import_profile_spec) |
| 211 |
if "VALID" == validate_task.get_info().result.get_field("status").value: |
| 212 |
self.module.exit_json(changed=False, status=validate_task.get_info().result.get_field("status").value) |
| 213 |
elif "INVALID" == validate_task.get_info().result.get_field("status").value: |
| 214 |
# TO-DO: move to vmware_rest_client |
| 215 |
self.module.exit_json(changed=False, status=validate_task.get_info().result.get_field("status").value) |
| 216 |
else: |
| 217 |
# TO-DO: move to vmware_rest_client |
| 218 |
self.module.fail_json(msg='Failed to validate profile status:"%s" ' % dir(validate_task.get_info().status)) |
| 219 |
|
| 220 |
def wait_for_task(self, task, poll_interval=1): |
| 221 |
while task.get_info().status == "RUNNING": |
| 222 |
time.sleep(poll_interval) |
| 223 |
|
| 224 |
|
| 225 |
def main(): |
| 226 |
argument_spec = rest_compatible_argument_spec() |
| 227 |
argument_spec.update( |
| 228 |
encryption_key=dict(type='str', required=False, no_log=True), |
| 229 |
description=dict(type='str', required=False), |
| 230 |
decryption_key=dict(type='str', required=False, no_log=True), |
| 231 |
api=dict(type='str', required=False, choices=['list', 'export', 'import', 'validate']), |
| 232 |
profiles=dict(type='str', required=False), |
| 233 |
config_path=dict(type='str', required=False), |
| 234 |
) |
| 235 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 236 |
result = {'failed': False, 'changed': False} |
| 237 |
vmware_vc_infra_profile = VcenterProfile(module) |
| 238 |
|
| 239 |
if module.params['api'].lower() == "list": |
| 240 |
if module.check_mode: |
| 241 |
result.update( |
| 242 |
changed=False, desired_operation='list_vc_profile_configs',) |
| 243 |
module.exit_json(**result) |
| 244 |
vmware_vc_infra_profile.list_vc_infraprofile_configs() |
| 245 |
if module.params['api'].lower() == "export": |
| 246 |
if module.check_mode: |
| 247 |
result.update( |
| 248 |
changed=False, |
| 249 |
desired_operation='export_vc_profile_configs',) |
| 250 |
module.exit_json(**result) |
| 251 |
vmware_vc_infra_profile.vc_export_profile_task() |
| 252 |
|
| 253 |
if module.params['api'].lower() == "import": |
| 254 |
if module.check_mode: |
| 255 |
result.update( |
| 256 |
changed=True, |
| 257 |
desired_operation='import_vc_profile_configs', |
| 258 |
) |
| 259 |
module.exit_json(**result) |
| 260 |
vmware_vc_infra_profile.vc_import_profile_task() |
| 261 |
|
| 262 |
if module.params['api'].lower() == "validate": |
| 263 |
if module.check_mode: |
| 264 |
result.update( |
| 265 |
changed=True, |
| 266 |
desired_operation='import_vc_profile_configs', |
| 267 |
) |
| 268 |
module.exit_json(**result) |
| 269 |
vmware_vc_infra_profile.vc_validate_profile_task() |
| 270 |
|
| 271 |
|
| 272 |
if __name__ == '__main__': |
| 273 |
main() |