How to push commands to multiple azure linux vms

arun kumar 71 Reputation points
2023-07-26T09:23:32.88+00:00

Hi, is there any way to push commands to Azure multiple linux vms to modify settings or configuring applications? Can we use azure cli or powershell for this purpose?

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
9,034 questions
{count} votes

2 answers

Sort by: Most helpful
  1. JimmySalian-2011 42,496 Reputation points
    2023-07-26T11:32:51.4233333+00:00

    Hi Arun,

    Yes it is possible and one way preferred is PowerShell so check this blog and it should assist you. - https://www.thomasmaurer.ch/2021/03/how-to-run-scripts-against-multiple-azure-vms-by-using-run-command/

    https://learn.microsoft.com/en-us/azure/virtual-machines/linux/run-command

    Hope this helps.

    JS

    ==

    Please accept as answer and do a Thumbs-up to upvote this response if you are satisfied with the community help. Your upvote will be beneficial for the community users facing similar issues.

    0 comments No comments

  2. vipullag-MSFT 26,487 Reputation points Moderator
    2023-07-27T05:24:31.54+00:00

    Hello arun kumar

    Welcome to Microsoft Q&A Platform, thanks for posting your query here.

    Adding to previous response from JimmySalian, Yes, you can use Azure CLI or PowerShell to push commands to multiple Linux VMs in Azure. One way to do this is by using Azure Virtual Machine Extensions. Extensions are small applications that provide post-deployment configuration and automation tasks on Azure VMs. You can use extensions to install and configure software, manage users and passwords, and execute scripts.

    To push commands to multiple Linux VMs, you can create a custom script extension that runs your commands on the VMs**.** You can use Azure CLI or PowerShell to create and manage extensions.
    Here's an example of how to create a custom script extension using Azure CLI:

    az vm extension set \
      --resource-group myResourceGroup \
      --vm-name myVM \
      --name customScript \
      --publisher Microsoft.Azure.Extensions \
      --version 2.0 \
      --settings '{"fileUris":["https://mystorageaccount.blob.core.windows.net/scripts/myscript.sh"],"commandToExecute":"./myscript.sh"}'
    

    This command creates a custom script extension that downloads a script from a storage account and executes it on the VM. You can use this command to create the extension on multiple VMs by running it in a loop or a script.

    You can also use Azure Automation to run PowerShell scripts on multiple VMs. Azure Automation is a cloud-based automation and configuration service that provides a way to manage your resources at scale. You can use Azure Automation to run PowerShell scripts on Azure VMs, on-premises servers, and other cloud platforms.
    Here's an example of how to run a PowerShell script on multiple VMs using Azure Automation:

    1. Create an Azure Automation account and configure it to manage your VMs.
    2. Create a PowerShell script that contains the commands you want to run on the VMs.
    3. Create an Azure Automation runbook that runs the PowerShell script.
    4. Schedule the runbook to run on a regular basis or trigger it manually.

    When the runbook runs, it executes the PowerShell script on the VMs you specified. You can use Azure Automation to manage and monitor the execution of the script.

    Ref: https://learn.microsoft.com/en-us/azure/automation/learn/automation-tutorial-runbook-textual

    https://learn.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows

    Hope this helps.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.