Bylaw

Gallery › Backup › Azure Community Policy

vm-backup-without-tag

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode indexed Version 1.0.0

need to add description

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

Azure Policy JSONazurepolicy.json · 191 lines
{
  "name": "7af362f3-2e06-441d-9eec-f83ad36774d1",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "vm-backup-without-tag",
    "description": "need to add description",
    "metadata": {
      "version": "1.0.0",
      "category": "Backup"
    },
    "mode": "indexed",
    "parameters": {
      "backupVaultResourceGroup": {
        "type": "string",
        "metadata": {
          "displayName": "Backup Vault Resource Group Name",
          "description": "Resource Group in which the backup vault resides"
        }
      },
      "backupVault": {
        "type": "string",
        "metadata": {
          "displayName": "Backup Vault Name",
          "description": "Target backup vault for VM backup onboarding"
        }
      },
      "backupPolicy": {
        "type": "string",
        "metadata": {
          "displayName": "backupPolicy",
          "description": "Target backup policy for VM backup onboarding"
        }
      },
      "exclusionTagName": {
        "type": "String",
        "metadata": {
          "displayName": "Exclusion Tag Name",
          "description": "Name of the tag to use for excluding VMs from the scope of this policy. This should be used along with the Exclusion Tag Value parameter."
        },
        "defaultValue": "SkipBackup"
      },
      "exclusionTagValue": {
        "type": "Array",
        "metadata": {
          "displayName": "Exclusion Tag Values",
          "description": "Value of the tag to use for excluding VMs from the scope of this policy (in case of multiple values, use a comma-separated list). This should be used along with the Exclusion Tag Name parameter."
        },
        "defaultValue": [
          "true",
          "yes"
        ]
      },
      "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/virtualMachines"
          },
          {
            "field": "id",
            "notContains": "/resourceGroups/databricks-rg-"
          },
          {
            "anyOf": [
              {
                "not": {
                  "field": "[concat('tags[', parameters('exclusionTagName'), ']')]",
                  "in": "[parameters('exclusionTagValue')]"
                }
              },
              {
                "value": "[empty(parameters('exclusionTagValue'))]",
                "equals": "true"
              },
              {
                "value": "[empty(parameters('exclusionTagName'))]",
                "equals": "true"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c",
            "/providers/microsoft.authorization/roleDefinitions/5e467623-bb1f-42f4-a55d-6e525e11384b"
          ],
          "type": "Microsoft.RecoveryServices/backupprotecteditems",
          "existenceCondition": {
            "allOf": [
              {
                "field": "name",
                "like": "*"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "vmName": {
                    "type": "string"
                  },
                  "vmResourceGroup": {
                    "type": "string"
                  },
                  "backupVaultResourceGroup": {
                    "type": "string"
                  },
                  "backupVault": {
                    "type": "string"
                  },
                  "backupPolicy": {
                    "type": "string"
                  }
                },
                "variables": {
                  "backupIntentConcat": "[concat('/Azure/vm;iaasvmcontainerv2;',parameters('vmResourceGroup'),';',parameters('vmName'))]"
                },
                "resources": [
                  {
                    "apiVersion": "2017-05-10",
                    "name": "[concat(parameters('vmName'), '-' , 'backupIntent')]",
                    "type": "Microsoft.Resources/deployments",
                    "resourceGroup": "[parameters('backupVaultResourceGroup')]",
                    "properties": {
                      "mode": "Incremental",
                      "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "resources": [
                          {
                            "name": "[concat(parameters('backupVault'),variables('backupIntentConcat'))]",
                            "apiVersion": "2017-07-01",
                            "type": "Microsoft.RecoveryServices/vaults/backupFabrics/backupProtectionIntent",
                            "properties": {
                              "protectionIntentItemType": "AzureResourceItem",
                              "policyId": "[concat(resourceId('Microsoft.RecoveryServices/vaults/backuppolicies',parameters('backupVault'),parameters('backupPolicy')))]",
                              "sourceResourceId": "[concat(resourceId('Microsoft.Compute/virtualMachines',parameters('vmName')))]"
                            }
                          }
                        ]
                      }
                    }
                  }
                ]
              },
              "parameters": {
                "vmName": {
                  "value": "[field('name')]"
                },
                "vmResourceGroup": {
                  "value": "[resourcegroup().name]"
                },
                "backupVaultResourceGroup": {
                  "value": "[parameters('backupVaultResourceGroup')]"
                },
                "backupVault": {
                  "value": "[parameters('backupVault')]"
                },
                "backupPolicy": {
                  "value": "[parameters('backupPolicy')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw7af362f3-2e06-441d-9eec-f83ad36774d1.bylaw.ps1 · 76 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '7af362f3-2e06-441d-9eec-f83ad36774d1' {

    Properties @{
        DisplayName = 'vm-backup-without-tag'
        Description = 'need to add description'
        Version     = '1.0.0'
        Category    = 'Backup'
        Mode        = 'Indexed'
    }

    Parameter BackupVaultResourceGroup @{
        Type        = 'string'
        DisplayName = 'Backup Vault Resource Group Name'
        Description = 'Resource Group in which the backup vault resides'
    }
    Parameter BackupVault @{
        Type        = 'string'
        DisplayName = 'Backup Vault Name'
        Description = 'Target backup vault for VM backup onboarding'
    }
    Parameter BackupPolicy @{
        Type        = 'string'
        DisplayName = 'backupPolicy'
        Description = 'Target backup policy for VM backup onboarding'
    }
    Parameter ExclusionTagName @{
        Default     = 'SkipBackup'
        DisplayName = 'Exclusion Tag Name'
        Description = 'Name of the tag to use for excluding VMs from the scope of this policy. This should be used along with the Exclusion Tag Value parameter.'
    }
    Parameter ExclusionTagValue @{
        Type        = 'Array'
        Default     = @('true', 'yes')
        DisplayName = 'Exclusion Tag Values'
        Description = 'Value of the tag to use for excluding VMs from the scope of this policy (in case of multiple values, use a comma-separated list). This should be used along with the Exclusion Tag Name parameter.'
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Compute/virtualMachines' {

        When {
            Field id | Must -Not -ContainText '/resourceGroups/databricks-rg-'
        }

        Require 'tag (Param ExclusionTagName) is one of the ExclusionTagValue parameter' {
            Tag (Param ExclusionTagName) | Must -BeIn (Param ExclusionTagValue)
        }

        Require 'Requirement 2' {
            Arm '[empty(parameters(''exclusionTagValue''))]' | Must -Not -Be 'true'
        }

        Require 'Requirement 3' {
            Arm '[empty(parameters(''exclusionTagName''))]' | Must -Not -Be 'true'
        }
    }

    DeployIfNotExists 'Microsoft.RecoveryServices/backupprotecteditems' -Role 'Virtual Machine Contributor', 'Backup Contributor' -Effect (Param Effect) {
        Require 'name is like *' {
            Field name | Must -BeLike '*'
        }
        Deployment -TemplateFile './templates/7af362f3-2e06-441d-9eec-f83ad36774d1.json' -Parameters ([ordered]@{
                vmName                   = (Arm '[field(''name'')]')
                vmResourceGroup          = (Arm '[resourcegroup().name]')
                backupVaultResourceGroup = (Param BackupVaultResourceGroup)
                backupVault              = (Param BackupVault)
                backupPolicy             = (Param BackupPolicy)
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Backup/vm-backup-without-tag/azurepolicy.json in Azure/Community-Policy, under the MIT licence.