Debian Codegraph @

ansible_collections/cisco/intersight/plugins/modules/intersight_kvm_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 __metaclass__ = type
8
9 ANSIBLE_METADATA = {'metadata_version': '1.1',
10 'status': ['preview'],
11 'supported_by': 'community'}
12
13 DOCUMENTATION = r'''
14 ---
15 module: intersight_kvm_policy
16 short_description: Virtual KVM Policy configuration for Cisco Intersight
17 description:
18 - Manages Virtual KVM Policy configuration on Cisco Intersight.
19 - A policy to configure virtual KVM settings on Cisco Intersight managed servers.
20 - When enabled, configures session limits, port settings, encryption, and tunneling options.
21 - When disabled, only the enabled state is configured while other settings are ignored.
22 - For more information see L(Cisco Intersight,https://intersight.com/apidocs/kvm/Policy/get/).
23 extends_documentation_fragment: intersight
24 options:
25 state:
26 description:
27 - If C(present), will verify the resource is present and will create if needed.
28 - If C(absent), will verify the resource is absent and will delete if needed.
29 type: str
30 choices: [present, absent]
31 default: present
32 organization:
33 description:
34 - The name of the Organization this resource is assigned to.
35 - Profiles, Policies, and Pools that are created within a Custom Organization are applicable only to devices in the same Organization.
36 type: str
37 default: default
38 name:
39 description:
40 - The name assigned to the KVM Policy.
41 - The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_.
42 type: str
43 required: true
44 description:
45 description:
46 - The user-defined description for the KVM Policy.
47 - Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_).
48 type: str
49 aliases: [descr]
50 tags:
51 description:
52 - List of tags in Key:<user-defined key> Value:<user-defined value> format.
53 type: list
54 elements: dict
55 enabled:
56 description:
57 - State of the vKVM service on the endpoint.
58 - When set to false, all other configuration parameters are ignored and only the enabled state is configured.
59 - When set to true, all other configuration parameters take effect.
60 type: bool
61 default: true
62 maximum_sessions:
63 description:
64 - The maximum number of concurrent KVM sessions allowed.
65 - Valid range is 1-4 sessions.
66 - This parameter is ignored when C(enabled) is C(false).
67 type: int
68 default: 4
69 remote_port:
70 description:
71 - The port used for KVM communication.
72 - Valid range is 1024-65535.
73 - This parameter is ignored when C(enabled) is C(false).
74 type: int
75 default: 2068
76 enable_video_encryption:
77 description:
78 - If enabled, encrypts all video information sent through KVM.
79 - Please note that this can no longer be disabled for servers running versions 4.2 and above.
80 - This parameter is ignored when C(enabled) is C(false).
81 type: bool
82 default: true
83 enable_local_server_video:
84 description:
85 - If enabled, displays KVM session on any monitor attached to the server.
86 - This parameter is ignored when C(enabled) is C(false).
87 type: bool
88 default: true
89 tunneled_kvm_enabled:
90 description:
91 - Enables Tunneled vKVM on the endpoint.
92 - Applicable only for Device Connectors that support Tunneled vKVM.
93 - This parameter is ignored when C(enabled) is C(false).
94 type: bool
95 default: false
96 author:
97 - Ron Gershburg (@rgershbu)
98 '''
99
100 EXAMPLES = r'''
101 - name: Create an enabled KVM Policy with default settings
102 cisco.intersight.intersight_kvm_policy:
103 api_private_key: "{{ api_private_key }}"
104 api_key_id: "{{ api_key_id }}"
105 name: "Default-KVM-Policy"
106 description: "KVM policy with standard settings"
107 enabled: true
108 tags:
109 - Key: "Environment"
110 Value: "Production"
111
112 - name: Create a KVM Policy with custom settings
113 cisco.intersight.intersight_kvm_policy:
114 api_private_key: "{{ api_private_key }}"
115 api_key_id: "{{ api_key_id }}"
116 name: "Custom-KVM-Policy"
117 description: "KVM policy with custom port and session limits"
118 enabled: true
119 maximum_sessions: 2
120 remote_port: 3000
121 enable_video_encryption: true
122 enable_local_server_video: false
123 tunneled_kvm_enabled: true
124 tags:
125 - Key: "Owner"
126 Value: "DevOps"
127
128 - name: Create a disabled KVM Policy
129 cisco.intersight.intersight_kvm_policy:
130 api_private_key: "{{ api_private_key }}"
131 api_key_id: "{{ api_key_id }}"
132 name: "Disabled-KVM-Policy"
133 description: "Disabled KVM policy for security compliance"
134 enabled: false
135 tags:
136 - Key: "Security"
137 Value: "Disabled"
138
139 - name: Delete a KVM Policy
140 cisco.intersight.intersight_kvm_policy:
141 api_private_key: "{{ api_private_key }}"
142 api_key_id: "{{ api_key_id }}"
143 name: "Old-KVM-Policy"
144 state: absent
145 '''
146
147 RETURN = r'''
148 api_response:
149 description: The API response output returned by the specified resource.
150 returned: always
151 type: dict
152 sample:
153 "api_response": {
154 "Name": "Custom-KVM-Policy",
155 "ObjectType": "kvm.Policy",
156 "Enabled": true,
157 "MaximumSessions": 2,
158 "RemotePort": 3000,
159 "EnableVideoEncryption": true,
160 "EnableLocalServerVideo": false,
161 "TunneledKvmEnabled": true,
162 "Tags": [
163 {
164 "Key": "Owner",
165 "Value": "DevOps"
166 }
167 ]
168 }
169 '''
170
171
172 from ansible.module_utils.basic import AnsibleModule
173 from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec
174
175
176 def validate_kvm_policy_params(module):
177 """
178 Validate KVM policy parameters
179 """
180 # Validate maximum_sessions range
181 max_sessions = module.params.get('maximum_sessions', 4)
182 if max_sessions < 1 or max_sessions > 4:
183 module.fail_json(msg="maximum_sessions must be between 1 and 4 (inclusive)")
184
185 # Validate remote_port range
186 remote_port = module.params.get('remote_port', 2068)
187 if remote_port < 1024 or remote_port > 65535:
188 module.fail_json(msg="remote_port must be between 1024 and 65535 (inclusive)")
189
190
191 def main():
192 argument_spec = intersight_argument_spec.copy()
193 argument_spec.update(
194 state=dict(type='str', choices=['present', 'absent'], default='present'),
195 organization=dict(type='str', default='default'),
196 name=dict(type='str', required=True),
197 description=dict(type='str', aliases=['descr']),
198 tags=dict(type='list', elements='dict'),
199 enabled=dict(type='bool', default=True),
200 maximum_sessions=dict(type='int', default=4),
201 remote_port=dict(type='int', default=2068),
202 enable_video_encryption=dict(type='bool', default=True),
203 enable_local_server_video=dict(type='bool', default=True),
204 tunneled_kvm_enabled=dict(type='bool', default=False)
205 )
206 module = AnsibleModule(
207 argument_spec,
208 supports_check_mode=True,
209 )
210
211 # Validate KVM policy specific parameters
212 validate_kvm_policy_params(module)
213
214 intersight = IntersightModule(module)
215 intersight.result['api_response'] = {}
216 intersight.result['trace_id'] = ''
217
218 # Resource path used to configure policy
219 resource_path = '/kvm/Policies'
220 # Define API body used in compares or create
221 intersight.api_body = {
222 'Organization': {
223 'Name': intersight.module.params['organization'],
224 },
225 'Name': intersight.module.params['name']
226 }
227
228 if module.params['state'] == 'present':
229 intersight.set_tags_and_description()
230 intersight.api_body.update({
231 'Enabled': intersight.module.params['enabled']
232 })
233 # Only add KVM-specific settings if enabled is True
234 if intersight.module.params['enabled']:
235 intersight.api_body.update({
236 'MaximumSessions': intersight.module.params['maximum_sessions'],
237 'RemotePort': intersight.module.params['remote_port'],
238 'EnableVideoEncryption': intersight.module.params['enable_video_encryption'],
239 'EnableLocalServerVideo': intersight.module.params['enable_local_server_video'],
240 'TunneledKvmEnabled': intersight.module.params['tunneled_kvm_enabled']
241 })
242
243 intersight.configure_policy_or_profile(resource_path=resource_path)
244
245 module.exit_json(**intersight.result)
246
247
248 if __name__ == '__main__':
249 main()