Update module to use Ansible.Basic (#51365)

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

@ -1,34 +1,46 @@
#!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)
#Requires -Module Ansible.ModuleUtils.Legacy
#AnsibleRequires -CSharpUtil Ansible.Basic
#AnsibleRequires -OSVersion 6.2
Set-StrictMode -Version 2
$ErrorActionPreference = "Stop"
$params = Parse-Args -arguments $args -supports_check_mode $true
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false
$state = Get-AnsibleParam -obj $params -name "state" -type "str" -failifempty $false -default "present" -validateset "absent", "present"
$drive_letter = Get-AnsibleParam -obj $params -name "drive_letter" -type "str" -failifempty $false
$disk_number = Get-AnsibleParam -obj $params -name "disk_number" -type "int" -failifempty $false
$partition_number = Get-AnsibleParam -obj $params -name "partition_number" -type "int" -failifempty $false
$partition_size = Get-AnsibleParam -obj $params -name "partition_size" -type "str" -failifempty $false
$read_only = Get-AnsibleParam -obj $params -name "read_only" -type "bool" -failifempty $false
$active = Get-AnsibleParam -obj $params -name "active" -type "bool" -failifempty $false
$hidden = Get-AnsibleParam -obj $params -name "hidden" -type "bool" -failifempty $false
$offline = Get-AnsibleParam -obj $params -name "offline" -type "bool" -failifempty $false
$mbr_type = Get-AnsibleParam -obj $params -name "mbr_type" -type "str" -failifempty $false -validateset "fat12", "fat16", "extended", "huge", "ifs", "fat32"
$gpt_type = Get-AnsibleParam -obj $params -name "gpt_type" -type "str" -failifempty $false -validateset "system_partition", "microsoft_reserved", "basic_data", "microsoft_recovery"
$result = @{
changed = $false
$spec = @{
options = @{
state = @{ type = "str"; choices = "absent", "present"; default = "present" }
drive_letter = @{ type = "str" }
disk_number = @{ type = "int" }
partition_number = @{ type = "int" }
partition_size = @{ type = "str" }
read_only = @{ type = "bool" }
active = @{ type = "bool" }
hidden = @{ type = "bool" }
offline = @{ type = "bool" }
mbr_type = @{ type = "str"; choices = "fat12", "fat16", "extended", "huge", "ifs", "fat32" }
gpt_type = @{ type = "str"; choices = "system_partition", "microsoft_reserved", "basic_data", "microsoft_recovery" }
}
supports_check_mode = $true
}
$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
$ansible_partition = $false
$ansible_partition_size = $null
@ -77,8 +89,8 @@ if ($null -ne $partition_size) {
$ansible_partition_size = Convert-SizeToBytes -Size $Matches.Size -Units $Matches.Units
}
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")
}
}
# If partition_exists, we can change or delete it; otherwise we only need the disk to create a new partition
@ -91,23 +103,23 @@ elseif ($drive_letter -and -not ($disk_number -and $partition_number)) {
$ansible_partition = Get-Partition -DriveLetter $drive_letter -ErrorAction SilentlyContinue
}
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) {
try {
Get-Disk -Number $disk_number | Out-Null
} catch {
Fail-Json -obj $result -message "Specified disk does not exist"
$module.FailJson("Specified disk does not exist")
}
}
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
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 {
@ -156,7 +168,7 @@ function New-AnsiblePartition {
try {
$new_partition = New-Partition @parameters
} 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
@ -191,7 +203,7 @@ function Set-AnsiblePartitionState {
try {
Set-Partition @parameters
} 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 ($state -eq "absent") {
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 {
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 {
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) {
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) {
try {
$max_supported_size = (Get-PartitionSupportedSize -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber).SizeMax
} 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) {
$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.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 {
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 {
Fail-Json -obj $result -message "Unable to change partition size: $($_.Exception.Message)"
}
$result.changed = $true
$module.FailJson("Unable to change partition size: $($_.Exception.Message)", $_)
}
$module.Result.changed = $true
}
} 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 (-not $check_mode) {
if (-not $module.CheckMode) {
try {
Set-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -NewDriveLetter $drive_letter
} 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 {
if ($state -eq "present") {
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){
Fail-Json -obj $result -message "Missing required parameter: partition_size"
$module.FailJson("Missing required parameter: partition_size")
}
if (-not $size_is_maximum) {
try {
$max_supported_size = (Get-Disk -Number $disk_number).LargestFreeExtent
} 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) {
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)) {
$partition_style = $mbr_styles.$mbr_type
} else {
Fail-Json -obj $result -message "Incorrect partition style specified"
$module.FailJson("Incorrect partition style specified")
}
}
if ($null -ne $gpt_type) {
if ($supp_part_type -eq "GPT" -and $gpt_styles.ContainsKey($gpt_type)) {
$partition_style = $gpt_styles.$gpt_type
} 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
}
$result.changed = $true
$module.Result.changed = $true
}
}
if ($state -eq "present" -and $ansible_partition) {
if ($offline -NotIn ($null, $ansible_partition.IsOffline)) {
if (-not $check_mode) {
if (-not $module.CheckMode) {
try {
Set-Partition -DiskNumber $ansible_partition.DiskNumber -PartitionNumber $ansible_partition.PartitionNumber -IsOffline $offline
} 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 (-not $check_mode) {
if (-not $module.CheckMode) {
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
# -*- 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)
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = r'''
---
@ -14,7 +16,7 @@ module: win_partition
version_added: '2.8'
short_description: Creates, changes and removes partitions on Windows Server
description:
- The M(win_partition) module can create, modify or delete a partition on a disk
- The M(win_partition) module can create, modify or delete a partition on a disk
options:
state:
description:
@ -80,13 +82,13 @@ options:
choices: [ system_partition, microsoft_reserved, basic_data, microsoft_recovery ]
notes:
- A minimum Operating System Version of 6.2 is required to use this module. To check if your OS is compatible, see
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.
- 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).
- A minimum Operating System Version of 6.2 is required to use this module. To check if your OS is compatible, see
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.
- 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.aspx).
author:
- Varun Chopra (@chopraaa)
- Varun Chopra (@chopraaa) <v@chopraaa.com>
'''
EXAMPLES = r'''

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

Loading…
Cancel
Save