Bylaw

Gallery › Compute › Azure Community Policy

Enable automatic OS upgrade on Virtual Machine Scale Sets

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

This policy enables automatic OS upgrade on Virtual Machine Scale Sets. New scale sets will have automatic OS upgrade enabled automatically. Existing scale sets that are not opted into automatic OS upgrade will be marked as non-compliant and can be enabled through policy remediation.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONazurepolicy.json · 96 lines
{
  "name": "8979aa44-e880-4b3f-b0f5-6f6ab4a8b93a",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Enable automatic OS upgrade on Virtual Machine Scale Sets",
    "description": "This policy enables automatic OS upgrade on Virtual Machine Scale Sets. New scale sets will have automatic OS upgrade enabled automatically. Existing scale sets that are not opted into automatic OS upgrade will be marked as non-compliant and can be enabled through policy remediation.",
    "metadata": {
      "category": "Compute",
      "version": "1.0.0"
    },
    "mode": "Indexed",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachineScaleSets"
          },
          {
            "field": "Microsoft.Compute/imageVersion",
            "equals": "latest"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Compute/virtualMachineScaleSets",
          "name": "[field('name')]",
          "existenceCondition": {
            "field": "Microsoft.Compute/VirtualMachineScaleSets/upgradePolicy.automaticOSUpgrade",
            "equals": "true"
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "vmssName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "name": "[parameters('vmssName')]",
                    "type": "Microsoft.Compute/virtualMachineScaleSets",
                    "location": "[parameters('location')]",
                    "apiVersion": "2018-10-01",
                    "properties": {
                      "upgradePolicy": {
                        "automaticOSUpgradePolicy": {
                          "enableAutomaticOSUpgrade": true
                        }
                      }
                    }
                  }
                ]
              },
              "parameters": {
                "vmssName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw8979aa44-e880-4b3f-b0f5-6f6ab4a8b93a.bylaw.ps1 · 35 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '8979aa44-e880-4b3f-b0f5-6f6ab4a8b93a' {

    Properties @{
        DisplayName = 'Enable automatic OS upgrade on Virtual Machine Scale Sets'
        Description = 'This policy enables automatic OS upgrade on Virtual Machine Scale Sets. New scale sets will have automatic OS upgrade enabled automatically. Existing scale sets that are not opted into automatic OS upgrade will be marked as non-compliant and can be enabled through policy remediation.'
        Category    = 'Compute'
        Version     = '1.0.0'
        Mode        = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Compute/virtualMachineScaleSets' {

        Require 'imageVersion is not latest' {
            Field 'Microsoft.Compute/imageVersion' | Must -Not -Be 'latest' -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Compute/virtualMachineScaleSets' -Role 'Virtual Machine Contributor' -Name (Arm '[field(''name'')]') -Effect (Param Effect) {
        Require 'upgradePolicy.automaticOSUpgrade is true' {
            Field upgradePolicy.automaticOSUpgrade | Must -Be 'true'
        }
        Deployment -TemplateFile './templates/8979aa44-e880-4b3f-b0f5-6f6ab4a8b93a.json' -Parameters ([ordered]@{
                vmssName = (Arm '[field(''name'')]')
                location = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Compute/enable-automatic-os-upgrade-on-virtual-machine-scale-sets/azurepolicy.json in Azure/Community-Policy, under the MIT licence.