Automated Installation on Existing VMs
Azure Automation Runbooks: Azure Automation allows you to create and execute runbooks to automate tasks. You can create a runbook that runs a bash script to install your security agent on all existing VMs.
Steps:
- Create an Azure Automation Account:
- Navigate to Azure Portal -> Automation Accounts -> Create a new Automation Account.
- Create a Runbook:
- In the Automation Account, go to Runbooks -> Create a new Runbook.
- Choose the type (PowerShell or Python). For simplicity, you can use PowerShell to execute the bash script.
- Script to Install Agent:
$script = @"
#!/bin/bash
# Your agent installation script
echo 'Installing security agent...'
sudo curl -o /tmp/install_agent.sh <URL_to_your_bash_script>
sudo bash /tmp/install_agent.sh
"@
$linuxVms = Get-AzVM -ResourceGroupName <YourResourceGroup> | Where-Object { $_.StorageProfile.OsDisk.OsType -eq "Linux" }
foreach ($vm in $linuxVms) {
Invoke-AzVMRunCommand -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name -CommandId 'RunShellScript' -ScriptString $script
}
- Schedule the Runbook:
- Create a schedule to run the runbook at regular intervals to ensure any new VM missed by the real-time automation gets the agent installed.
Real-Time Installation on New VMs
Azure Policy and Azure Event Grid: Azure Policy can enforce configurations on your resources, and Azure Event Grid can trigger actions based on events.
Steps:
- Create a Custom Azure Policy:
- This policy will audit if the security agent is installed and can trigger a remediation task.
{ "mode": "Indexed", "policyRule": { "if": { "allOf": [ { "field": "type", "equals": "Microsoft.Compute/virtualMachines" }, { "field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType", "equals": "Linux" } ] }, "then": { "effect": "AuditIfNotExists", "details": { "type": "Microsoft.Compute/virtualMachines/extensions", "name": "InstallAgent", "existenceCondition": { "allOf": [ { "field": "Microsoft.Compute/virtualMachines/extensions/publisher", "equals": "<YourExtensionPublisher>" }, { "field": "Microsoft.Compute/virtualMachines/extensions/type", "equals": "<YourExtensionType>" } ] } } } } }
- Remediation Task:
Example Event Grid setup:- Set up a remediation task for the policy to deploy an extension or run a script if the agent is not found. ```1. **Azure Event Grid and Azure Automation:** ```sql - Use Event Grid to monitor for the creation of new VMs. - When a new VM is created, trigger an Azure Automation runbook to install the agent.
- Create an Event Grid subscription for the Microsoft.Compute/virtualMachines/write event.
- The Event Grid subscription triggers an Azure Function or Logic App, which in turn invokes the Azure Automation runbook to install the agent.
Preferred Installation Method Using Bash Script Azure VM Extensions: Azure VM Custom Script Extension can be used to run a bash script on VMs.
Steps:
- Custom Script Extension:
Create a script file (e.g., install_agent.sh) and upload it to an Azure Storage Account or GitHub.
- Deploy the Extension:
Use the following ARM template or CLI command to deploy the extension on new VMs: ARM Template snippet:
{
"type": "Microsoft.Compute/virtualMachines/extensions",
"name": "[concat(parameters('vmName'), '/CustomScript')]",
"apiVersion": "2021-03-01",
"location": "[parameters('location')]",
"properties": {
"publisher": "Microsoft.Azure.Extensions",
"type": "CustomScript",
"typeHandlerVersion": "2.0",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"https://<your_storage_account>.blob.core.windows.net/scripts/install_agent.sh"
],
"commandToExecute": "bash install_agent.sh"
}
}
}
If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.
hth Marcin