0

I have an Azure CLI container that I have running. I would like to send 2 commands to the container;

  1. Find resources tagged with X : az resource list --tag az=test --query "[].id" -otsv
  2. 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.

Beefcake
  • 733
  • 2
  • 12
  • 37
  • Commands inside a `$(..)` run in a sub-shell. May be some variables that needed login information `az login` cannot be available in the subshell. – Inian Jul 30 '18 at 09:48

2 Answers2

0

The error says the reason. If you want to execute the azure Cli in the container, you can connect into the container using the command docker exec -it containerName bash, Or you can do what you do. But all of all, you should log in Azure CLI first.

For your second error, the parameter should be -o tsv.

Update 1

I test the command docker run imageName az resource delete and the result give the only error that please run 'az login' to set up account. enter image description here

So no matter what you want to do with Azure CLI, you should log in first.

Update 2

To achieve this, you can add & between the two command line. And the whole command will like this:

docker run docker_image_name az login & az resource delete --ids $(az resource list --name resource_name --query "[].id" -o tsv)

Because the command az login of the two will be executed first, so you have to log in first. But don't worry, the second command also will be executed after your login.

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • Yes, that would be the normal way to go, however, I will be sending automated commands from Azure Functions to this running container to execute the deletion of said resourcesm :/ – Beefcake Jul 30 '18 at 10:08
  • oh, you would be right about the 2nd error. but that still errors the same, and run in a shell with `-otsv` still works the same as `-o tsv` – Beefcake Jul 30 '18 at 10:16
  • That‘s strange. When I execute the command `docker run docker.io/microsoft/azure-cli az resource delete --ids $(az resource list --name charlesremoteapp --query "[].id" -o tsv)`, there is just the error that please run 'az login' to set up account. – Charles Xu Jul 31 '18 at 02:48
  • yes, I have that too (shown above). I believe it's like @Inian said, that whatever is in `$(...)` is taken as a sub-shell, and thus is using your shell to perform whatever that action is. So, if you're not signed into Azure, it will error asking you to sign in. – Beefcake Jul 31 '18 at 11:55
  • @Beefcake I find out a way to achieve your purpose and it works well. – Charles Xu Aug 01 '18 at 06:29
  • @Beefcake If the answer is helpful or for more help. please let me know. – Charles Xu Aug 02 '18 at 00:59
  • apologies for the late reply. sadly this will not work either, as the point of this exercise is to fire an up Azure Function and send the command off to the container to perform this action on a re-occurring schedule, so it must be automated without human interaction. So looking at the command, I want to find all resources with a specific tag, and then delete them all, but as mentioned, Bash sees the $(..) command to be run before it gets sent to the container :S – Beefcake Aug 10 '18 at 09:03
0

I finally get to come back to this after being side tracked. Turns out it was the inverted commas that are needed;

docker run -it asdf bash -c 'az resource delete --ids $(az resource list --tag az=test --query "[].id" -otsv)'

Thanks to this for giving me the idea; Execute two commands with docker exec

Beefcake
  • 733
  • 2
  • 12
  • 37