| 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_service_profile_from_template |
| 16 |
short_description: Configures Service Profiles from templates on Cisco UCS Manager |
| 17 |
description: |
| 18 |
- Configures Service Profile created from templates on Cisco UCS Manager. |
| 19 |
extends_documentation_fragment: cisco.ucs.ucs |
| 20 |
options: |
| 21 |
state: |
| 22 |
description: |
| 23 |
- If C(present), will verify Service Profiles are present and will create if needed. |
| 24 |
- If C(absent), will verify Service Profiles are absent and will delete if needed. |
| 25 |
choices: [present, absent] |
| 26 |
default: present |
| 27 |
type: str |
| 28 |
name: |
| 29 |
description: |
| 30 |
- The name of the service profile. |
| 31 |
- This name can be between 2 and 32 alphanumeric characters. |
| 32 |
- "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)." |
| 33 |
- This name must be unique across all service profiles and service profile templates within the same organization. |
| 34 |
required: true |
| 35 |
type: str |
| 36 |
source_template: |
| 37 |
description: |
| 38 |
- The name of the service profile template used to create this serivce profile. |
| 39 |
required: true |
| 40 |
type: str |
| 41 |
power_state: |
| 42 |
description: |
| 43 |
- The power state to be applied when this service profile is associated with a server. |
| 44 |
- If no value is provided, the power_state for the service profile will not be modified. |
| 45 |
choices: [up, down] |
| 46 |
type: str |
| 47 |
user_label: |
| 48 |
description: |
| 49 |
- The User Label you want to assign to this service profile. |
| 50 |
type: str |
| 51 |
org_dn: |
| 52 |
description: |
| 53 |
- Org dn (distinguished name) |
| 54 |
default: org-root |
| 55 |
type: str |
| 56 |
description: |
| 57 |
description: |
| 58 |
- Optional |
| 59 |
- The Description of the service profile |
| 60 |
type: str |
| 61 |
requirements: |
| 62 |
- ucsmsdk |
| 63 |
author: |
| 64 |
- David Soper (@dsoper2) |
| 65 |
- CiscoUcs (@CiscoUcs) |
| 66 |
''' |
| 67 |
|
| 68 |
EXAMPLES = r''' |
| 69 |
- name: Configure Service Profile from Template |
| 70 |
cisco.ucs.ucs_service_profile_from_template: |
| 71 |
hostname: 172.16.143.150 |
| 72 |
username: admin |
| 73 |
password: password |
| 74 |
name: test-sp-instance1 |
| 75 |
source_template: test-sp |
| 76 |
discription: Created from Ansible |
| 77 |
|
| 78 |
- name: Remove Service Profile |
| 79 |
cisco.ucs.ucs_service_profile_from_template: |
| 80 |
hostname: 172.16.143.150 |
| 81 |
username: admin |
| 82 |
password: password |
| 83 |
name: test-sp-instance1 |
| 84 |
state: absent |
| 85 |
''' |
| 86 |
|
| 87 |
RETURN = r''' |
| 88 |
# |
| 89 |
''' |
| 90 |
|
| 91 |
from ansible.module_utils.basic import AnsibleModule |
| 92 |
from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec |
| 93 |
|
| 94 |
|
| 95 |
def main(): |
| 96 |
argument_spec = ucs_argument_spec.copy() |
| 97 |
argument_spec.update( |
| 98 |
org_dn=dict(type='str', default='org-root'), |
| 99 |
name=dict(type='str', required=True), |
| 100 |
source_template=dict(type='str', required=True), |
| 101 |
user_label=dict(type='str'), |
| 102 |
power_state=dict(type='str', choices=['up', 'down']), |
| 103 |
state=dict(type='str', default='present', choices=['present', 'absent']), |
| 104 |
description=dict(type='str'), |
| 105 |
) |
| 106 |
|
| 107 |
module = AnsibleModule( |
| 108 |
argument_spec, |
| 109 |
supports_check_mode=True, |
| 110 |
) |
| 111 |
ucs = UCSModule(module) |
| 112 |
|
| 113 |
err = False |
| 114 |
|
| 115 |
# UCSModule creation above verifies ucsmsdk is present and exits on failure. Additional imports are done below. |
| 116 |
from ucsmsdk.mometa.ls.LsServer import LsServer |
| 117 |
from ucsmsdk.mometa.ls.LsPower import LsPower |
| 118 |
|
| 119 |
changed = False |
| 120 |
try: |
| 121 |
mo_exists = False |
| 122 |
props_match = False |
| 123 |
dn = module.params['org_dn'] + '/ls-' + module.params['name'] |
| 124 |
|
| 125 |
mo = ucs.login_handle.query_dn(dn) |
| 126 |
if mo: |
| 127 |
mo_exists = True |
| 128 |
|
| 129 |
if module.params['state'] == 'absent': |
| 130 |
# mo must exist but all properties do not have to match |
| 131 |
if mo_exists: |
| 132 |
if not module.check_mode: |
| 133 |
ucs.login_handle.remove_mo(mo) |
| 134 |
ucs.login_handle.commit() |
| 135 |
changed = True |
| 136 |
else: |
| 137 |
if mo_exists: |
| 138 |
# check top-level mo props |
| 139 |
kwargs = dict(src_templ_name=module.params['source_template']) |
| 140 |
kwargs['usr_lbl'] = module.params['user_label'] |
| 141 |
# service profiles are of type 'instance' |
| 142 |
kwargs['type'] = 'instance' |
| 143 |
|
| 144 |
if mo.check_prop_match(**kwargs): |
| 145 |
# top-level props match |
| 146 |
if module.params.get('power_state'): |
| 147 |
child_dn = dn + '/power' |
| 148 |
mo_1 = ucs.login_handle.query_dn(child_dn) |
| 149 |
if mo_1: |
| 150 |
kwargs = dict(state=module.params['power_state']) |
| 151 |
if mo_1.check_prop_match(**kwargs): |
| 152 |
props_match = True |
| 153 |
else: |
| 154 |
# no power state provided, use existing state as match |
| 155 |
props_match = True |
| 156 |
|
| 157 |
if not props_match: |
| 158 |
if not module.check_mode: |
| 159 |
# create if mo does not already exist |
| 160 |
mo = LsServer( |
| 161 |
parent_mo_or_dn=module.params['org_dn'], |
| 162 |
name=module.params['name'], |
| 163 |
src_templ_name=module.params['source_template'], |
| 164 |
type='instance', |
| 165 |
usr_lbl=module.params['user_label'], |
| 166 |
descr=module.params['description'], |
| 167 |
) |
| 168 |
if module.params.get('power_state'): |
| 169 |
admin_state = 'admin-' + module.params['power_state'] |
| 170 |
mo_1 = LsPower( |
| 171 |
parent_mo_or_dn=mo, |
| 172 |
state=admin_state, |
| 173 |
) |
| 174 |
|
| 175 |
ucs.login_handle.add_mo(mo, True) |
| 176 |
ucs.login_handle.commit() |
| 177 |
changed = True |
| 178 |
|
| 179 |
except Exception as e: |
| 180 |
err = True |
| 181 |
ucs.result['msg'] = "setup error: %s " % str(e) |
| 182 |
|
| 183 |
ucs.result['changed'] = changed |
| 184 |
if err: |
| 185 |
module.fail_json(**ucs.result) |
| 186 |
module.exit_json(**ucs.result) |
| 187 |
|
| 188 |
|
| 189 |
if __name__ == '__main__': |
| 190 |
main() |