VM Extension (Datadog) Install with Azure Policy

Philipp Gerber 251 Reputation points
2022-05-17T05:55:58.05+00:00

Hello Community,

it is possible to install an Extension on an Azure VM with Azure Policy?
An Customer use Datadog fof his Monitoring and would like to install the VM Extension on all the Azure VMs.

Now was the question about install the Extension with an Azure Policy.

Thanks a lot.

Best Regards,
Phil

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
6,930 questions
Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
770 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. SwathiDhanwada-MSFT 16,966 Reputation points
    2022-05-20T08:37:09.957+00:00

    @PhilippGerber-6516 Thanks for your query. Yes, it's possible to install an extension on Azure VM with Azure Policy. You can create PowerShell script to install the Datadog and you can use below Azure Policy to deploy the extension.

    "policyRule": {
        "if": {
            "allOf": [
                {
                    "field": "type",
                    "equals": "Microsoft.Compute/virtualMachines"
                },
                {
                    "field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType",
                    "equals": "Windows"
                }
            ]
        },
        "then": {
            "effect": "deployIfNotExists",
            "details": {
                "type": "Microsoft.Compute/virtualMachines/extensions",
                "roleDefinitionIds": [
                    "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c",
                    "/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c"
                ],
                "existenceCondition": {
                    "allOf": [
                        {
                            "field": "name",
                            "contains": "DatadogExtension"
                        }
                    ]
                },
                "deployment": {
                    "properties": {
                        "mode": "incremental",
                        "template": {
                            "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                            "contentVersion": "1.0.0.0",
                            "parameters": {
                                "vmName": {
                                    "type": "String"
                                },
                                "location": {
                                    "type": "String"
                                },
                                "fileUris": {
                                    "type": "String"
                                },
                                "arguments": {
                                    "defaultValue": " ",
                                    "type": "SecureString"
                                }
                            },
                            "variables": {
                                "UriFileNamePieces": "[split(parameters('fileUris'), '/')]",
                                "firstFileNameString": "[variables('UriFileNamePieces')[sub(length(variables('UriFileNamePieces')), 1)]]",
                                "firstFileNameBreakString": "[split(variables('firstFileNameString'), '?')]",
                                "firstFileName": "[variables('firstFileNameBreakString')[0]]"
                            },
                            "resources": [
                                {
                                    "type": "Microsoft.Compute/virtualMachines/extensions",
                                    "apiVersion": "2015-06-15",
                                    "name": "[concat(parameters('vmName'),'/DataDogExtension')]",
                                    "location": "[parameters('location')]",
                                    "properties": {
                                        "publisher": "Microsoft.Compute",
                                        "type": "CustomScriptExtension",
                                        "typeHandlerVersion": "1.9",
                                        "autoUpgradeMinorVersion": true,
                                        "settings": {
                                            "fileUris": "[split(parameters('fileUris'), ' ')]"
                                        },
                                        "protectedSettings": {
                                            "commandToExecute": "[concat ('powershell -ExecutionPolicy Unrestricted -File ', variables('firstFileName'), ' ', parameters('arguments'))]"
                                        }
                                    }
                                }
                            ]
                        },
                        "parameters": {
                            "vmName": {
                                "value": "[field('name')]"
                            },
                            "location": {
                                "value": "[field('location')]"
                            },
                            "fileUris": {
                                "value": "<url of powershell script>"
                            }
                        }
                    }
                }
            }
        }
    }
    }
    
    0 comments No comments