@Firthouse M G - Thanks for the question and using MS Q&A forum.
Here’s a step-by-step guide to create an Azure Automation runbook using PowerShell to run a Linux shell script on an Azure VM, and then call this runbook from Azure Data Factory (ADF) using a Webhook activity:
Create an Azure Automation Account:
- Navigate to the Azure portal and create a new Automation Account.
- Create a Runbook within the Automation Account.
Add the PowerShell Script:
param (
[string]$vmName,
[string]$resourceGroupName,
[string]$scriptPath,
[string]$sshUsername,
[string]$sshPassword
)
# Authenticate with Azure
$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
Connect-AzAccount -ServicePrincipal -Tenant $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
# Get the public IP of the VM
$publicIP = (Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName -Name "$vmName-ip").IpAddress
# Use SSH to run the shell script on the Linux VM
$command = "sshpass -p $sshPassword ssh -o StrictHostKeyChecking=no $sshUsername@$publicIP 'bash -s' < $scriptPath"
Invoke-Expression $command
- Save and publish the runbook.
Create a Webhook for the Runbook
- Create a Webhook:
- In the Automation Account, navigate to the runbook you just created.
- Click on
WebhooksunderResources. - Click
Add a webhook. - Fill in the details and set the parameters you need (e.g.,
vmName,resourceGroupName,scriptPath,sshUsername,sshPassword). - Copy the webhook URL (you will need this for ADF).
- Click on
Use Webhook Activity in Azure Data Factory (ADF)
- Create a new Pipeline:
- In ADF, create a new pipeline.
- Add a Web Activity:
- From the
Activitiespane, drag aWebactivity to the pipeline canvas. - Click on the
Webactivity to configure it.
- From the
- Configure the Web Activity:
- In the
Settingstab, set the following: - URL: Paste the webhook URL you copied earlier.
- Method: POST
- Headers: Set
Content-Typetoapplication/json. - Body: Construct the JSON body with the parameters required by your runbook, e.g.:
{ "vmName": "your-vm-name", "resourceGroupName": "your-resource-group", "scriptPath": "/path/to/your/script.sh", "sshUsername": "your-ssh-username", "sshPassword": "your-ssh-password" }
- In the
- Debug the pipeline to ensure it works correctly. Once confirmed, you can trigger the pipeline based on your requirements.
This setup involves creating an Azure Automation Runbook with PowerShell to execute a Linux shell script on an Azure VM and using an ADF Webhook activity to trigger the runbook. Make sure to replace placeholder values with your actual Azure resources and credentials.
Hope this helps. Do let us know if you have any further issues!
If this answers your query, do click `Accept Answer` and `Yes` for was this answer helpful. And, if you have any further query do let us know.