Here’s a bit of a puzzle - all of the clues in this post, and the answer will follow in the next one. It starts with trying to write a simple bash script to create an Azure Resource Group.
Working in my Ubuntu WSL2 environment, I wanted to write a bash shell script that created an Azure Resource Group if a group with that name doesn’t already exist.
The idiomatic way to test whether a resource group exists by using the Azure CLI tool:
$ az group exists --resource-group demogroup
This command literally returns either true
or false
, and is designed for use in a conditional test like this one:
# Create resource group
if [ $(az group exists -n demogroup) == false ]; then
az group create -n demogroup --location eastus
else
echo "resource group exists"
fi
Unfortunately, when I ran this script fragment, I was dutifully informed the resource group existed:
$ ./provision.sh
resource group exists
This in spite of the fact the resource group did not exist!
$ az group exists -n demogroup
false
I hope you can appreciate that this was quite confusing.
Skipping a whole bunch of failed attempts to solve the problem, I eventually modified the script to pull out the result of the az command and write it to the console before the test:
# Create resource group
EXISTS=(az group exists -n demogroup)
echo "\$EXISTS=--$EXISTS--"
if [ $EXISTS == false ]; then
az group create -n demogroup --location eastus
else
echo "resource group exists"
fi
Running this script, I got the following surprising output:
$ ./provision.sh
--XISTS=--false
resource group exists
This was the clue that I needed to solve the problem - can you solve it too?
Comments
blog comments powered by Disqus