1

I have need to reboot all Nodes in Azure Batch. How can i do that using Azure CLI ?

Essentially how can i iterate over List/Collection in Azure CLI and call command within that loop to stop the VM

Shui shengbao
  • 18,746
  • 3
  • 27
  • 45
NSS
  • 1,835
  • 2
  • 29
  • 66

1 Answers1

1

There is no one command can do this, but you could use a script to reboot all nodes. If you use bash shell, you could use the following example:

pools="$(az batch pool list |grep id|awk -F\" '{print $4}')"
for pool in $pools
{
  nodes="$(az batch node list --pool-id $name|grep -F "id\":"|awk -F\" '{print $4}')"
      for node in $nodes
      {
          az batch node reboot --node-id $node --pool-id $pool
      }
}

If you could use Power Shell, you could reboot all nodes by using

Get-AzureBatchComputeNode -PoolId "MyPool" -BatchContext $Context | Restart-AzureBatchComputeNode -BatchContext $Context

More information about this see this link.

Shui shengbao
  • 18,746
  • 3
  • 27
  • 45
  • 1
    If you use cli on Windows, please let me know, I could change my script. – Shui shengbao Mar 29 '18 at 06:00
  • 1
    In general, I would recommend using [--query](https://learn.microsoft.com/en-us/cli/azure/query-azure-cli?view=azure-cli-latest) to select fields that I am interested in and [--output tsv](https://learn.microsoft.com/en-us/cli/azure/format-output-azure-cli?view=azure-cli-latest) to get raw text output from az commands. It is rare that grep and awk are needed. – Johan Stenberg - MSFT Mar 30 '18 at 16:14
  • @JohanStenberg-MSFT Honestly say, I am a Linux script lover. I more like use `sed` and `awk`. Yes, maybe `--query` could get the value. But it gets a json file. I need use `jq` to filter again. Hi, if you are a Azure CLI developer, I suggest you could optimize it. – Shui shengbao Apr 02 '18 at 02:49
  • The use of --query and expected output format are orthogonal. In other words, using --query does not imply --output json. One of the reasons that the CLI support --output tsv is to support *exactly* the scenario of extracting a one or more scalar values from the output and pass them as a set of space (well, tab actually) separated values to your favorite next command. – Johan Stenberg - MSFT Apr 03 '18 at 15:07