| 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_virtual_media_policy |
| 16 |
short_description: Virtual Media policy configuration for Cisco Intersight |
| 17 |
description: |
| 18 |
- Virtual Media policy configuration for Cisco Intersight. |
| 19 |
- Used to configure Virtual Media image mappings on Cisco Intersight managed devices. |
| 20 |
- For more information see L(Cisco Intersight,https://intersight.com/apidocs). |
| 21 |
extends_documentation_fragment: intersight |
| 22 |
options: |
| 23 |
state: |
| 24 |
description: |
| 25 |
- If C(present), will verify the resource is present and will create if needed. |
| 26 |
- If C(absent), will verify the resource is absent and will delete if needed. |
| 27 |
type: str |
| 28 |
choices: [present, absent] |
| 29 |
default: present |
| 30 |
organization: |
| 31 |
description: |
| 32 |
- The name of the Organization this resource is assigned to. |
| 33 |
- Profiles and Policies that are created within a Custom Organization are applicable only to devices in the same Organization. |
| 34 |
type: str |
| 35 |
default: default |
| 36 |
name: |
| 37 |
description: |
| 38 |
- The name assigned to the NTP policy. |
| 39 |
- The name must be between 1 and 62 alphanumeric characters, allowing special characters :-_. |
| 40 |
type: str |
| 41 |
required: true |
| 42 |
tags: |
| 43 |
description: |
| 44 |
- List of tags in Key:<user-defined key> Value:<user-defined value> format. |
| 45 |
type: list |
| 46 |
elements: dict |
| 47 |
description: |
| 48 |
description: |
| 49 |
- The user-defined description of the NTP policy. |
| 50 |
- Description can contain letters(a-z, A-Z), numbers(0-9), hyphen(-), period(.), colon(:), or an underscore(_). |
| 51 |
type: str |
| 52 |
aliases: [descr] |
| 53 |
enable: |
| 54 |
description: |
| 55 |
- Enable or disable virtual media. |
| 56 |
type: bool |
| 57 |
default: true |
| 58 |
encryption: |
| 59 |
description: |
| 60 |
- If enabled, allows encryption of all Virtual Media communications |
| 61 |
type: bool |
| 62 |
default: false |
| 63 |
low_power_usb: |
| 64 |
description: |
| 65 |
- If enabled, the virtual drives appear on the boot selection menu after mapping the image and rebooting the host. |
| 66 |
type: bool |
| 67 |
default: true |
| 68 |
cdd_virtual_media: |
| 69 |
description: |
| 70 |
- CDD Virtual Media image mapping options. |
| 71 |
type: dict |
| 72 |
suboptions: |
| 73 |
enable: |
| 74 |
description: |
| 75 |
- Enable or disable CDD image mapping. |
| 76 |
type: bool |
| 77 |
default: true |
| 78 |
mount_type: |
| 79 |
description: |
| 80 |
- Type (protocol) of network share used by the remote_hostname. |
| 81 |
- Ensure that the remote_hostname's communication port for the mount type that you choose is accessible from the managed endpoint. |
| 82 |
- For CIFS as your mount type, ensure port 445 (which is its communication port) on the remote_hostname is accessible. |
| 83 |
- For HTTP, ensure port 80 is accessible. |
| 84 |
- For HTTPS, ensure port 443 is accessible. |
| 85 |
- For NFS, ensure port 2049 is accessible. |
| 86 |
type: str |
| 87 |
choices: [nfs,cifs,http,https] |
| 88 |
required: true |
| 89 |
volume: |
| 90 |
description: |
| 91 |
- A user defined name of the image mounted for mapping. |
| 92 |
type: str |
| 93 |
required: true |
| 94 |
remote_hostname: |
| 95 |
description: |
| 96 |
- Hostname or IP address of the server hosting the virtual media image. |
| 97 |
type: str |
| 98 |
required: true |
| 99 |
remote_path: |
| 100 |
description: |
| 101 |
- Filepath (not including the filename) of the remote image. |
| 102 |
- Ex. mnt/SHARE/ISOS |
| 103 |
type: str |
| 104 |
required: true |
| 105 |
remote_file: |
| 106 |
description: |
| 107 |
- Filename of the remote image. |
| 108 |
- Ex. custom_image.iso |
| 109 |
type: str |
| 110 |
required: true |
| 111 |
username: |
| 112 |
description: |
| 113 |
- The username for the specified Mount Type, if required. |
| 114 |
type: str |
| 115 |
password: |
| 116 |
description: |
| 117 |
- The password for the selected username, if required. |
| 118 |
type: str |
| 119 |
mount_options: |
| 120 |
description: |
| 121 |
- Mount options for the Virtual Media mapping. |
| 122 |
- For NFS, supported options are ro, rw, nolock, noexec, soft, port=VALUE, timeo=VALUE, retry=VALUE |
| 123 |
- For CIFS, supported options are soft, nounix, noserverino, guest |
| 124 |
type: str |
| 125 |
required: false |
| 126 |
authentication_protocol: |
| 127 |
description: |
| 128 |
- Authentication Protocol for CIFS Mount Type |
| 129 |
type: str |
| 130 |
default: none |
| 131 |
required: false |
| 132 |
hdd_virtual_media: |
| 133 |
description: |
| 134 |
- HDD Virtual Media image mapping options. |
| 135 |
type: dict |
| 136 |
suboptions: |
| 137 |
enable: |
| 138 |
description: |
| 139 |
- Enable or disable HDD image mapping. |
| 140 |
type: bool |
| 141 |
default: true |
| 142 |
mount_type: |
| 143 |
description: |
| 144 |
- Type (protocol) of network share used by the remote_hostname. |
| 145 |
- Ensure that the remote_hostname's communication port for the mount type that you choose is accessible from the managed endpoint. |
| 146 |
- For CIFS as your mount type, ensure port 445 (which is its communication port) on the remote_hostname is accessible. |
| 147 |
- For HTTP, ensure port 80 is accessible. |
| 148 |
- For HTTPS, ensure port 443 is accessible. |
| 149 |
- For NFS, ensure port 2049 is accessible. |
| 150 |
type: str |
| 151 |
choices: [nfs,cifs,http,https] |
| 152 |
required: true |
| 153 |
volume: |
| 154 |
description: |
| 155 |
- A user defined name of the image mounted for mapping. |
| 156 |
type: str |
| 157 |
required: true |
| 158 |
remote_hostname: |
| 159 |
description: |
| 160 |
- Hostname or IP address of the server hosting the virtual media image. |
| 161 |
type: str |
| 162 |
required: true |
| 163 |
remote_path: |
| 164 |
description: |
| 165 |
- Filepath (not including the filename) of the remote image. |
| 166 |
- Ex. mnt/SHARE/ISOS |
| 167 |
type: str |
| 168 |
required: true |
| 169 |
remote_file: |
| 170 |
description: |
| 171 |
- Filename of the remote image. |
| 172 |
- Ex. custom_image.iso |
| 173 |
type: str |
| 174 |
required: true |
| 175 |
username: |
| 176 |
description: |
| 177 |
- The username for the specified Mount Type, if required. |
| 178 |
type: str |
| 179 |
password: |
| 180 |
description: |
| 181 |
- The password for the selected username, if required. |
| 182 |
type: str |
| 183 |
mount_options: |
| 184 |
description: |
| 185 |
- Mount options for the Virtual Media mapping. |
| 186 |
- For NFS, supported options are ro, rw, nolock, noexec, soft, port=VALUE, timeo=VALUE, retry=VALUE |
| 187 |
- For CIFS, supported options are soft, nounix, noserverino, guest |
| 188 |
type: str |
| 189 |
required: false |
| 190 |
authentication_protocol: |
| 191 |
description: |
| 192 |
- Authentication Protocol for CIFS Mount Type |
| 193 |
type: str |
| 194 |
default: none |
| 195 |
required: false |
| 196 |
author: |
| 197 |
- David Soper (@dsoper2) |
| 198 |
- Sid Nath (@SidNath21) |
| 199 |
''' |
| 200 |
|
| 201 |
EXAMPLES = r''' |
| 202 |
- name: Configure Virtual Media Policy |
| 203 |
cisco.intersight.intersight_virtual_media_policy: |
| 204 |
api_private_key: "{{ api_private_key }}" |
| 205 |
api_key_id: "{{ api_key_id }}" |
| 206 |
organization: DevNet |
| 207 |
name: lab-vmedia |
| 208 |
description: Virutal Media policy for lab use |
| 209 |
tags: |
| 210 |
- Key: Site |
| 211 |
Value: RCDN |
| 212 |
cdd_virtual_media: |
| 213 |
mount_type: nfs |
| 214 |
volume: nfs-cdd |
| 215 |
remote_hostname: 172.28.224.77 |
| 216 |
remote_path: mnt/SHARE/ISOS/CENTOS |
| 217 |
remote_file: CentOS7.iso |
| 218 |
hdd_virtual_media: |
| 219 |
mount_type: nfs |
| 220 |
volume: nfs-hdd |
| 221 |
remote_hostname: 172.28.224.77 |
| 222 |
remote_path: mnt/SHARE/ISOS/CENTOS |
| 223 |
remote_file: CentOS7.iso |
| 224 |
|
| 225 |
- name: Delete Virtual Media Policy |
| 226 |
cisco.intersight.intersight_virtual_media_policy: |
| 227 |
api_private_key: "{{ api_private_key }}" |
| 228 |
api_key_id: "{{ api_key_id }}" |
| 229 |
organization: DevNet |
| 230 |
name: lab-vmedia |
| 231 |
state: absent |
| 232 |
''' |
| 233 |
|
| 234 |
RETURN = r''' |
| 235 |
api_response: |
| 236 |
description: The API response output returned by the specified resource. |
| 237 |
returned: always |
| 238 |
type: dict |
| 239 |
sample: |
| 240 |
"api_response": { |
| 241 |
"Name": "lab-ntp", |
| 242 |
"ObjectType": "ntp.Policy", |
| 243 |
"Tags": [ |
| 244 |
{ |
| 245 |
"Key": "Site", |
| 246 |
"Value": "RCDN" |
| 247 |
} |
| 248 |
] |
| 249 |
} |
| 250 |
''' |
| 251 |
|
| 252 |
|
| 253 |
from ansible.module_utils.basic import AnsibleModule |
| 254 |
from ansible_collections.cisco.intersight.plugins.module_utils.intersight import IntersightModule, intersight_argument_spec, compare_values |
| 255 |
|
| 256 |
|
| 257 |
def main(): |
| 258 |
path = '/vmedia/Policies' |
| 259 |
virtual_media_mapping = dict( |
| 260 |
enable=dict(type='bool', default=True), |
| 261 |
mount_type=dict(type='str', choices=['nfs', 'cifs', 'http', 'https'], required=True), |
| 262 |
volume=dict(type='str', required=True), |
| 263 |
remote_hostname=dict(type='str', required=True), |
| 264 |
remote_path=dict(type='str', required=True), |
| 265 |
remote_file=dict(type='str', required=True), |
| 266 |
mount_options=dict(type='str'), |
| 267 |
username=dict(type='str'), |
| 268 |
password=dict(type='str', no_log=True), |
| 269 |
authentication_protocol=dict(type='str', default='none'), |
| 270 |
) |
| 271 |
argument_spec = intersight_argument_spec.copy() |
| 272 |
argument_spec.update( |
| 273 |
state=dict(type='str', choices=['present', 'absent'], default='present'), |
| 274 |
organization=dict(type='str', default='default'), |
| 275 |
name=dict(type='str', required=True), |
| 276 |
description=dict(type='str', aliases=['descr']), |
| 277 |
tags=dict(type='list', elements='dict'), |
| 278 |
enable=dict(type='bool', default=True), |
| 279 |
encryption=dict(type='bool', default=False), |
| 280 |
low_power_usb=dict(type='bool', default=True), |
| 281 |
cdd_virtual_media=dict(type='dict', options=virtual_media_mapping), |
| 282 |
hdd_virtual_media=dict(type='dict', options=virtual_media_mapping), |
| 283 |
) |
| 284 |
|
| 285 |
module = AnsibleModule( |
| 286 |
argument_spec, |
| 287 |
supports_check_mode=True, |
| 288 |
) |
| 289 |
|
| 290 |
intersight = IntersightModule(module) |
| 291 |
intersight.result['api_response'] = {} |
| 292 |
intersight.result['trace_id'] = '' |
| 293 |
# Defined API body used in compares or create |
| 294 |
intersight.api_body = { |
| 295 |
'Organization': { |
| 296 |
'Name': intersight.module.params['organization'], |
| 297 |
}, |
| 298 |
'Name': intersight.module.params['name'], |
| 299 |
'Tags': intersight.module.params['tags'], |
| 300 |
'Description': intersight.module.params['description'], |
| 301 |
'Enabled': intersight.module.params['enable'], |
| 302 |
"Encryption": intersight.module.params['encryption'], |
| 303 |
"LowPowerUsb": intersight.module.params['low_power_usb'], |
| 304 |
'Mappings': [], |
| 305 |
} |
| 306 |
|
| 307 |
if intersight.module.params.get('cdd_virtual_media'): |
| 308 |
intersight.api_body['Mappings'].append( |
| 309 |
{ |
| 310 |
"ClassId": "vmedia.Mapping", |
| 311 |
"ObjectType": "vmedia.Mapping", |
| 312 |
"AuthenticationProtocol": intersight.module.params['cdd_virtual_media']['authentication_protocol'], |
| 313 |
"DeviceType": "cdd", |
| 314 |
"HostName": intersight.module.params['cdd_virtual_media']['remote_hostname'], |
| 315 |
"Password": intersight.module.params['cdd_virtual_media']['password'], |
| 316 |
"IsPasswordSet": intersight.module.params['cdd_virtual_media']['password'] != '', |
| 317 |
"MountOptions": intersight.module.params['cdd_virtual_media']['mount_options'], |
| 318 |
"MountProtocol": intersight.module.params['cdd_virtual_media']['mount_type'], |
| 319 |
"RemoteFile": intersight.module.params['cdd_virtual_media']['remote_file'], |
| 320 |
"RemotePath": intersight.module.params['cdd_virtual_media']['remote_path'], |
| 321 |
"Username": intersight.module.params['cdd_virtual_media']['username'], |
| 322 |
"VolumeName": intersight.module.params['cdd_virtual_media']['volume'], |
| 323 |
} |
| 324 |
) |
| 325 |
if intersight.module.params.get('hdd_virtual_media'): |
| 326 |
intersight.api_body['Mappings'].append( |
| 327 |
{ |
| 328 |
"ClassId": "vmedia.Mapping", |
| 329 |
"ObjectType": "vmedia.Mapping", |
| 330 |
"AuthenticationProtocol": intersight.module.params['hdd_virtual_media']['authentication_protocol'], |
| 331 |
"DeviceType": "hdd", |
| 332 |
"HostName": intersight.module.params['hdd_virtual_media']['remote_hostname'], |
| 333 |
"Password": intersight.module.params['hdd_virtual_media']['password'], |
| 334 |
"IsPasswordSet": intersight.module.params['hdd_virtual_media']['password'] != '', |
| 335 |
"MountOptions": intersight.module.params['hdd_virtual_media']['mount_options'], |
| 336 |
"MountProtocol": intersight.module.params['hdd_virtual_media']['mount_type'], |
| 337 |
"RemoteFile": intersight.module.params['hdd_virtual_media']['remote_file'], |
| 338 |
"RemotePath": intersight.module.params['hdd_virtual_media']['remote_path'], |
| 339 |
"Username": intersight.module.params['hdd_virtual_media']['username'], |
| 340 |
"VolumeName": intersight.module.params['hdd_virtual_media']['volume'], |
| 341 |
} |
| 342 |
) |
| 343 |
|
| 344 |
organization_moid = None |
| 345 |
# GET Organization Moid |
| 346 |
intersight.get_resource( |
| 347 |
resource_path='/organization/Organizations', |
| 348 |
query_params={ |
| 349 |
'$filter': "Name eq '" + intersight.module.params['organization'] + "'", |
| 350 |
'$select': 'Moid', |
| 351 |
}, |
| 352 |
) |
| 353 |
if intersight.result['api_response'].get('Moid'): |
| 354 |
# resource exists and moid was returned |
| 355 |
organization_moid = intersight.result['api_response']['Moid'] |
| 356 |
|
| 357 |
intersight.result['api_response'] = {} |
| 358 |
# get the current state of the resource |
| 359 |
filter_str = "Name eq '" + intersight.module.params['name'] + "'" |
| 360 |
filter_str += "and Organization.Moid eq '" + organization_moid + "'" |
| 361 |
intersight.get_resource( |
| 362 |
resource_path=path, |
| 363 |
query_params={ |
| 364 |
'$filter': filter_str, |
| 365 |
'$expand': 'Organization', |
| 366 |
}, |
| 367 |
) |
| 368 |
|
| 369 |
moid = None |
| 370 |
resource_values_match = False |
| 371 |
if intersight.result['api_response'].get('Moid'): |
| 372 |
# resource exists and moid was returned |
| 373 |
moid = intersight.result['api_response']['Moid'] |
| 374 |
if module.params['state'] == 'present': |
| 375 |
resource_values_match = compare_values(intersight.api_body, intersight.result['api_response']) |
| 376 |
else: # state == 'absent' |
| 377 |
intersight.delete_resource( |
| 378 |
moid=moid, |
| 379 |
resource_path=path, |
| 380 |
) |
| 381 |
moid = None |
| 382 |
|
| 383 |
if module.params['state'] == 'present' and not resource_values_match: |
| 384 |
# remove read-only Organization key |
| 385 |
intersight.api_body.pop('Organization') |
| 386 |
if not moid: |
| 387 |
# Organization must be set, but can't be changed after initial POST |
| 388 |
intersight.api_body['Organization'] = { |
| 389 |
'Moid': organization_moid, |
| 390 |
} |
| 391 |
intersight.configure_resource( |
| 392 |
moid=moid, |
| 393 |
resource_path=path, |
| 394 |
body=intersight.api_body, |
| 395 |
query_params={ |
| 396 |
'$filter': filter_str, |
| 397 |
}, |
| 398 |
) |
| 399 |
|
| 400 |
module.exit_json(**intersight.result) |
| 401 |
|
| 402 |
|
| 403 |
if __name__ == '__main__': |
| 404 |
main() |