| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 10 |
'status': ['preview'], |
| 11 |
'supported_by': 'community'} |
| 12 |
|
| 13 |
DOCUMENTATION = r''' |
| 14 |
--- |
| 15 |
module: ucs_query |
| 16 |
|
| 17 |
short_description: Queries UCS Manager objects by class or distinguished name |
| 18 |
|
| 19 |
description: |
| 20 |
-Queries UCS Manager objects by class or distinguished name. |
| 21 |
|
| 22 |
extends_documentation_fragment: cisco.ucs.ucs |
| 23 |
|
| 24 |
options: |
| 25 |
class_ids: |
| 26 |
description: |
| 27 |
- One or more UCS Manager Class IDs to query. |
| 28 |
- As a comma separated list |
| 29 |
type: str |
| 30 |
|
| 31 |
distinguished_names: |
| 32 |
description: |
| 33 |
- One or more UCS Manager Distinguished Names to query. |
| 34 |
- As a comma separated list |
| 35 |
type: str |
| 36 |
|
| 37 |
delegate_to: |
| 38 |
description: |
| 39 |
- Where the module will be run |
| 40 |
default: localhost |
| 41 |
type: str |
| 42 |
|
| 43 |
requirements: |
| 44 |
- ucsmsdk |
| 45 |
|
| 46 |
author: |
| 47 |
- John McDonough (@movinalot) |
| 48 |
- CiscoUcs (@CiscoUcs) |
| 49 |
''' |
| 50 |
|
| 51 |
EXAMPLES = r''' |
| 52 |
- name: Query UCS Class ID |
| 53 |
cisco.ucs.ucs_query: |
| 54 |
hostname: "{{ ucs_hostname }}" |
| 55 |
username: "{{ ucs_username }}" |
| 56 |
password: "{{ ucs_password }}" |
| 57 |
class_ids: computeBlade |
| 58 |
delegate_to: localhost |
| 59 |
|
| 60 |
- name: Query UCS Class IDs |
| 61 |
cisco.ucs.ucs_query: |
| 62 |
hostname: "{{ ucs_hostname }}" |
| 63 |
username: "{{ ucs_username }}" |
| 64 |
password: "{{ ucs_password }}" |
| 65 |
class_ids: computeBlade, fabricVlan |
| 66 |
delegate_to: localhost |
| 67 |
|
| 68 |
- name: Query UCS Distinguished Name |
| 69 |
cisco.ucs.ucs_query: |
| 70 |
hostname: "{{ ucs_hostname }}" |
| 71 |
username: "{{ ucs_username }}" |
| 72 |
password: "{{ ucs_password }}" |
| 73 |
distinguished_names: org-root |
| 74 |
delegate_to: localhost |
| 75 |
|
| 76 |
- name: Query UCS Distinguished Names |
| 77 |
cisco.ucs.ucs_query: |
| 78 |
hostname: "{{ ucs_hostname }}" |
| 79 |
username: "{{ ucs_username }}" |
| 80 |
password: "{{ ucs_password }}" |
| 81 |
distinguished_names: org-root, sys/rack-unit-1, sys/chassis-1/blade-2 |
| 82 |
delegate_to: localhost |
| 83 |
''' |
| 84 |
|
| 85 |
RETURN = r''' |
| 86 |
objects: |
| 87 |
description: results JSON encodded |
| 88 |
returned: always |
| 89 |
type: dict |
| 90 |
''' |
| 91 |
|
| 92 |
from ansible.module_utils.basic import AnsibleModule |
| 93 |
from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec |
| 94 |
|
| 95 |
|
| 96 |
def retrieve_class_id(class_id, ucs): |
| 97 |
return ucs.login_handle.query_classid(class_id) |
| 98 |
|
| 99 |
|
| 100 |
def retrieve_distinguished_name(distinguished_name, ucs): |
| 101 |
return ucs.login_handle.query_dn(distinguished_name) |
| 102 |
|
| 103 |
|
| 104 |
def make_mo_dict(ucs_mo): |
| 105 |
obj_dict = {} |
| 106 |
for mo_property in ucs_mo.prop_map.values(): |
| 107 |
obj_dict[mo_property] = getattr(ucs_mo, mo_property) |
| 108 |
return obj_dict |
| 109 |
|
| 110 |
|
| 111 |
def main(): |
| 112 |
argument_spec = ucs_argument_spec.copy() |
| 113 |
argument_spec.update( |
| 114 |
class_ids=dict(type='str'), |
| 115 |
distinguished_names=dict(type='str'), |
| 116 |
delegate_to=dict(type='str', default='localhost'), |
| 117 |
) |
| 118 |
|
| 119 |
module = AnsibleModule( |
| 120 |
argument_spec, |
| 121 |
supports_check_mode=False, |
| 122 |
mutually_exclusive=[ |
| 123 |
['class_ids', 'distinguished_names'], |
| 124 |
], |
| 125 |
) |
| 126 |
|
| 127 |
# UCSModule verifies ucsmsdk is present and exits on failure. |
| 128 |
# Imports are below for UCS object creation. |
| 129 |
ucs = UCSModule(module) |
| 130 |
err = False |
| 131 |
query_result = {} |
| 132 |
|
| 133 |
try: |
| 134 |
if module.params['class_ids']: |
| 135 |
class_ids = [ |
| 136 |
x.strip() for x in module.params['class_ids'].split(',') |
| 137 |
] |
| 138 |
for class_id in class_ids: |
| 139 |
query_result[class_id] = [] |
| 140 |
ucs_mos = retrieve_class_id(class_id, ucs) |
| 141 |
if ucs_mos: |
| 142 |
for ucs_mo in ucs_mos: |
| 143 |
query_result[class_id].append(make_mo_dict(ucs_mo)) |
| 144 |
|
| 145 |
ucs.result['objects'] = query_result |
| 146 |
|
| 147 |
elif module.params['distinguished_names']: |
| 148 |
distinguished_names = [ |
| 149 |
x.strip() |
| 150 |
for x in module.params['distinguished_names'].split(',') |
| 151 |
] |
| 152 |
for distinguished_name in distinguished_names: |
| 153 |
query_result[distinguished_name] = {} |
| 154 |
ucs_mo = retrieve_distinguished_name(distinguished_name, ucs) |
| 155 |
|
| 156 |
if ucs_mo: |
| 157 |
query_result[distinguished_name] = make_mo_dict(ucs_mo) |
| 158 |
|
| 159 |
ucs.result['objects'] = query_result |
| 160 |
|
| 161 |
except Exception as e: |
| 162 |
err = True |
| 163 |
ucs.result['msg'] = "setup error: %s " % str(e) |
| 164 |
|
| 165 |
if err: |
| 166 |
module.fail_json(**ucs.result) |
| 167 |
|
| 168 |
module.exit_json(**ucs.result) |
| 169 |
|
| 170 |
|
| 171 |
if __name__ == '__main__': |
| 172 |
main() |