Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_scrub_policy.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
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_scrub_policy
17
18 short_description: Manages UCS Scrub Policies on UCS Manager
19
20 description:
21 - Manages UCS Scrub 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 bios_settings_scrub:
55 description:
56 - Scrub the BIOS settings.
57 - If the field is set to Yes, when a service profile containing this
58 - scrub policy is disassociated from a server, the BIOS settings for
59 - that server are erased and reset to the defaults for that server
60 - type and vendor. If this field is set to No, the BIOS settings are
61 - preserved.
62 - yes scrub the BIOS settings.
63 - no do not scrub the BIOS settings.
64 choices: ['yes', 'no']
65 type: str
66
67 disk_scrub:
68 description:
69 - Scrub the BIOS settings.
70 - If this field is set to Yes, when a service profile containing this
71 - scrub policy is disassociated from a server, all data on the server
72 - local drives is completely erased. If this field is set to No, the
73 - data on the local drives is preserved, including all local storage
74 - configuration.
75 - yes scrub the server disks.
76 - no do not scrub the server disks.
77 choices: ['yes', 'no']
78 type: str
79
80 flex_flash_scrub:
81 description:
82 - Scrub the BIOS settings.
83 - If the field is set to Yes, the HV partition on the SD card is
84 - formatted using the PNUOS formatting utility when the server is
85 - reacknowledged. If this field is set to No, the SD card is preserved.
86 - yes scrub the flex flash.
87 - no do not scrub the flex flash.
88 choices: ['yes', 'no']
89 type: str
90
91 persistent_memory_scrub:
92 description:
93 - Scrub the BIOS settings.
94 - If the field is set to Yes, when a service profile containing this
95 - scrub policy is disassociated from a server, all persistent memory
96 - modules for that server are erased and reset to the defaults for that
97 - server type and vendor. If this field is set to No, the persistent
98 - memory modules are preserved.
99 - yes scrub the persistent memory.
100 - no do not scrub the persistent memory.
101 choices: ['yes', 'no']
102 type: str
103
104 org_dn:
105 description:
106 - Org dn (distinguished name)
107 default: org-root
108 type: str
109
110 requirements:
111 - ucsmsdk
112
113 author:
114 - John McDonough (@movinalot)
115 '''
116
117 EXAMPLES = r'''
118 - name: Add UCS Scrub Policy
119 cisco.ucs.ucs_scrub_policy:
120 hostname: "{{ ucs_hostname }}"
121 username: "{{ ucs_username }}"
122 password: "{{ ucs_password }}"
123 state: present
124 description: Scrub All Policy
125 name: all_scrub
126 bios_settings_scrub: yes
127 disk_scrub: yes
128 flex_flash_scrub: yes
129 persistent_memory_scrub: yes
130 delegate_to: localhost
131
132 - name: Add UCS Scrub Policy in an Organization
133 cisco.ucs.ucs_scrub_policy:
134 hostname: "{{ ucs_hostname }}"
135 username: "{{ ucs_username }}"
136 password: "{{ ucs_password }}"
137 state: present
138 org_dn: org-root/org-prod
139 name: all_scrub
140 description: Scrub All Policy Org Prod servers
141 bios_settings_scrub: yes
142 disk_scrub: yes
143 flex_flash_scrub: yes
144 persistent_memory_scrub: yes
145 delegate_to: localhost
146
147 - name: Update UCS Scrub Policy
148 cisco.ucs.ucs_scrub_policy:
149 hostname: "{{ ucs_hostname }}"
150 username: "{{ ucs_username }}"
151 password: "{{ ucs_password }}"
152 state: present
153 org_dn: org-root/org-prod
154 name: BD_scrub
155 description: Scrub BIOS and Disk Policy Org Prod servers
156 bios_settings_scrub: yes
157 disk_scrub: yes
158 flex_flash_scrub: no
159 persistent_memory_scrub: no
160 delegate_to: localhost
161
162 - name: Update UCS Scrub Policy
163 cisco.ucs.ucs_scrub_policy:
164 hostname: "{{ ucs_hostname }}"
165 username: "{{ ucs_username }}"
166 password: "{{ ucs_password }}"
167 state: present
168 org_dn: org-root/org-prod
169 name: BD_scrub
170 description: Scrub BIOS and Disk Policy Org Prod servers
171 bios_settings_scrub: yes
172 disk_scrub: yes
173 flex_flash_scrub: yes
174 delegate_to: localhost
175
176 - name: Delete UCS Scrub Policy
177 cisco.ucs.ucs_scrub_policy:
178 hostname: "{{ ucs_hostname }}"
179 username: "{{ ucs_username }}"
180 password: "{{ ucs_password }}"
181 state: absent
182 org_dn: org-root/org-prod
183 name: BD_scrub
184 delegate_to: localhost
185
186 - name: Delete UCS Scrub Policy
187 cisco.ucs.ucs_scrub_policy:
188 hostname: "{{ ucs_hostname }}"
189 username: "{{ ucs_username }}"
190 password: "{{ ucs_password }}"
191 state: absent
192 name: BD_scrub
193 delegate_to: localhost
194 '''
195
196 RETURN = r'''
197 #
198 '''
199
200 from ansible.module_utils.basic import AnsibleModule
201 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import (
202 UCSModule,
203 ucs_argument_spec
204 )
205
206
207 def main():
208 argument_spec = ucs_argument_spec.copy()
209 argument_spec.update(
210 org_dn=dict(type='str', default='org-root'),
211 name=dict(required=True, type='str'),
212 description=dict(type='str', aliases=['descr']),
213 bios_settings_scrub=dict(type='str', choices=['yes', 'no']),
214 disk_scrub=dict(type='str', choices=['yes', 'no']),
215 flex_flash_scrub=dict(type='str', choices=['yes', 'no']),
216 persistent_memory_scrub=dict(type='str', choices=['yes', 'no']),
217 state=dict(
218 type='str', default='present',
219 choices=['present', 'absent']
220 ),
221 )
222
223 module = AnsibleModule(
224 argument_spec,
225 supports_check_mode=True,
226 required_if=[
227 ['state', 'present', ['name']],
228 ],
229 )
230
231 # UCSModule verifies ucsmsdk is present and exits on failure.
232 # Imports are below for UCS object creation.
233 ucs = UCSModule(module)
234 from importlib import import_module
235 from ucsmsdk.ucscoreutils import get_meta_info
236
237 # The Class(es) this module is managing
238 module_file = 'ucsmsdk.mometa.compute.ComputeScrubPolicy'
239 module_class = 'ComputeScrubPolicy'
240 mo_module = import_module(module_file)
241 mo_class = getattr(mo_module, module_class)
242
243 META = get_meta_info(class_id=module_class)
244
245 err = False
246 changed = False
247 requested_state = module.params['state']
248
249 kwargs = dict()
250
251 # Manage Attributes
252 for attribute in [
253 'bios_settings_scrub',
254 'disk_scrub',
255 'flex_flash_scrub',
256 'persistent_memory_scrub'
257 ]:
258 if module.params[attribute] is not None:
259 kwargs[attribute] = module.params[attribute]
260
261 kwargs['descr'] = module.params['description']
262
263 try:
264 dn = (
265 module.params['org_dn'] + '/' +
266 META.rn[0:META.rn.index('-') + 1] +
267 module.params['name']
268 )
269 mo = ucs.login_handle.query_dn(dn)
270
271 # Determine state change
272 if mo:
273 # Object exists, if it should exist has anything changed?
274 if requested_state == 'present':
275 # Do some or all Object properties not match, that is a change
276
277 if not mo.check_prop_match(**kwargs):
278 changed = True
279
280 # Object does not exist but should, that is a change
281 else:
282 if requested_state == 'present':
283 changed = True
284
285 # Object exists but should not, that is a change
286 if mo and requested_state == 'absent':
287 changed = True
288
289 # Apply state if not check_mode
290 if changed and not module.check_mode:
291 if requested_state == 'absent':
292 ucs.login_handle.remove_mo(mo)
293 else:
294 kwargs['parent_mo_or_dn'] = module.params['org_dn']
295 kwargs['name'] = module.params['name']
296
297 mo = mo_class(**kwargs)
298 ucs.login_handle.add_mo(mo, modify_present=True)
299 ucs.login_handle.commit()
300
301 except Exception as e:
302 err = True
303 ucs.result['msg'] = "setup error: %s " % str(e)
304
305 ucs.result['changed'] = changed
306 if err:
307 module.fail_json(**ucs.result)
308
309 module.exit_json(**ucs.result)
310
311
312 if __name__ == '__main__':
313 main()