Debian Codegraph @

ansible_collections/cisco/ucs/plugins/modules/ucs_serial_over_lan_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_serial_over_lan_policy
17
18 short_description: Manages UCS Serial Over Lan Policies on UCS Manager
19
20 description:
21 - Manages UCS Serial Over Lan 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 Serial Over Lan Policy.
29 - If C(present), will create or update Serial Over Lan Policy.
30 choices: [absent, present]
31 default: present
32 type: str
33
34 name:
35 description:
36 - The name of the serial over lan policy.
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 serial over lan policy.
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 admin_state:
55 description:
56 - The administrative state of the serial over lan policy.
57 - disable Serial over LAN access is blocked.
58 - enable Serial over LAN access is permitted.
59 choices: [disable, enable]
60 type: str
61
62 speed:
63 description:
64 - The transmission speed of the serial over lan policy.
65 choices: ['9600', '19200', '38400', '57600', '115200']
66 type: str
67
68 org_dn:
69 description:
70 - Org dn (distinguished name) of the serial over lan policy.
71 default: org-root
72 type: str
73
74 requirements:
75 - ucsmsdk
76
77 author:
78 - John McDonough (@movinalot)
79 '''
80
81 EXAMPLES = r'''
82 - name: Add UCS Serial Over Lan Policy
83 cisco.ucs.ucs_serial_over_lan:
84 hostname: "{{ ucs_hostname }}"
85 username: "{{ ucs_username }}"
86 password: "{{ ucs_password }}"
87 state: present
88 name: sol_org_root
89 description: Serial Over Lan for Org root servers
90 admin_state: enable
91 speed: 115200
92 delegate_to: localhost
93
94 - name: Add UCS Serial Over Lan Policy in Organization
95 cisco.ucs.ucs_serial_over_lan:
96 hostname: "{{ ucs_hostname }}"
97 username: "{{ ucs_username }}"
98 password: "{{ ucs_password }}"
99 state: present
100 org_dn: org-root/org-prod
101 name: sol_org_prod
102 description: Serial Over Lan for Org Prod servers
103 admin_state: enable
104 speed: 115200
105 delegate_to: localhost
106
107 - name: Update UCS Serial Over Lan Policy in Organization
108 cisco.ucs.ucs_serial_over_lan:
109 hostname: "{{ ucs_hostname }}"
110 username: "{{ ucs_username }}"
111 password: "{{ ucs_password }}"
112 state: present
113 org_dn: org-root/org-prod
114 name: sol_org_prod
115 description: Serial Over Lan for Org Prod servers
116 admin_state: enable
117 speed: 38400
118 delegate_to: localhost
119
120 - name: Update UCS Serial Over Lan Policy in Organization
121 cisco.ucs.ucs_serial_over_lan:
122 hostname: "{{ ucs_hostname }}"
123 username: "{{ ucs_username }}"
124 password: "{{ ucs_password }}"
125 state: present
126 org_dn: org-root/org-prod
127 name: sol_org_prod
128 descr: Serial Over Lan for Org Prod servers
129 admin_state: enable
130 speed: 57600
131 delegate_to: localhost
132
133 - name: Delete UCS Serial Over Lan Policy in Organization
134 cisco.ucs.ucs_serial_over_lan:
135 hostname: "{{ ucs_hostname }}"
136 username: "{{ ucs_username }}"
137 password: "{{ ucs_password }}"
138 state: absent
139 org_dn: org-root/org-prod
140 name: sol_org_prod
141 delegate_to: localhost
142
143 - name: Delete UCS Serial Over Lan Policy
144 cisco.ucs.ucs_serial_over_lan:
145 hostname: "{{ ucs_hostname }}"
146 username: "{{ ucs_username }}"
147 password: "{{ ucs_password }}"
148 state: absent
149 name: sol_org_root
150 delegate_to: localhost
151 '''
152
153 RETURN = r'''
154 #
155 '''
156
157 from ansible.module_utils.basic import AnsibleModule
158 from ansible_collections.cisco.ucs.plugins.module_utils.ucs import (
159 UCSModule,
160 ucs_argument_spec
161 )
162
163
164 def main():
165 argument_spec = ucs_argument_spec.copy()
166 argument_spec.update(
167 org_dn=dict(type='str', default='org-root'),
168 name=dict(required=True, type='str'),
169 description=dict(type='str', aliases=['descr']),
170 admin_state=dict(type='str', choices=['enable', 'disable']),
171 speed=dict(type='str', choices=[
172 '9600', '19200', '38400', '57600', '115200'
173 ]),
174 state=dict(
175 type='str', default='present',
176 choices=['present', 'absent']
177 ),
178 )
179
180 module = AnsibleModule(
181 argument_spec,
182 supports_check_mode=True,
183 required_if=[
184 ['state', 'present', ['name']],
185 ],
186 )
187
188 # UCSModule verifies ucsmsdk is present and exits on failure.
189 # Imports are below for UCS object creation.
190 ucs = UCSModule(module)
191 from importlib import import_module
192 from ucsmsdk.ucscoreutils import get_meta_info
193
194 # The Class(es) this module is managing
195 module_file = 'ucsmsdk.mometa.sol.SolPolicy'
196 module_class = 'SolPolicy'
197 mo_module = import_module(module_file)
198 mo_class = getattr(mo_module, module_class)
199
200 META = get_meta_info(class_id=module_class)
201
202 err = False
203 changed = False
204 requested_state = module.params['state']
205
206 kwargs = dict()
207
208 # Manage Attributes
209 for attribute in ['admin_state', 'speed']:
210 if module.params[attribute] is not None:
211 kwargs[attribute] = module.params[attribute]
212
213 kwargs['descr'] = module.params['description']
214
215 try:
216 dn = (
217 module.params['org_dn'] + '/' +
218 META.rn[0:META.rn.index('-') + 1] +
219 module.params['name']
220 )
221 mo = ucs.login_handle.query_dn(dn)
222
223 # Determine state change
224 if mo:
225 # Object exists, if it should exist has anything changed?
226 if requested_state == 'present':
227 # Do some or all Object properties not match, that is a change
228
229 if not mo.check_prop_match(**kwargs):
230 changed = True
231
232 # Object does not exist but should, that is a change
233 else:
234 if requested_state == 'present':
235 changed = True
236
237 # Object exists but should not, that is a change
238 if mo and requested_state == 'absent':
239 changed = True
240
241 # Apply state if not check_mode
242 if changed and not module.check_mode:
243 if requested_state == 'absent':
244 ucs.login_handle.remove_mo(mo)
245 else:
246 kwargs['parent_mo_or_dn'] = module.params['org_dn']
247 kwargs['name'] = module.params['name']
248
249 mo = mo_class(**kwargs)
250 ucs.login_handle.add_mo(mo, modify_present=True)
251 ucs.login_handle.commit()
252
253 except Exception as e:
254 err = True
255 ucs.result['msg'] = "setup error: %s " % str(e)
256
257 ucs.result['changed'] = changed
258 if err:
259 module.fail_json(**ucs.result)
260
261 module.exit_json(**ucs.result)
262
263
264 if __name__ == '__main__':
265 main()