Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_server_maintenance.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_server_maintenance
17 short_description: Creates Server Maintenance Policy on Cisco UCS Manager
18 description:
19 - Configures Server Maintenance Policy on Cisco UCS Manager.
20 extends_documentation_fragment: cisco.ucs.ucs
21 options:
22 state:
23 description:
24 - If C(present), will verify Server Maintenance Policy is present and will create if needed.
25 - If C(absent), will verify Server Maintenance Policy is absent and will delete if needed.
26 choices: [present, absent]
27 default: present
28 type: str
29 name:
30 description:
31 - The name assigned to the Server Maintenance Policy.
32 - The Server Maintenance Policy name is case sensitive.
33 - This name can be between 1 and 16 alphanumeric characters.
34 - "You cannot use spaces or any special characters other than - (hyphen), \"_\" (underscore), : (colon), and . (period)."
35 - You cannot change this name after the Server Maintenance Policy is created.
36 required: yes
37 type: str
38 description:
39 description:
40 - A description of the Server Maintenance Package Policy.
41 - Cisco recommends including information about where and when to use the policy.
42 - Enter up to 256 characters.
43 - "You can use any characters or spaces except the following:"
44 - "` (accent mark), \ (backslash), ^ (carat), \" (double quote), = (equal sign), > (greater than), < (less than), or ' (single quote)."
45 aliases: [ descr ]
46 type: str
47 trigger_config:
48 description:
49 - This option is used in combination with either User Ack (user-ack) or Timer Automatic (timer-automatic).
50 - When the On Next Boot option is enabled, the host OS reboot, shutdown, or server reset also triggers the associated FSM to apply the changes.
51 - Note that de-selecting the On Next Boot option disables the Maintenance Policy on the BMC.
52 choices: [on-next-boot]
53 type: str
54 uptime_disr:
55 description:
56 - When a Server profile is associated with a Server, or when changes are made to an associated Server profile, you must reboot the Server.
57 - The Reboot Policy field determines when the reboot occurs for Server associated with any Server profiles that include this maintenance policy.
58 choices: [immediate, timer-automatic, user-ack]
59 required: true
60 type: str
61 requirements:
62 - ucsmsdk
63 author:
64 - Brett Johnson (@brettjohnson008)
65 '''
66
67 EXAMPLES = r'''
68 - name: Add Server Maintenance Policy
69 cisco.ucs.ucs_server_maintenance:
70 hostname: 172.16.143.150
71 username: admin
72 password: password
73 name: user-ack
74 uptime_disr: user-ack
75 trigger_config: on-next-boot
76 '''
77
78 RETURN = r'''
79 #
80 '''
81
82 from ansible.module_utils.basic import AnsibleModule
83 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import UCSModule, ucs_argument_spec
84
85
86 def main():
87 argument_spec = ucs_argument_spec.copy()
88 argument_spec.update(
89 name=dict(type='str', required=True),
90 description=dict(type='str', aliases=['descr']),
91 trigger_config=dict(type='str', choices=['on-next-boot']),
92 uptime_disr=dict(type='str', required=True, choices=['immediate', 'timer-automatic', 'user-ack']),
93 state=dict(type='str', default='present', choices=['present', 'absent']),
94 )
95
96 module = AnsibleModule(
97 argument_spec,
98 supports_check_mode=True,
99 )
100
101 ucs = UCSModule(module)
102
103 err = False
104
105 # UCSModule creation above verifies ucsmsdk is present and exits on failure, so additional imports are done below.
106 from ucsmsdk.mometa.lsmaint.LsmaintMaintPolicy import LsmaintMaintPolicy
107
108 changed = False
109 try:
110 mo_exists = False
111 props_match = False
112 dn_base = 'org-root'
113 dn = dn_base + '/maint-' + module.params['name']
114
115 mo = ucs.login_handle.query_dn(dn)
116 if mo:
117 mo_exists = True
118
119 if module.params['state'] == 'absent':
120 # mo must exist but all properties do not have to match
121 if mo_exists:
122 if not module.check_mode:
123 ucs.login_handle.remove_mo(mo)
124 ucs.login_handle.commit()
125 changed = True
126 else:
127 if mo_exists:
128 # check top-level mo props
129 kwargs = dict(name=module.params['name'])
130 kwargs['descr'] = module.params['description']
131 kwargs['trigger_config'] = module.params['trigger_config']
132 kwargs['uptime_disr'] = module.params['uptime_disr']
133 if mo.check_prop_match(**kwargs):
134 props_match = True
135
136 if not props_match:
137 if not module.check_mode:
138 # create if mo does not already exist
139 mo = LsmaintMaintPolicy(
140 parent_mo_or_dn=dn_base,
141 name=module.params['name'],
142 descr=module.params['description'],
143 trigger_config=module.params['trigger_config'],
144 uptime_disr=module.params['uptime_disr'],
145 )
146
147 ucs.login_handle.add_mo(mo, True)
148 ucs.login_handle.commit()
149 changed = True
150
151 except Exception as e:
152 err = True
153 ucs.result['msg'] = "setup error: %s " % str(e)
154
155 ucs.result['changed'] = changed
156 if err:
157 module.fail_json(**ucs.result)
158 module.exit_json(**ucs.result)
159
160
161 if __name__ == '__main__':
162 main()