Skip to content

Instantly share code, notes, and snippets.

@fearphage
Created September 11, 2019 17:26
Show Gist options
  • Save fearphage/d14f1cc08124e6cd5d22aca82f9f2e23 to your computer and use it in GitHub Desktop.
Save fearphage/d14f1cc08124e6cd5d22aca82f9f2e23 to your computer and use it in GitHub Desktop.
Collection of shell functions wrapping the Azure CLI

az-get

Returns a property value of a resource passing additional params as needed.

Usage

az-get <resource> <property> [additional params]

Example

id=$(az-get image id --name my-image-name --resource-group my-resource-group-name)

# allows for deeply nested properties
agent=$(az-get vm osProfile.linuxConfiguration.provisionVmAgent --name vm-name --resource-group my-group)

az-group

Returns a list of group names resulting from az group list. By default it will list all groups. All passed params will be searched for in the names of the groups using an or (||).

Usage

az-group [search strings]

Calling az-group without parameters is idential to az group list --query '[].name' --output tsv.

Calling it with params will search for the substring(s) in the names of the groups.

Example

# deletes all groups with the substring of deleteme *OR* killme
az-group deleteme killme | xargs --max-args 1 --no-run-if-empty az group delete --yes --name

Notes: --no-run-if-empty is a GNU extensions and thus doesn't work on a Mac.

function az-get {
# Usage: id=$(az-get image id --name $name --resource-group $group)
local property
local resource
resource="$1"
property="$2"
shift
shift
az "$resource" show "$@" --query "$property" --output tsv
}
function az-group {
local query
if [ $# -gt 0 ]; then
# cross-shell (bash, zsh, etc.) array.join() support
query="?contains(name, '$(python -c 'import sys;print sys.argv[1].join(sys.argv[2:])' "') || contains(name, '" "$@")')"
fi
az group list --query "[$query].name" --output tsv
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment