Skip to content

Commit

Permalink
Added ansible files
Browse files Browse the repository at this point in the history
  • Loading branch information
Hasshim Ali committed Aug 18, 2022
1 parent a136101 commit 5ca96be
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ansible-hyper-v-patching/Drain_Role_v1.0.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
$blade = $(hostname)

# Fucntion
function GetTotalVms {
Get-ClusterResource | where {($_.ResourceType) -like "Virtual Machine"} | where {($_.OwnerNode) -like $blade} |Select OwnerGroup, OwnerNode, State | Sort-Object OwnerNode -Descending
}

#Delet existing log files
$logPath = "C:\ansible\*"
if (Test-Path $logPath -Include *_log*.txt)
{
Remove-Item C:\ansible\*_log.txt*
}

#Get existing VMs before drain
$beforeDrain = GetTotalVms
$beforeDrain | Out-File c:\ansible\BeforeDrain_log.txt


#Drain node and put to pause state
Suspend-ClusterNode -Name "$blade" -Drain
Do {
Write-Host (get-clusternode –Name "$blade").DrainStatus -ForegroundColor Magenta
Sleep 10
}
until ((get-clusternode –Name "$blade").DrainStatus -ne "InProgress")
If ((get-clusternode –Name "$blade").DrainStatus -eq "Completed")
{
#Get no. of VMs on blade and log
$totalVMS = GetTotalVms
If ($totalVMS.Count -gt 0) {
GetTotalVms | Out-File C:\ansible\DrainError_log.txt
} else {
$totalVMS.Count | Out-File C:\ansible\DrainSuccess_log.txt
}
}

26 changes: 26 additions & 0 deletions ansible-hyper-v-patching/Failback_Role_v1.0.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$blade = $(hostname)
$beforeDrain = Get-Content c:\ansible\BeforeDrain_log.txt

# Fucntion
function GetTotalVms {
Get-ClusterResource | where {($_.ResourceType) -like "Virtual Machine"} | where {($_.OwnerNode) -like $blade} |Select OwnerGroup, OwnerNode, State | Sort-Object OwnerNode -Descending
}

Resume-clusternode -Name "$blade" -Failback Immediate
$vmStatus = $(Get-ClusterResource).state
while ($vmStatus -contains "OfflinePending") {
Write-Host "Live migration in progress" -ForegroundColor Magenta
Sleep 10
$vmStatus = $(Get-ClusterResource).state
}

Write-Host "Fail back to $blade is DONE!" -ForegroundColor Green

GetTotalVms | Out-File c:\ansible\AfterDrain_log.txt
$afterDrain = Get-Content c:\ansible\AfterDrain_log.txt

if ($beforeDrain.Count -eq $afterDrain.Count) {
Out-File c:\ansible\Failback_Success_Log.txt
} else {
Out-File c:\ansible\Failback_Error_Log.txt
}
50 changes: 50 additions & 0 deletions ansible-hyper-v-patching/ansible-hyper-v-patching.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
##### Drain Role & Reboot
- hosts: mgmtBlade
serial: 1
become_method: run_as

#vars_files:
# - vars/win_secret.yml

tasks:
- name: Task 1.0 (Drain Role)
script: scripts/Drain_Role_v1.0.ps1

- name: Task 1.1 (Check for errors)
win_stat:
path: c:\ansible\DrainError_log.txt
register: Drain_file
- fail:
msg: Drain role fail on host. Kindly verify & failback if necessary.
when: Drain_file.stat.exists == True

- name: Task 1.2 (Reboot Server if no errors)
win_reboot:
reboot_timeout: 7200
post_reboot_delay: 60
test_command: ipconfig
when: Drain_file.stat.exists == False

##### FailBack
- name: Task 2.0 (Failback VMs)
script: scripts/Failback_Role_v1.0.ps1

- name: Task 2.1 (Check for errors)
win_stat:
path: c:\ansible\Failback_Error_Log.txt
register: Failback_file
- fail:
msg: Failback fail on host. Kindly check the logs.
when: Failback_file.stat.exists == True

##### Check KB Details
- hosts: mgmtBlade
become_method: run_as

tasks:
- name: Task 3 (Get KB Patched Details)
win_shell: gwmi win32_quickfixengineering | sort installedon -desc | select -First 1
register: KB_result
- debug:
var: KB_result.stdout_lines

0 comments on commit 5ca96be

Please sign in to comment.