Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_service_profile_association.py

raw
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_association
16 short_description: Configures Service Profile Association on Cisco UCS Manager
17 description:
18 - Configures Service Profile Association (change association or disassociate) on Cisco UCS Manager.
19 extends_documentation_fragment: cisco.ucs.ucs
20 options:
21 state:
22 description:
23 - If C(present), will verify service profile association and associate with specified server or server pool if needed.
24 - If C(absent), will verify service profile is not associated and will disassociate if needed. This is the same as specifying Assign Later in the webUI.
25 choices: [present, absent]
26 default: present
27 type: str
28 service_profile_name:
29 description:
30 - The name of the Service Profile being associated or disassociated.
31 required: true
32 type: str
33 server_assignment:
34 description:
35 - "Specifies how to associate servers with this service profile using the following choices:"
36 - "server - Use to pre-provision a slot or select an existing server. Slot or server is specified by the server_dn option."
37 - "pool - Use to select from a server pool. The server_pool option specifies the name of the server pool to use."
38 - Option is not valid if the service profile is bound to a template.
39 - Optional if the state is absent.
40 choices: [server, pool]
41 type: str
42 server_dn:
43 description:
44 - The Distinguished Name (dn) of the server object used for pre-provisioning or selecting an existing server.
45 - Required if the server_assignment option is server.
46 - Optional if the state is absent.
47 type: str
48 server_pool_name:
49 description:
50 - Name of the server pool used for server pool based assignment.
51 - Required if the server_assignment option is pool.
52 - Optional if the state is absent.
53 type: str
54 restrict_migration:
55 description:
56 - Restricts the migration of the service profile after it has been associated with a server.
57 - If set to no, Cisco UCS Manager does not perform any compatibility checks on the new server before migrating the existing service profile.
58 - If set to no and the hardware of both servers used in migration are not similar, the association might fail.
59 choices: ['yes', 'no']
60 default: 'no'
61 type: str
62 org_dn:
63 description:
64 - The distinguished name (dn) of the organization where the resource is assigned.
65 default: org-root
66 type: str
67 requirements:
68 - ucsmsdk
69 author:
70 - David Soper (@dsoper2)
71 - CiscoUcs (@CiscoUcs)
72 '''
73
74 EXAMPLES = r'''
75 - name: Change Service Profile Association to server pool Container-Pool and restrict migration
76 cisco.ucs.ucs_service_profile_association:
77 hostname: 172.16.143.150
78 username: admin
79 password: password
80 service_profile_name: test-sp
81 server_assignment: pool
82 server_pool_name: Container-Pool
83 restrict_migration: 'yes'
84
85 - name: Attempt to change association once a minute for up to 10 minutes
86 cisco.ucs.ucs_service_profile_association:
87 hostname: 172.16.143.150
88 username: admin
89 password: password
90 service_profile_name: test-sp
91 server_assignment: server
92 server_dn: sys/chassis-2/blade-1
93 register: result
94 until: result.assign_state == 'assigned' and result.assoc_state == 'associated'
95 retries: 10
96 delay: 60
97
98 - name: Disassociate Service Profile
99 cisco.ucs.ucs_service_profile_association:
100 hostname: 172.16.143.150
101 username: admin
102 password: password
103 service_profile_name: test-sp
104 state: absent
105 '''
106
107 RETURN = r'''
108 assign_state:
109 description: The logical server Assigned State (assigned, unassigned, or failed).
110 returned: success
111 type: str
112 sample: assigned
113 assoc_state:
114 description: The logical server Association State (associated or unassociated).
115 returned: success
116 type: str
117 sample: associated
118 '''
119
120 from ansible.module_utils.basic import AnsibleModule
121 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
122
123
124 def main():
125 argument_spec = ucs_argument_spec.copy()
126 argument_spec.update(
127 org_dn=dict(type='str', default='org-root'),
128 service_profile_name=dict(type='str', required=True),
129 server_assignment=dict(type='str', choices=['server', 'pool']),
130 server_dn=dict(type='str'),
131 server_pool_name=dict(type='str'),
132 restrict_migration=dict(type='str', default='no', choices=['yes', 'no']),
133 state=dict(default='present', choices=['present', 'absent'], type='str'),
134 )
135 module = AnsibleModule(
136 argument_spec,
137 supports_check_mode=True,
138 required_if=[
139 ['state', 'present', ['server_assignment']],
140 ['server_assignment', 'server', ['server_dn']],
141 ['server_assignment', 'pool', ['server_pool_name']],
142 ],
143 mutually_exclusive=[
144 ['server_dn', 'server_pool_name'],
145 ],
146 )
147 # UCSModule verifies ucsmsdk is present and exits on failure. Imports are below ucs object creation.
148 ucs = UCSModule(module)
149
150 err = False
151
152 from ucsmsdk.mometa.ls.LsRequirement import LsRequirement
153 from ucsmsdk.mometa.ls.LsBinding import LsBinding
154 from ucsmsdk.mometa.ls.LsServer import LsServer
155
156 changed = False
157 ucs.result['assign_state'] = 'unassigned'
158 ucs.result['assoc_state'] = 'unassociated'
159 try:
160 ls_mo_exists = False
161 pn_mo_exists = False
162 pn_req_mo_exists = False
163 props_match = False
164
165 # logical server distinguished name is <org>/ls-<name> and physical node dn appends 'pn' or 'pn-req'
166 ls_dn = module.params['org_dn'] + '/ls-' + module.params['service_profile_name']
167 ls_mo = ucs.login_handle.query_dn(ls_dn)
168 if ls_mo:
169 ls_mo_exists = True
170 pn_dn = ls_dn + '/pn'
171 pn_mo = ucs.login_handle.query_dn(pn_dn)
172 if pn_mo:
173 pn_mo_exists = True
174
175 pn_req_dn = ls_dn + '/pn-req'
176 pn_req_mo = ucs.login_handle.query_dn(pn_req_dn)
177 if pn_req_mo:
178 pn_req_mo_exists = True
179
180 if module.params['state'] == 'absent':
181 if ls_mo_exists and ls_mo.assign_state != 'unassigned':
182 if pn_mo_exists:
183 if not module.check_mode:
184 ucs.login_handle.remove_mo(pn_mo)
185 ucs.login_handle.commit()
186 changed = True
187 elif pn_req_mo_exists:
188 if not module.check_mode:
189 ucs.login_handle.remove_mo(pn_req_mo)
190 ucs.login_handle.commit()
191 changed = True
192 elif ls_mo_exists:
193 # check if logical server is assigned and associated
194 ucs.result['assign_state'] = ls_mo.assign_state
195 ucs.result['assoc_state'] = ls_mo.assoc_state
196 if module.params['server_assignment'] == 'pool' and pn_req_mo_exists:
197 # check the current pool
198 kwargs = dict(name=module.params['server_pool_name'])
199 kwargs['restrict_migration'] = module.params['restrict_migration']
200 if pn_req_mo.check_prop_match(**kwargs):
201 props_match = True
202 elif pn_mo_exists:
203 kwargs = dict(pn_dn=module.params['server_dn'])
204 kwargs['restrict_migration'] = module.params['restrict_migration']
205 if pn_mo.check_prop_match(**kwargs):
206 props_match = True
207
208 if not props_match:
209 if not module.check_mode:
210 # create if mo does not already exist in desired state
211 mo = LsServer(
212 parent_mo_or_dn=module.params['org_dn'],
213 name=module.params['service_profile_name'],
214 )
215 if module.params['server_assignment'] == 'pool':
216 if pn_mo_exists:
217 ucs.login_handle.remove_mo(pn_mo)
218
219 mo_1 = LsRequirement(
220 parent_mo_or_dn=mo,
221 name=module.params['server_pool_name'],
222 restrict_migration=module.params['restrict_migration'],
223 )
224 else:
225 mo_1 = LsBinding(
226 parent_mo_or_dn=mo,
227 pn_dn=module.params['server_dn'],
228 restrict_migration=module.params['restrict_migration'],
229 )
230 ucs.login_handle.add_mo(mo_1, True)
231 ucs.login_handle.commit()
232
233 pn_req_mo = ucs.login_handle.query_dn(pn_req_dn)
234 if pn_req_mo:
235 # profiles from templates will add a server pool, so remove and add the server again
236 ucs.login_handle.remove_mo(pn_req_mo)
237
238 ucs.login_handle.add_mo(mo_1, True)
239 ucs.login_handle.commit()
240 ls_mo = ucs.login_handle.query_dn(ls_dn)
241 if ls_mo:
242 ucs.result['assign_state'] = ls_mo.assign_state
243 ucs.result['assoc_state'] = ls_mo.assoc_state
244 changed = True
245
246 except Exception as e:
247 err = True
248 ucs.result['msg'] = "setup error: %s " % str(e)
249
250 ucs.result['changed'] = changed
251 if err:
252 module.fail_json(**ucs.result)
253 module.exit_json(**ucs.result)
254
255
256 if __name__ == '__main__':
257 main()