Update module to use Ansible.Basic (#51365)

pull/51452/head
Varun Chopra 6 years ago committed by Jordan Borean
parent eebece91b1
commit 19441df7e9

@ -1,33 +1,45 @@
#!powershell #!powershell
# Copyright: (c) 2018, Varun Chopra (@chopraaa) # Copyright: (c) 2018, Varun Chopra (@chopraaa) <v@chopraaa.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#Requires -Module Ansible.ModuleUtils.Legacy #AnsibleRequires -CSharpUtil Ansible.Basic
#AnsibleRequires -OSVersion 6.2 #AnsibleRequires -OSVersion 6.2
Set-StrictMode -Version 2 Set-StrictMode -Version 2
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
$params = Parse-Args -arguments $args -supports_check_mode $true $spec = @{
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false options = @{
state = @{ type = "str"; choices = "absent", "present"; default = "present" }
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -failifempty $false -default "present" -validateset "absent", "present" drive_letter = @{ type = "str" }
$drive_letter = Get-AnsibleParam -obj $params -name "drive_letter" -type "str" -failifempty $false disk_number = @{ type = "int" }
$disk_number = Get-AnsibleParam -obj $params -name "disk_number" -type "int" -failifempty $false partition_number = @{ type = "int" }
$partition_number = Get-AnsibleParam -obj $params -name "partition_number" -type "int" -failifempty $false partition_size = @{ type = "str" }
$partition_size = Get-AnsibleParam -obj $params -name "partition_size" -type "str" -failifempty $false read_only = @{ type = "bool" }
$read_only = Get-AnsibleParam -obj $params -name "read_only" -type "bool" -failifempty $false active = @{ type = "bool" }
$active = Get-AnsibleParam -obj $params -name "active" -type "bool" -failifempty $false hidden = @{ type = "bool" }
$hidden = Get-AnsibleParam -obj $params -name "hidden" -type "bool" -failifempty $false offline = @{ type = "bool" }
$offline = Get-AnsibleParam -obj $params -name "offline" -type "bool" -failifempty $false mbr_type = @{ type = "str"; choices = "fat12", "fat16", "extended", "huge", "ifs", "fat32" }
$mbr_type = Get-AnsibleParam -obj $params -name "mbr_type" -type "str" -failifempty $false -validateset "fat12", "fat16", "extended", "huge", "ifs", "fat32" gpt_type = @{ type = "str"; choices = "system_partition", "microsoft_reserved", "basic_data", "microsoft_recovery" }
$gpt_type = Get-AnsibleParam -obj $params -name "gpt_type" -type "str" -failifempty $false -validateset "system_partition", "microsoft_reserved", "basic_data", "microsoft_recovery" }
supports_check_mode = $true
$result = @{ }
changed = $false
} $module = [Ansible.Basic.AnsibleModule]::Create($args, $spec)
$state = $module.Params.state
$drive_letter = $module.Params.drive_letter
$disk_number = $module.Params.disk_number
$partition_number = $module.Params.partition_number
$partition_size = $module.Params.partition_size
$read_only = $module.Params.read_only
$active = $module.Params.active
$hidden = $module.Params.hidden
$offline = $module.Params.offline
$mbr_type = $module.Params.mbr_type
$gpt_type = $module.Params.gpt_type
$size_is_maximum = $false $size_is_maximum = $false
$ansible_partition = $false $ansible_partition = $false
@ -77,7 +89,7 @@ if ($null -ne $partition_size) {
$ansible_partition_size = Convert-SizeToBytes -Size $Matches.Size -Units $Matches.Units $ansible_partition_size = Convert-SizeToBytes -Size $Matches.Size -Units $Matches.Units
} }
else { else {
Fail-Json -obj $result -message "Invalid partition size. B, KB, KiB, MB, MiB, GB, GiB, TB, TiB are valid partition size units" $module.FailJson("Invalid partition size. B, KB, KiB, MB, MiB, GB, GiB, TB, TiB are valid partition size units")
} }
} }
@ -91,23 +103,23 @@ elseif ($drive_letter -and -not ($disk_number -and $partition_number)) {
$ansible_partition = Get-Partition -DriveLetter $drive_letter -ErrorAction SilentlyContinue $ansible_partition = Get-Partition -DriveLetter $drive_letter -ErrorAction SilentlyContinue
} }
else { else {
Fail-Json -obj $result -message "Incorrect usage of drive_letter: specify a drive letter from A-Z or use 'auto' to automatically assign a drive letter" $module.FailJson("Incorrect usage of drive_letter: specify a drive letter from A-Z or use 'auto' to automatically assign a drive letter")
} }
} }
elseif ($disk_number) { elseif ($disk_number) {
try { try {
Get-Disk -Number $disk_number | Out-Null Get-Disk -Number $disk_number | Out-Null
} catch { } catch {
Fail-Json -obj $result -message "Specified disk does not exist" $module.FailJson("Specified disk does not exist")
} }
} }
else { else {
Fail-Json -obj $result -message "You must provide disk_number, partition_number" $module.FailJson("You must provide disk_number, partition_number")
} }
# Partition can't have two partition styles # Partition can't have two partition styles
if ($null -ne $gpt_type -and $null -ne $mbr_type) { if ($null -ne $gpt_type -and $null -ne $mbr_type) {
Fail-Json "Cannot specify both GPT and MBR parititon styles. Check which partition style is supported by the disk" $module.FailJson("Cannot specify both GPT and MBR partition styles. Check which partition style is supported by the disk")
} }
function New-AnsiblePartition { function New-AnsiblePartition {
@ -156,7 +168,7 @@ function New-AnsiblePartition {
try { try {
$new_partition = New-Partition @parameters $new_partition = New-Partition @parameters
} catch { } catch {
Fail-Json -obj $result -message "Unable to create a new partition: $($_.Exception.Message)" $module.FailJson("Unable to create a new partition: $($_.Exception.Message)", $_)
} }
return $new_partition return $new_partition
@ -191,7 +203,7 @@ function Set-AnsiblePartitionState {
try { try {
Set-Partition @parameters Set-Partition @parameters
} catch { } catch {
Fail-Json -obj $result -message "Error changing state of partition: $($_.Exception.Message)" $module.FailJson("Error changing state of partition: $($_.Exception.Message)", $_)
} }
} }
@ -199,75 +211,75 @@ function Set-AnsiblePartitionState {
if ($ansible_partition) { if ($ansible_partition) {
if ($state -eq "absent") { if ($state -eq "absent") {
try { try {
Remove-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -Confirm:$false -WhatIf:$check_mode Remove-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -Confirm:$false -WhatIf:$module.CheckMode
} catch { } catch {
Fail-Json -obj $result -message "There was an error removing the partition: $($_.Exception.Message)" $module.FailJson("There was an error removing the partition: $($_.Exception.Message)", $_)
} }
$result.changed = $true $module.Result.changed = $true
} }
else { else {
if ($null -ne $gpt_type -and $gpt_styles.$gpt_type -ne $partition.GptType) { if ($null -ne $gpt_type -and $gpt_styles.$gpt_type -ne $partition.GptType) {
Fail-Json -obj $result -message "gpt_type is not a valid parameter for existing partitions" $module.FailJson("gpt_type is not a valid parameter for existing partitions")
} }
if ($null -ne $mbr_type -and $mbr_styles.$mbr_type -ne $partition.MbrType) { if ($null -ne $mbr_type -and $mbr_styles.$mbr_type -ne $partition.MbrType) {
Fail-Json -obj $result -message "mbr_type is not a valid parameter for existing partitions" $module.FailJson("mbr_type is not a valid parameter for existing partitions")
} }
if ($partition_size) { if ($partition_size) {
try { try {
$max_supported_size = (Get-PartitionSupportedSize -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber).SizeMax $max_supported_size = (Get-PartitionSupportedSize -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber).SizeMax
} catch { } catch {
Fail-Json -obj $result -message "Unable to get maximum supported partition size: $($_.Exception.Message)" $module.FailJson("Unable to get maximum supported partition size: $($_.Exception.Message)", $_)
} }
if ($size_is_maximum) { if ($size_is_maximum) {
$ansible_partition_size = $max_supported_size $ansible_partition_size = $max_supported_size
} }
if ($ansible_partition_size -ne $ansible_partition.Size -and $ansible_partition_size -le $max_supported_size) { if ($ansible_partition_size -ne $ansible_partition.Size -and $ansible_partition_size -le $max_supported_size) {
if ($ansible_partition.IsReadOnly) { if ($ansible_partition.IsReadOnly) {
Fail-Json -obj $result -message "Unable to resize partition: Partition is read only" $module.FailJson("Unable to resize partition: Partition is read only")
} else { } else {
try { try {
Resize-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -Size $ansible_partition_size -WhatIf:$check_mode Resize-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -Size $ansible_partition_size -WhatIf:$module.CheckMode
} catch { } catch {
Fail-Json -obj $result -message "Unable to change partition size: $($_.Exception.Message)" $module.FailJson("Unable to change partition size: $($_.Exception.Message)", $_)
} }
$result.changed = $true $module.Result.changed = $true
} }
} elseif ($ansible_partition_size -gt $max_supported_size) { } elseif ($ansible_partition_size -gt $max_supported_size) {
Fail-Json -obj $result -message "Specified partition size exceeds size supported by partition" $module.FailJson("Specified partition size exceeds size supported by the partition")
} }
} }
if ($drive_letter -NotIn ("auto", $null, $ansible_partition.DriveLetter)) { if ($drive_letter -NotIn ("auto", $null, $ansible_partition.DriveLetter)) {
if (-not $check_mode) { if (-not $module.CheckMode) {
try { try {
Set-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -NewDriveLetter $drive_letter Set-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -NewDriveLetter $drive_letter
} catch { } catch {
Fail-Json -obj $result -message "Unable to change drive letter: $($_.Exception.Message)" $module.FailJson("Unable to change drive letter: $($_.Exception.Message)", $_)
} }
} }
$result.changed = $true $module.Result.changed = $true
} }
} }
} }
else { else {
if ($state -eq "present") { if ($state -eq "present") {
if ($null -eq $disk_number) { if ($null -eq $disk_number) {
Fail-Json -obj $result -message "Missing required parameter: disk_number" $module.FailJson("Missing required parameter: disk_number")
} }
if ($null -eq $ansible_partition_size -and -not $size_is_maximum){ if ($null -eq $ansible_partition_size -and -not $size_is_maximum){
Fail-Json -obj $result -message "Missing required parameter: partition_size" $module.FailJson("Missing required parameter: partition_size")
} }
if (-not $size_is_maximum) { if (-not $size_is_maximum) {
try { try {
$max_supported_size = (Get-Disk -Number $disk_number).LargestFreeExtent $max_supported_size = (Get-Disk -Number $disk_number).LargestFreeExtent
} catch { } catch {
Fail-Json -obj $result -message "Unable to get maximum size supported by disk: $($_.Exception.Message)" $module.FailJson("Unable to get maximum size supported by disk: $($_.Exception.Message)", $_)
} }
if ($ansible_partition_size -gt $max_supported_size) { if ($ansible_partition_size -gt $max_supported_size) {
Fail-Json -obj $result -message "Partition size is not supported by disk. Use partition_size: -1 to get maximum size" $module.FailJson("Partition size is not supported by disk. Use partition_size: -1 to get maximum size")
} }
} }
@ -276,42 +288,42 @@ else {
if ($supp_part_type -eq "MBR" -and $mbr_styles.ContainsKey($mbr_type)) { if ($supp_part_type -eq "MBR" -and $mbr_styles.ContainsKey($mbr_type)) {
$partition_style = $mbr_styles.$mbr_type $partition_style = $mbr_styles.$mbr_type
} else { } else {
Fail-Json -obj $result -message "Incorrect partition style specified" $module.FailJson("Incorrect partition style specified")
} }
} }
if ($null -ne $gpt_type) { if ($null -ne $gpt_type) {
if ($supp_part_type -eq "GPT" -and $gpt_styles.ContainsKey($gpt_type)) { if ($supp_part_type -eq "GPT" -and $gpt_styles.ContainsKey($gpt_type)) {
$partition_style = $gpt_styles.$gpt_type $partition_style = $gpt_styles.$gpt_type
} else { } else {
Fail-Json -obj $result -message "Incorrect partition style specified" $module.FailJson("Incorrect partition style specified")
} }
} }
if (-not $check_mode) { if (-not $module.CheckMode) {
$ansible_partition = New-AnsiblePartition -DiskNumber $disk_number -Letter $drive_letter -SizeMax $size_is_maximum -Size $ansible_partition_size -MbrType $mbr_type -GptType $gpt_type -Style $partition_style $ansible_partition = New-AnsiblePartition -DiskNumber $disk_number -Letter $drive_letter -SizeMax $size_is_maximum -Size $ansible_partition_size -MbrType $mbr_type -GptType $gpt_type -Style $partition_style
} }
$result.changed = $true $module.Result.changed = $true
} }
} }
if ($state -eq "present" -and $ansible_partition) { if ($state -eq "present" -and $ansible_partition) {
if ($offline -NotIn ($null, $ansible_partition.IsOffline)) { if ($offline -NotIn ($null, $ansible_partition.IsOffline)) {
if (-not $check_mode) { if (-not $module.CheckMode) {
try { try {
Set-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -IsOffline $offline Set-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -IsOffline $offline
} catch { } catch {
Fail-Json -obj $result -message "Error setting partition offline: $($_.Exception.Message)" $module.FailJson("Error setting partition offline: $($_.Exception.Message)", $_)
} }
} }
$result.changed = $true $module.Result.changed = $true
} }
if ($hidden -NotIn ($null, $ansible_partition.IsHidden) -or $read_only -NotIn ($null, $ansible_partition.IsReadOnly) -or $active -NotIn ($null, $ansible_partition.IsActive)) { if ($hidden -NotIn ($null, $ansible_partition.IsHidden) -or $read_only -NotIn ($null, $ansible_partition.IsReadOnly) -or $active -NotIn ($null, $ansible_partition.IsActive)) {
if (-not $check_mode) { if (-not $module.CheckMode) {
Set-AnsiblePartitionState -hidden $hidden -read_only $read_only -active $active -partition $ansible_partition Set-AnsiblePartitionState -hidden $hidden -read_only $read_only -active $active -partition $ansible_partition
} }
$result.changed = $true $module.Result.changed = $true
} }
} }
Exit-Json -obj $result $module.ExitJson()

@ -1,12 +1,14 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright: (c) 2018, Varun Chopra (@chopraaa) # Copyright: (c) 2018, Varun Chopra (@chopraaa) <v@chopraaa.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
ANSIBLE_METADATA = {'metadata_version': '1.1', ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'], 'status': ['preview'],
'supported_by': 'community'} 'supported_by': 'community'
}
DOCUMENTATION = r''' DOCUMENTATION = r'''
--- ---
@ -84,9 +86,9 @@ notes:
U(https://docs.microsoft.com/en-us/windows/desktop/sysinfo/operating-system-version). U(https://docs.microsoft.com/en-us/windows/desktop/sysinfo/operating-system-version).
- This module cannot be used for removing the drive letter associated with a partition, initializing a disk or, file system formatting. - This module cannot be used for removing the drive letter associated with a partition, initializing a disk or, file system formatting.
- Idempotence works only if you're specifying a drive letter or other unique attributes such as a combination of disk number and partition number. - Idempotence works only if you're specifying a drive letter or other unique attributes such as a combination of disk number and partition number.
- For more information, see U(https://msdn.microsoft.com/en-us/library/windows/desktop/hh830524(v=vs.85).aspx). - For more information, see U(https://msdn.microsoft.com/en-us/library/windows/desktop/hh830524.aspx).
author: author:
- Varun Chopra (@chopraaa) - Varun Chopra (@chopraaa) <v@chopraaa.com>
''' '''
EXAMPLES = r''' EXAMPLES = r'''

@ -165,7 +165,7 @@
win_partition: win_partition:
disk_number: 1 disk_number: 1
partition_size: -1 partition_size: -1
mbr_type: IFS mbr_type: ifs
offline: True offline: True
register: recreate_partition_check register: recreate_partition_check
check_mode: True check_mode: True
@ -174,7 +174,7 @@
win_partition: win_partition:
disk_number: 1 disk_number: 1
partition_size: -1 partition_size: -1
mbr_type: IFS mbr_type: ifs
offline: True offline: True
register: recreate_partition register: recreate_partition
@ -182,7 +182,7 @@
win_partition: win_partition:
disk_number: 1 disk_number: 1
partition_size: -1 partition_size: -1
mbr_type: IFS mbr_type: ifs
offline: True offline: True
register: recreate_partition_idempotence_failure register: recreate_partition_idempotence_failure
ignore_errors: True ignore_errors: True

Loading…
Cancel
Save