| 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 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 11 |
'status': ['preview'], |
| 12 |
'supported_by': 'community'} |
| 13 |
|
| 14 |
DOCUMENTATION = r''' |
| 15 |
--- |
| 16 |
module: ucs_graphics_card_policy |
| 17 |
|
| 18 |
short_description: Manages UCS Graphics Card Policies on UCS Manager |
| 19 |
|
| 20 |
description: |
| 21 |
- Manages UCS Graphics Card Policies on UCS Manager. |
| 22 |
|
| 23 |
extends_documentation_fragment: cisco.ucs.ucs |
| 24 |
|
| 25 |
options: |
| 26 |
state: |
| 27 |
description: |
| 28 |
- If C(absent), will remove organization. |
| 29 |
- If C(present), will create or update organization. |
| 30 |
choices: [absent, present] |
| 31 |
default: present |
| 32 |
type: str |
| 33 |
|
| 34 |
name: |
| 35 |
description: |
| 36 |
- The name of the organization. |
| 37 |
- Enter up to 16 characters. |
| 38 |
- "You can use any characters or spaces except the following:" |
| 39 |
- "` (accent mark), \ (backslash), ^ (carat), \" (double quote)" |
| 40 |
- "= (equal sign), > (greater than), < (less than), ' (single quote)." |
| 41 |
required: true |
| 42 |
type: str |
| 43 |
|
| 44 |
description: |
| 45 |
description: |
| 46 |
- A user-defined description of the organization. |
| 47 |
- Enter up to 256 characters. |
| 48 |
- "You can use any characters or spaces except the following:" |
| 49 |
- "` (accent mark), \ (backslash), ^ (carat), \" (double quote)" |
| 50 |
- "= (equal sign), > (greater than), < (less than), ' (single quote)." |
| 51 |
aliases: [ descr ] |
| 52 |
type: str |
| 53 |
|
| 54 |
graphics_card_mode: |
| 55 |
description: |
| 56 |
- Set the Graphics Card Mode. |
| 57 |
choices: [any-configuration, compute, graphics] |
| 58 |
type: str |
| 59 |
|
| 60 |
org_dn: |
| 61 |
description: |
| 62 |
- Org dn (distinguished name) |
| 63 |
default: org-root |
| 64 |
type: str |
| 65 |
|
| 66 |
requirements: |
| 67 |
- ucsmsdk |
| 68 |
|
| 69 |
author: |
| 70 |
- John McDonough (@movinalot) |
| 71 |
''' |
| 72 |
|
| 73 |
EXAMPLES = r''' |
| 74 |
- name: Add UCS Graphics Card Policy |
| 75 |
cisco.ucs.ucs_graphics_card_policy: |
| 76 |
hostname: "{{ ucs_hostname }}" |
| 77 |
username: "{{ ucs_username }}" |
| 78 |
password: "{{ ucs_password }}" |
| 79 |
state: present |
| 80 |
description: Any Graphics Mode Policy |
| 81 |
name: any_graphics |
| 82 |
graphics_card_mode: any-configuration |
| 83 |
delegate_to: localhost |
| 84 |
|
| 85 |
- name: Add UCS Graphics Card Policy in an Organization |
| 86 |
cisco.ucs.ucs_graphics_card_policy: |
| 87 |
hostname: "{{ ucs_hostname }}" |
| 88 |
username: "{{ ucs_username }}" |
| 89 |
password: "{{ ucs_password }}" |
| 90 |
state: present |
| 91 |
org_dn: org-root/org-prod |
| 92 |
description: Any Graphics Mode Policy |
| 93 |
name: prod_graphics |
| 94 |
graphics_card_mode: any-configuration |
| 95 |
delegate_to: localhost |
| 96 |
|
| 97 |
- name: Update UCS Graphics Card Policy in an Organization |
| 98 |
cisco.ucs.ucs_graphics_card_policy: |
| 99 |
hostname: "{{ ucs_hostname }}" |
| 100 |
username: "{{ ucs_username }}" |
| 101 |
password: "{{ ucs_password }}" |
| 102 |
state: present |
| 103 |
org_dn: org-root/org-prod |
| 104 |
description: Graphics Mode Policy |
| 105 |
name: prod_graphics |
| 106 |
graphics_card_mode: graphics |
| 107 |
delegate_to: localhost |
| 108 |
|
| 109 |
- name: Update UCS Graphics Card Policy in an Organization |
| 110 |
cisco.ucs.ucs_graphics_card_policy: |
| 111 |
hostname: "{{ ucs_hostname }}" |
| 112 |
username: "{{ ucs_username }}" |
| 113 |
password: "{{ ucs_password }}" |
| 114 |
state: present |
| 115 |
org_dn: org-root/org-prod |
| 116 |
description: Compute Mode Policy |
| 117 |
name: prod_graphics |
| 118 |
graphics_card_mode: compute |
| 119 |
delegate_to: localhost |
| 120 |
|
| 121 |
- name: Delete UCS Graphics Card Policy in an Organization |
| 122 |
cisco.ucs.ucs_graphics_card_policy: |
| 123 |
hostname: "{{ ucs_hostname }}" |
| 124 |
username: "{{ ucs_username }}" |
| 125 |
password: "{{ ucs_password }}" |
| 126 |
state: absent |
| 127 |
org_dn: org-root/org-prod |
| 128 |
name: prod_graphics |
| 129 |
delegate_to: localhost |
| 130 |
|
| 131 |
- name: Delete UCS Graphics Card Policy |
| 132 |
cisco.ucs.ucs_graphics_card_policy: |
| 133 |
hostname: "{{ ucs_hostname }}" |
| 134 |
username: "{{ ucs_username }}" |
| 135 |
password: "{{ ucs_password }}" |
| 136 |
state: absent |
| 137 |
name: any_graphics |
| 138 |
delegate_to: localhost |
| 139 |
''' |
| 140 |
|
| 141 |
RETURN = r''' |
| 142 |
# |
| 143 |
''' |
| 144 |
|
| 145 |
from ansible.module_utils.basic import AnsibleModule |
| 146 |
from ansible_collections.cisco.ucs.plugins.module_utils.ucs import ( |
| 147 |
UCSModule, |
| 148 |
ucs_argument_spec |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
def main(): |
| 153 |
argument_spec = ucs_argument_spec.copy() |
| 154 |
argument_spec.update( |
| 155 |
org_dn=dict(type='str', default='org-root'), |
| 156 |
name=dict(required=True, type='str'), |
| 157 |
description=dict(type='str', aliases=['descr']), |
| 158 |
graphics_card_mode=dict(type='str', choices=[ |
| 159 |
'any-configuration', |
| 160 |
'compute', |
| 161 |
'graphics' |
| 162 |
]), |
| 163 |
state=dict( |
| 164 |
type='str', default='present', |
| 165 |
choices=['present', 'absent'] |
| 166 |
), |
| 167 |
) |
| 168 |
|
| 169 |
module = AnsibleModule( |
| 170 |
argument_spec, |
| 171 |
supports_check_mode=True, |
| 172 |
required_if=[ |
| 173 |
['state', 'present', ['name']], |
| 174 |
], |
| 175 |
) |
| 176 |
|
| 177 |
# UCSModule verifies ucsmsdk is present and exits on failure. |
| 178 |
# Imports are below for UCS object creation. |
| 179 |
ucs = UCSModule(module) |
| 180 |
from importlib import import_module |
| 181 |
from ucsmsdk.ucscoreutils import get_meta_info |
| 182 |
|
| 183 |
# The Class(es) this module is managing |
| 184 |
module_file = 'ucsmsdk.mometa.compute.ComputeGraphicsCardPolicy' |
| 185 |
module_class = 'ComputeGraphicsCardPolicy' |
| 186 |
mo_module = import_module(module_file) |
| 187 |
mo_class = getattr(mo_module, module_class) |
| 188 |
|
| 189 |
META = get_meta_info(class_id=module_class) |
| 190 |
|
| 191 |
err = False |
| 192 |
changed = False |
| 193 |
requested_state = module.params['state'] |
| 194 |
|
| 195 |
kwargs = dict() |
| 196 |
|
| 197 |
# Manage Attributes |
| 198 |
for attribute in [ |
| 199 |
'graphics_card_mode', 'description']: |
| 200 |
if module.params[attribute] is not None: |
| 201 |
kwargs[attribute] = module.params[attribute] |
| 202 |
|
| 203 |
try: |
| 204 |
dn = ( |
| 205 |
module.params['org_dn'] + '/' + |
| 206 |
META.rn[0:META.rn.rindex('-') + 1] + |
| 207 |
module.params['name'] |
| 208 |
) |
| 209 |
mo = ucs.login_handle.query_dn(dn) |
| 210 |
|
| 211 |
# Determine state change |
| 212 |
if mo: |
| 213 |
# Object exists, if it should exist has anything changed? |
| 214 |
if requested_state == 'present': |
| 215 |
# Do some or all Object properties not match, that is a change |
| 216 |
|
| 217 |
if not mo.check_prop_match(**kwargs): |
| 218 |
changed = True |
| 219 |
|
| 220 |
# Object does not exist but should, that is a change |
| 221 |
else: |
| 222 |
if requested_state == 'present': |
| 223 |
changed = True |
| 224 |
|
| 225 |
# Object exists but should not, that is a change |
| 226 |
if mo and requested_state == 'absent': |
| 227 |
changed = True |
| 228 |
|
| 229 |
# Apply state if not check_mode |
| 230 |
if changed and not module.check_mode: |
| 231 |
if requested_state == 'absent': |
| 232 |
ucs.login_handle.remove_mo(mo) |
| 233 |
else: |
| 234 |
kwargs['parent_mo_or_dn'] = module.params['org_dn'] |
| 235 |
kwargs['name'] = module.params['name'] |
| 236 |
|
| 237 |
mo = mo_class(**kwargs) |
| 238 |
ucs.login_handle.add_mo(mo, modify_present=True) |
| 239 |
ucs.login_handle.commit() |
| 240 |
|
| 241 |
except Exception as e: |
| 242 |
err = True |
| 243 |
ucs.result['msg'] = "setup error: %s " % str(e) |
| 244 |
|
| 245 |
ucs.result['changed'] = changed |
| 246 |
if err: |
| 247 |
module.fail_json(**ucs.result) |
| 248 |
|
| 249 |
module.exit_json(**ucs.result) |
| 250 |
|
| 251 |
|
| 252 |
if __name__ == '__main__': |
| 253 |
main() |