Last active
June 11, 2019 18:22
-
-
Save weiyentan/1e46ce4d56a4a9421b77d4d7083319aa to your computer and use it in GitHub Desktop.
Configure_failovercluster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!powershell | |
# Description: Move Failover cluster from one node to the next. | |
#Requires -Module Ansible.ModuleUtils.Legacy | |
$ErrorActionPreference = 'Stop' | |
$results = @{ | |
changed = $false #mandatory key value pair | |
msg = '' #optional keyvalue pair | |
} | |
#region helper functions | |
function Test-PSModule | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[Parameter(Mandatory = $true, | |
Position = 1)] | |
[string]$module | |
) | |
if (-not (Get-Module -name $module)) | |
{ | |
if (Get-Module -ListAvailable | | |
Where-Object { $_.name -eq $module }) | |
{ | |
$true | |
} #end if module available then import | |
else { $false } #module not available | |
} # end if not module | |
else { $true } #module exists | |
} | |
function testclusterelement | |
{ | |
param | |
( | |
[ValidateSet('clusterresource', 'clustergroup', 'clustersharedvolume')] | |
$type, | |
$name | |
) | |
if ($type -eq 'clusterresource') | |
{ | |
$testClusterresouceexists = Get-ClusterResource -Name $name | |
if ($testClusterresouceexists) | |
{ | |
$true | |
} | |
else | |
{ | |
$false | |
} | |
} | |
if ($type -eq 'clustergroup') | |
{ | |
$testClustergroupexists = Get-ClusterGroup -Name $name | |
if ($testClustergroupexists) | |
{ | |
$true | |
} | |
else | |
{ | |
$false | |
} | |
} | |
if ($type -eq 'clustersharedvolume') | |
{ | |
$testClustersharedvolumeexists = Get-ClusterSharedVolume -name $name | |
if ($testClustersharedvolumeexists) | |
{ | |
$true | |
} | |
else | |
{ | |
$false | |
} | |
} | |
} | |
#endregion | |
#check if Windows Failover module exists | |
$evalmodule = Test-PSModule -module FailoverClusters | |
if ($evalmodule -eq $false) | |
{ | |
Fail-Json $results -message "The module FailoverClusters does not exist. Cannot continue." | |
} | |
#endregion | |
$params = Parse-Args $args -supports_check_mode $false | |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false | |
$name = Get-AnsibleParam -obj $params -name "name" -type "str" -failifempty $true | |
$Clustertype = Get-AnsibleParam -obj $params -name "Clustertype" -type "str" -ValidateSet "Clustergroup" , "Clusterresource" , "ClusterSharedVolume" | |
$destination = Get-AnsibleParam -obj $params -name "destination" -type "str" | |
$group = Get-AnsibleParam -obj $params -name "group" -type "str" | |
#region Clustergroup | |
if ($Clustertype -eq "Clustergroup") | |
{ | |
$eval = testclusterelement -type clustergroup $name | |
if ($eval -eq $false) | |
{ | |
Fail-Json -message "The cluster group $name does not exist. Cannot continue." | |
} | |
#test node ownership - whether or not relevant it is relevant to continue the task | |
if ($destination) | |
{ | |
$currentownernode = Get-ClusterGroup -Name $name | Select-Object -ExpandProperty ownernode | |
if ($currentownernode -eq $destination) | |
{ | |
$results.msg ="The cluster group $name is already on $destination" | |
Exit-Json -obj $results | |
} | |
try | |
{ | |
if(!$check_mode){ | |
Move-ClusterGroup -Name $name -Node $destination -ErrorAction stop | |
} | |
$results.changed = $true | |
$results.msg = "The cluster group $name has successfully moved to node $destination" | |
Exit-Json $results | |
} | |
catch | |
{ | |
Fail-Json $results -message "The cluster group $name did not move successfully to $destination" | |
} | |
}else #playbook has omitted destination | |
{ | |
try | |
{ | |
if (!$check_mode) | |
{ | |
Move-ClusterGroup -Name $name -ErrorAction Stop | |
} | |
$ownernode = Get-ClusterGroup -name $name | Select-Object -ExpandProperty ownernode | |
$results.changed = $true | |
$results.msg = "The cluster group $name has successfully moved to another node $ownernode" | |
Exit-Json | |
} | |
catch | |
{ | |
Fail-Json $results -message "The cluster group $name did not move successfully to another node" | |
} | |
} | |
} | |
#endregion | |
#region Clusterresource | |
if ($Clustertype -eq "ClusterResource") | |
{ | |
#test whether cluster resource exists | |
$evalclusterresource = testclusterelement -type clusterresource -name $name | |
if ($testClustergroupexists -eq $false) | |
{ | |
Fail-Json -message "The cluster group $name does not exist" | |
} | |
#test group ownership - whether or not relevant it is relevant to continue the task | |
$currentresourceownership = Get-ClusterResource $name | Select-Object -ExpandProperty ownergroup | |
if ($currentresourceownership -eq $group) | |
{ | |
$results.msg = "The cluster resource $name is already on $group" | |
Exit-Json $results | |
} | |
try | |
{ | |
if (!$check_mode) | |
{ | |
Move-ClusterResource -Name $name -Group $group -ErrorAction Stop | |
} | |
$results.changed = $true | |
$results.msg = "The cluster resource $name has successfully moved to another group $group" | |
Exit-Json | |
} | |
catch | |
{ | |
Fail-Json $results -message "The cluster resource did not move successfully to another node" | |
} | |
} | |
#endregion | |
#region Clustersharedvolume | |
if ($Clustertype -eq "Clustersharedvolume") | |
{ | |
$evalclustersharedvolume = testclusterelement -type clustersharedvolume -name $name | |
if (!$evalclustersharedvolume) | |
{ | |
Fail-Json -message "The cluster shared volume $name does not exist" | |
} | |
if ($destination) | |
{ | |
$currentsharedvolumeownership = Get-ClusterSharedVolume -Name $name | Select-Object -ExpandProperty node | |
if ($currentsharedvolumeownership -eq $destination) | |
{ | |
$results.msg = "The cluster shared volume $name is already on $destination" | |
Exit-Json -obj $results | |
} | |
try | |
{ | |
if (!$check_mode) | |
{ | |
Move-ClusterSharedVolume -Name $name -Node $destination -ErrorAction Stop | |
} | |
$results.changed = $true | |
$results.msg = "The cluster shared volume $name has successfully moved to another node $destination" | |
Exit-Json | |
} | |
catch | |
{ | |
Fail-Json $results -message "The cluster resource did not move successfully to node $destination" | |
} | |
} | |
else #playbook has omitted destination which means it will randomly move to another node | |
{ | |
try | |
{ | |
if (!$check_mode) | |
{ | |
Move-ClusterSharedVolume -Name $name | |
} | |
$ownernode = Get-ClusterSharedVolume -name $name | Select-Object -ExpandProperty ownernode | |
$results.changed = $true | |
$results.msg = "The cluster group $name has successfully moved to another node $ownernode" | |
Exit-Json | |
} | |
catch | |
{ | |
Fail-Json $results -message "The cluster shared volume did not move successfully to another node" | |
} | |
} | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment