I have an Azure CLI container that I have running. I would like to send 2 commands to the container;
- Find resources tagged with X :
az resource list --tag az=test --query "[].id" -otsv
- Delete resources tagged with X:
az resource delete --ids $(az resource list --tag az=test --query "[].id" -otsv)
My image/container has environment variables coded into it, so if I were to run any Az command, it will run against the Service Principals saved within it.
If I were to log into the container and run the command in one line, it will work just fine:
λ docker run -it asdf sh
/bin # az resource delete --ids $(az resource list --tag az=test --query "[].id" -otsv)
/bin #
But if I were to run the command outside of the container (or image), it will want me to log into Az CLI:
λ docker run asdf az resource delete --ids $(az resource list --tag az=test --query "[].id" -otsv)
Please run 'az login' to setup account.
ERROR: az resource delete: error: argument --ids: expected at least one argument
usage: az resource delete [-h] [--verbose] [--debug]
[--output {json,jsonc,table,tsv}] [--query JMESPATH]
[--ids RESOURCE_IDS [RESOURCE_IDS ...]]
[--resource-group RESOURCE_GROUP_NAME]
[--namespace RESOURCE_PROVIDER_NAMESPACE]
[--parent PARENT_RESOURCE_PATH]
[--resource-type RESOURCE_TYPE]
[--name RESOURCE_NAME] [--api-version API_VERSION]
[--subscription _SUBSCRIPTION]
It seems bash looks at the $(..)
command and doesn't send that through to the image/container. I have tried escaping characters with the \
, but it will bring back some other random error where I know -otsv
does actually work.
λ docker run asdf az resource delete --ids \$\(az resource list --tag az=test --query "[].id" -ots
v\)
ERROR: az resource delete: 'tsv)' is not a valid value for '--output'. See 'az resource delete --help'.
The most similar choice to 'tsv)' is:
tsv
I'm new to Bash, and I usually use PowerShell, but we have to go with Bash this time. Usually in PowerShell I could pipe the search results into another command to delete the resources, all in one line... but, I've no idea how to do that in this case.
Any ideas, please?
FYI: I will be sending automated commands from Azure Functions to this running container to execute the deletion of said resources, so I can't run an interactive shell.