We have a file located in a sub folder called scripts, the file is called ssvm.ps1 and we don’t alter it.
In our main code we have the resource azurerm_automation_runbook. Code looks like this
resource "azurerm_automation_runbook" "ssvm_patching" {
name = "Auto-patching-start-stop"
location = module.global_logging.logging_rg_location
resource_group_name = module.global_logging.logging_rg_name
automation_account_name = module.fmg-tf-patching-module.automation_name
log_verbose = "true"
log_progress = "true"
description = "This Run Book is used to start and stop VM during patching"
runbook_type = "PowerShell"
content = file("scripts/ssvm.ps1",)
We use Windows workstations
Before we push our code upstream we always run a “Terraform Plan” to confirm that the code appears valid.
When we run the Terraform Plan on our local machine, noting that it is still using TF Cloud as the backend we always see
# azurerm_automation_runbook.ssvm_patching will be updated in-place
~ resource "azurerm_automation_runbook" "ssvm_patching" {
~ content = <<-EOT
param(
[parameter(Mandatory = $true)]
[string]$subscriptionid
)
#Import the Modules
Import-Module 'Az.Accounts'
Import-Module 'Az.Compute'
#Authenticating through identity
Connect-AzAccount -Identity
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process
# set and store context
Set-AzContext -Subscription $subscriptionid
#Turning on the VM which are in deallocated state with the tag vlaue third sunday
$VMs = Get-AzVM -Status | Where-Object PowerState -eq "VM deallocated"
foreach ($VM in $VMs) {
[Hashtable]$VMTag = (Get-AzVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name).Tags
foreach ($h in $VMTag.GetEnumerator()) {
if (($h.Name -eq "updateGroup") -and ($h.value -eq "thirdsunday")) {
Write-Output "VM with tags updateGroup:thirdsunday are" $($VM.Name)
Write-Output "Starting virtual machine...$($VM.Name)"
Start-AzVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name
}
}
}
Start-Sleep -Seconds 9000
#returning the VM to its last known state
foreach ($VM in $VMs) {
if ($VMs.PowerState -eq "VM deallocated") {
Write-Output "Stopping virtual machine...$($VM.Name)"
Stop-AzVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Force
}
}
EOT
id = "/subscriptions/c2ed245c-85dc-4179-aeb4-a637a3cf96db/resourceGroups/fzanzcspdrgp002/providers/Microsoft.Automation/automationAccounts/franzgnpdaa001/runbooks/Auto-patching-start-stop"
name = "Auto-patching-start-stop"
tags = {
"costCentre" = "CoreInfrastructure"
"deployType" = "Terraform"
"description" = "start & stop third sunday VM patching runbook"
"source" = "https://github.com/fmgplatform/az-coreservices-tf-L1"
}
# (9 unchanged attributes hidden)
}
Yet if we run a “plan and apply” or “plan only” from directly TF Cloud we don’t see this.
The only thing I can think off is that it has something to do with Our windows machines knowing the TF Cloud use Linux but I can’t verify this.
Does anyone else know why this could be the case?