Using az cli commands on Azure Devops pipelines
Az cli tool can be proven an important tool to interact with Azure resources for various scenarios. Azure Devops has a build in task that is called AzureCLI@2 which will automatically connect and authenticate in your azure subscription given that a service connection is already created.
The important thing to run az cli commands is the azureSubscription field which should be set to point your service connection.
In the below sample you can find a pipeline that will use az cli to open a closed VM on azure.
trigger:
- nonepool:
vmImage: ubuntu-latestpr: noneparameters:
- name: vmname
type: stringsteps:
- checkout: self- task: AzureCLI@2
displayName: Close VM using az cli
inputs:
azureSubscription: 'GERALEXGR'
scriptType: 'pscore'
scriptLocation: 'inlineScript'
inlineScript: |
$vmrequest = az vm list | ConvertFrom-Json | Where-Object {$_.Name -Match "${{parameters.vmname}}"}
az vm start --resource-group $vmrequest.resourcegroup --name $vmrequest.name
Before the run of the pipeline the VM state is off.
In order to run the pipeline the VM name must be provided as an input. Then the vm name will be passed to az cli which will open the vm through the azure service connection that points in the subscription.
After pipeline runs successfully
The VM will be started on Azure