Sample bash script executing Azure CLI 2.0 commands, which can be executed on Linux (tested on Ubuntu) or Bash on Ubuntu on Windows 10

Hello everyone, this blog post directs you to a sample bash script that uses Azure CLI 2.0 that performs some deployment operations on Azure. There is a counterpart script based on PowerShell that can be found at https://gallery.technet.microsoft.com/Azure-Resource-Manager-6514f9ca?redir=0.

Basically this bash script with Azure CLI 2.0 deploys the following components:

  • 1 x Resource Group
  • 1 x Storage Account
  • 2 x Virtual Networks
  • 1 x Network Security Group
  • 2 x Linux Virtual Machines, one in each virtual network

This script requires JQ and Azure CLI 2.0 installed in your Linux/Bash on Ubuntu on Windows.

If you want to use Bash on Ubuntu on Windows 10, please refer to this documentation in order to enable it.

Installing Azure CLI 2.0 on Bash on Ubuntu on Windows

Azure CLI 2.0 is a new version of Azure Xplat CLI, and it is used to manage Azure (Resource Manager mode only) through command line on Windows, Linux and OS X.

  • Updating Python to the latest version (minimum supported is 2.7.6)

    • To check installed version: python –version,
    • Execute the following steps to update to the latest version if needed:
     sudo add-apt-repository ppa:fkrull/deadsnakes-python2.7
     sudo apt-get update 
     sudo apt-get install python2.7
    
  • Install other dependencies

     sudo apt-get update && sudo apt-get install -y libssl-dev libffi-dev python-dev
    

 

  • Install Azure CLI 2.0

     sudo su
     curl -L https://aka.ms/InstallAzureCli | bash
    

    When asked type the following answers:

     /usr/lib/azure-cli
     /usr/bin
     y
     /etc/bash.bashrc
    

 

  • Exit bash and open it again

 

Installing Azure CLI 2.0 on Ubuntu 16.10

  • These steps assumes you installed a VM based on Ubuntu 16.10 from Azure marketplace, perform upgrade on existing packages

     sudo apt-get update
     sudo apt-get upgrade –y
    

   

  • Install dependencies

     sudo apt-get update && sudo apt-get install -y libssl-dev libffi-dev python-dev build-essential
     sudo su
     curl -L https://aka.ms/InstallAzureCli | bash
    
  • When asked type the following answers:

     /usr/lib/azure-cli    
     /usr/bin
     y
     /etc/bash.bashrc
    
  • Logoff of this session and open session again

 

 

For more information about Azure CLI 2.0 please refer to /en-us/cli/azure/overview.



Installing JQ

jq is a json command-line json processor which helps querying (it also transforms) json strings to extract information.

  • On Bash on Ubuntu on Windows 10 prompt execute the following steps:

    • Install dependencies

       sudo apt-get install build-essential libtool autoconf gcc –y
      
    • Download and extract oniguruma and jq sources

       curl -L https://github.com/kkos/oniguruma/releases/download/v5.9.6_p1/onig-5.9.6_p1.tar.gz -o ./onig-5.9.6_p1.tar.gz
       curl -L https://github.com/stedolan/jq/releases/download/jq-1.5/jq-1.5.tar.gz -o jq-1.5.tar.gz
       tar -xvzf ./onig-5.9.6_p1.tar.gz
       tar -xvzf ./jq-1.5.tar.gz
      
    • Install oniguruma

       cd onig-5.9.6
       ./configure && make && sudo make install
      
    • Install jq

       cd jq-1.5
       ./configure && make && sudo make install
      
  • To install on full Ubuntu 16.10 execute the following:

     sudo apt-get install jq -y
    

For more information on jq, please refer to https://stedolan.github.io/jq/.

 

 

Running the script

The script can be manually downloaded from https://github.com/paulomarquesc/AzureCLI2.0BashDeployment or you can download directly using curl:

 curl -O https://raw.githubusercontent.com/paulomarquesc/AzureCLI2.0BashDeployment/master/azuredeploy.sh

 

Change the file properties to be able to be executed as a script:

 chmod 700 ./azuredeploy.sh

 

Authenticate to azure with az login (follow the steps to sign on using a browser and entering your Azure AD or MSA account that you use to manage Azure resources):

 az login

 

Basic Script syntax:

 ./azuredeploy.sh -s <subscription name> -r <resource group name> -l <location>

 

 

Hope that this blog post gives you a good visibility on how to use Azure CLI 2.0 and bash. This script was tested on Ubuntu 16.10 and Bash on Ubuntu on Windows 10 environments.

 

Regards

 

Paulo