| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2019, Brant Evans <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 7 |
#AnsibleRequires -OSVersion 6.2 |
| 8 |
|
| 9 |
Set-StrictMode -Version 2 |
| 10 |
|
| 11 |
$spec = @{ |
| 12 |
options = @{ |
| 13 |
disk_number = @{ type = "int" } |
| 14 |
uniqueid = @{ type = "str" } |
| 15 |
path = @{ type = "str" } |
| 16 |
style = @{ type = "str"; choices = "gpt", "mbr"; default = "gpt" } |
| 17 |
online = @{ type = "bool"; default = $true } |
| 18 |
force = @{ type = "bool"; default = $false } |
| 19 |
} |
| 20 |
mutually_exclusive = @( |
| 21 |
, @('disk_number', 'uniqueid', 'path') |
| 22 |
) |
| 23 |
required_one_of = @( |
| 24 |
, @('disk_number', 'uniqueid', 'path') |
| 25 |
) |
| 26 |
supports_check_mode = $true |
| 27 |
} |
| 28 |
|
| 29 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 30 |
|
| 31 |
$disk_number = $module.Params.disk_number |
| 32 |
$uniqueid = $module.Params.uniqueid |
| 33 |
$path = $module.Params.path |
| 34 |
$partition_style = $module.Params.style |
| 35 |
$bring_online = $module.Params.online |
| 36 |
$force_init = $module.Params.force |
| 37 |
|
| 38 |
function Get-AnsibleDisk { |
| 39 |
param( |
| 40 |
$DiskNumber, |
| 41 |
$UniqueId, |
| 42 |
$Path |
| 43 |
) |
| 44 |
|
| 45 |
if ($null -ne $DiskNumber) { |
| 46 |
try { |
| 47 |
$disk = Get-Disk -Number $DiskNumber |
| 48 |
} |
| 49 |
catch { |
| 50 |
$module.FailJson("There was an error retrieving the disk using disk_number $($DiskNumber): $($_.Exception.Message)") |
| 51 |
} |
| 52 |
} |
| 53 |
elseif ($null -ne $UniqueId) { |
| 54 |
try { |
| 55 |
$disk = Get-Disk -UniqueId $UniqueId |
| 56 |
} |
| 57 |
catch { |
| 58 |
$module.FailJson("There was an error retrieving the disk using id $($UniqueId): $($_.Exception.Message)") |
| 59 |
} |
| 60 |
} |
| 61 |
elseif ($null -ne $Path) { |
| 62 |
try { |
| 63 |
$disk = Get-Disk -Path $Path |
| 64 |
} |
| 65 |
catch { |
| 66 |
$module.FailJson("There was an error retrieving the disk using path $($Path): $($_.Exception.Message)") |
| 67 |
} |
| 68 |
} |
| 69 |
else { |
| 70 |
$module.FailJson("Unable to retrieve disk: disk_number, id, or path was not specified") |
| 71 |
} |
| 72 |
|
| 73 |
return $disk |
| 74 |
} |
| 75 |
|
| 76 |
function Initialize-AnsibleDisk { |
| 77 |
param( |
| 78 |
$AnsibleDisk, |
| 79 |
$PartitionStyle |
| 80 |
) |
| 81 |
|
| 82 |
if ($AnsibleDisk.IsReadOnly) { |
| 83 |
$module.FailJson("Unable to initialize disk as it is read-only") |
| 84 |
} |
| 85 |
|
| 86 |
$parameters = @{ |
| 87 |
Number = $AnsibleDisk.Number |
| 88 |
PartitionStyle = $PartitionStyle |
| 89 |
} |
| 90 |
|
| 91 |
if ((-Not $module.CheckMode) -And -Not ("Offline" -eq $AnsibleDisk.OperationalStatus)) { |
| 92 |
Initialize-Disk @parameters -Confirm:$false |
| 93 |
} |
| 94 |
|
| 95 |
$module.Result.changed = $true |
| 96 |
} |
| 97 |
|
| 98 |
function Clear-AnsibleDisk { |
| 99 |
param( |
| 100 |
$AnsibleDisk |
| 101 |
) |
| 102 |
|
| 103 |
$parameters = @{ |
| 104 |
Number = $AnsibleDisk.Number |
| 105 |
} |
| 106 |
|
| 107 |
if (-Not $module.CheckMode) { |
| 108 |
Clear-Disk @parameters -RemoveData -RemoveOEM -Confirm:$false |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
function Set-AnsibleDisk { |
| 113 |
param( |
| 114 |
$AnsibleDisk, |
| 115 |
$BringOnline |
| 116 |
) |
| 117 |
|
| 118 |
$refresh_disk_status = $false |
| 119 |
|
| 120 |
if ($BringOnline) { |
| 121 |
if (-Not $module.CheckMode) { |
| 122 |
if ($AnsibleDisk.IsOffline) { |
| 123 |
Set-Disk -Number $AnsibleDisk.Number -IsOffline:$false |
| 124 |
$refresh_disk_status = $true |
| 125 |
} |
| 126 |
|
| 127 |
if ($AnsibleDisk.IsReadOnly) { |
| 128 |
Set-Disk -Number $AnsibleDisk.Number -IsReadOnly:$false |
| 129 |
$refresh_disk_status = $true |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if ($refresh_disk_status) { |
| 135 |
$AnsibleDisk = Get-AnsibleDisk -DiskNumber $AnsibleDisk.Number |
| 136 |
} |
| 137 |
|
| 138 |
return $AnsibleDisk |
| 139 |
} |
| 140 |
|
| 141 |
$ansible_disk = Get-AnsibleDisk -DiskNumber $disk_number -UniqueId $uniqueid -Path $path |
| 142 |
$ansible_part_style = $ansible_disk.PartitionStyle |
| 143 |
|
| 144 |
if (("RAW" -eq $ansible_part_style) -Or ("Offline" -eq $ansible_disk.OperationalStatus)) { |
| 145 |
$ansible_disk = Set-AnsibleDisk -AnsibleDisk $ansible_disk -BringOnline $bring_online |
| 146 |
Initialize-AnsibleDisk -AnsibleDisk $ansible_disk -PartitionStyle $partition_style |
| 147 |
} |
| 148 |
else { |
| 149 |
if (($ansible_part_style -ne $partition_style.ToUpper()) -And -Not $force_init) { |
| 150 |
$msg = -join @( |
| 151 |
"Force initialization must be specified since the target partition style: $($partition_style.ToLower()) " |
| 152 |
"is different from the current partition style: $($ansible_part_style.ToLower())" |
| 153 |
) |
| 154 |
$module.FailJson($msg) |
| 155 |
} |
| 156 |
elseif ($force_init) { |
| 157 |
$ansible_disk = Set-AnsibleDisk -AnsibleDisk $ansible_disk -BringOnline $bring_online |
| 158 |
Clear-AnsibleDisk -AnsibleDisk $ansible_disk |
| 159 |
if ( $bring_online ) { Initialize-AnsibleDisk -AnsibleDisk $ansible_disk -PartitionStyle $partition_style } |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
$module.ExitJson() |