Browse code

initial commit of win_disk_image (#22175)

Matt Davis authored on 2017/03/02 09:31:01
Showing 3 changed files
... ...
@@ -262,6 +262,7 @@ Ansible Changes By Release
262 262
 - system
263 263
   * parted
264 264
 - windows:
265
+  * win_disk_image
265 266
   * win_dns_client
266 267
   * win_domain_membership
267 268
   * win_find
268 269
new file mode 100644
... ...
@@ -0,0 +1,91 @@
0
+#!powershell
1
+
2
+# (c) 2017, Red Hat, Inc.
3
+#
4
+# This file is part of Ansible
5
+#
6
+# Ansible is free software: you can redistribute it and/or modify
7
+# it under the terms of the GNU General Public License as published by
8
+# the Free Software Foundation, either version 3 of the License, or
9
+# (at your option) any later version.
10
+#
11
+# Ansible is distributed in the hope that it will be useful,
12
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+# GNU General Public License for more details.
15
+#
16
+# You should have received a copy of the GNU General Public License
17
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
18
+
19
+# POWERSHELL_COMMON
20
+# WANT_JSON
21
+
22
+$ErrorActionPreference = "Stop"
23
+Set-StrictMode -Version 2
24
+
25
+If(-not (Get-Command Get-DiskImage -ErrorAction SilentlyContinue)) {
26
+    Fail-Json -message "win_disk_image requires Windows 8+ or Windows Server 2012+"
27
+}
28
+
29
+$parsed_args = Parse-Args $args -supports_check_mode $true
30
+
31
+$result = @{changed=$false}
32
+
33
+$image_path = Get-AnsibleParam $parsed_args "image_path" -failifempty $result
34
+$state = Get-AnsibleParam $parsed_args "state" -default "present" -validateset "present","absent"
35
+$check_mode = Get-AnsibleParam $parsed_args "_ansible_check_mode" -default $false
36
+
37
+$di = Get-DiskImage $image_path
38
+
39
+If($state -eq "present") {
40
+    If(-not $di.Attached) {
41
+      $result.changed = $true
42
+
43
+      If(-not $check_mode) {
44
+        $di = Mount-DiskImage $image_path -PassThru
45
+
46
+        # the actual mount is async, so the CIMInstance result may not immediately contain the data we need
47
+        $retry_count = 0
48
+        While(-not $di.Attached -and $retry_count -lt 5) {
49
+          Sleep -Seconds 1 | Out-Null
50
+          $di = $di | Get-DiskImage
51
+          $retry_count++
52
+        }
53
+
54
+        If(-not $di.Attached) {
55
+          Fail-Json $result -message "Timed out waiting for disk to attach"
56
+        }
57
+     }
58
+  }
59
+
60
+  # FUTURE: detect/handle "ejected" ISOs
61
+  # FUTURE: support explicit drive letter and NTFS in-volume mountpoints.
62
+  # VHDs don't always auto-assign, and other system settings can prevent automatic assignment
63
+
64
+  If($di.Attached) { # only try to get the mount_path if the disk is attached (
65
+    If($di.StorageType -eq 1) { # ISO, we can get the mountpoint directly from Get-Volume
66
+      $drive_letter = ($di | Get-Volume).DriveLetter
67
+    }
68
+    ElseIf($di.StorageType -in @(2,3)) { # VHD/VHDX, need Get-Disk + Get-Partition to discover mountpoint
69
+      # FUTURE: support multi-partition VHDs
70
+      $drive_letter = ($di | Get-Disk | Get-Partition)[0].DriveLetter
71
+    }
72
+
73
+
74
+    If(-not $drive_letter) {
75
+      Fail-Json -message "Unable to retrieve drive letter from mounted image"
76
+    }
77
+
78
+    $result.mount_path = $drive_letter + ":\"
79
+  }
80
+}
81
+ElseIf($state -eq "absent") {
82
+  If($di.Attached) {
83
+    $result.changed = $true
84
+    If(-not $check_mode) {
85
+      Dismount-DiskImage $image_path | Out-Null
86
+    }
87
+  }
88
+}
89
+
90
+Exit-Json $result
0 91
\ No newline at end of file
1 92
new file mode 100644
... ...
@@ -0,0 +1,76 @@
0
+#!/usr/bin/python
1
+# -*- coding: utf-8 -*-
2
+
3
+# (c) 2017, Red Hat, Inc.
4
+#
5
+# This file is part of Ansible
6
+#
7
+# Ansible is free software: you can redistribute it and/or modify
8
+# it under the terms of the GNU General Public License as published by
9
+# the Free Software Foundation, either version 3 of the License, or
10
+# (at your option) any later version.
11
+#
12
+# Ansible is distributed in the hope that it will be useful,
13
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
+# GNU General Public License for more details.
16
+#
17
+# You should have received a copy of the GNU General Public License
18
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
19
+
20
+ANSIBLE_METADATA = {'status': ['preview'],
21
+                    'supported_by': 'core',
22
+                    'version': '1.0'}
23
+
24
+DOCUMENTATION='''
25
+module: win_disk_image
26
+short_description: Manage ISO/VHD/VHDX mounts on Windows hosts
27
+version_added: 2.3
28
+description:
29
+     - Manages mount behavior for a specified ISO, VHD, or VHDX image on a Windows host. When C(state) is C(present),
30
+       the image will be mounted under a system-assigned drive letter, which will be returned in the C(mount_path) value
31
+       of the module result. Requires Windows 8+ or Windows Server 2012+.
32
+options:
33
+  image_path:
34
+    description:
35
+      - path to an ISO, VHD, or VHDX image on the target Windows host (the file cannot reside on a network share)
36
+    required: true
37
+  state:
38
+    description:
39
+      - whether the image should be present as a drive-letter mount or not.
40
+    choices:
41
+      - present
42
+      - absent
43
+    default: present
44
+author:
45
+    - Matt Davis (@nitzmahone)
46
+'''
47
+
48
+RETURN=r'''
49
+mount_path:
50
+    description: filesystem path where the target image is mounted
51
+    returned: when C(state) is C(present)
52
+    type: string
53
+    sample: F:\
54
+'''
55
+
56
+EXAMPLES=r'''
57
+# ensure an iso is mounted
58
+- win_disk_image:
59
+    image_path: C:\install.iso
60
+    state: present
61
+  register: disk_image_out
62
+
63
+# run installer from mounted iso
64
+- win_package:
65
+    path: '{{ disk_image_out.mount_path }}setup\setup.exe'
66
+    product_id: '35a4e767-0161-46b0-979f-e61f282fee21'
67
+    state: present
68
+
69
+# unmount iso
70
+- win_disk_image:
71
+    image_path: C:\install.iso
72
+    state: absent
73
+
74
+'''
75
+