0

I have below commands for run az aks command:

from azure.cli.core import get_default_cli
az_cli = get_default_cli()
res = az_cli.invoke(['login', '--service-principal', '-u', client_id, '-p', client_secret,'--tenant',tenant_id])
az_cli.invoke(['aks','command','invoke','--resource-group',resourcegroup,'--name',clustername,'--command','kubectl apply -f',outfile_final])

I want as below,

azcmd = "az login --service-principal -u " + client_id + " -p " + client_secret + " --tenant " + tenant_id

**res = az_cli.invoke([azcmd])**

but Above script is giving error like args should be a list or tuple, and 2nd error: enter image description here Is there anyways to run invoke with get input from variable.

Edit1: I'm applying deployment file as below:

namespace = "kubectl apply -f abc.yaml"
2
  • Could please Elaborate what are you trying? Are you trying to create aks cluster Via CLI? Commented Dec 22, 2022 at 12:20
  • @RithwikBojja , question updated. Commented Dec 22, 2022 at 13:28

1 Answer 1

1

I tried in my environment and got below results:

Initially I tried with same command and got same error:

enter image description here

I installed azure-cli module in my local machine, Also instead of using -

res = az_cli.invoke([azcmd])

You can add your azcmd inside ([ ])

from  azure.cli.core  import  get_default_cli
az_cli = get_default_cli()
cmd=(['login', '--service-principal', '-u', client_id, '-p', client_secret,'--tenant',tenant_id])

res=az_cli.invoke(cmd)

Is there anyways to run invoke with get input from variable.

With above code I can invoke with get input from variable.

Console:

enter image description here

Reference: Authenticating Azure CLI with Python SDK - Stack Overflow by Jim Xu.

Sign up to request clarification or add additional context in comments.

7 Comments

Venkatesan, Thanks for the response. az login is working fine but below command is not working ---------------- namespace1 = "nirav" az_cli.invoke(['aks','command','invoke',resourcegroup,'--name',clustername,'--command','kubectl delete namespaces',namespace1])
Hi @microset I tried with above mentioned commands in my environment, and it executed successfully.here is my Snapshot
Thanks again Venkatesan , Can you please send me line no 4 from the you send the link of snapshot i.imgur.com/n8XpNBK.png , I need to check what is wrong with my code.
Here is my full code: from azure.cli.core import get_default_cli az_cli = get_default_cli() az_cli.invoke(['login', '--service-principal', '-u','client-id', '-p', 'client secret','--tenant','tenant-id']) cmd=(['aks','command','invoke','--resource-group','myResourceGroup','--name','myAKSCluster','--command','kubectl delete namespaces namespace1']) res=az_cli.invoke(cmd)
Thanks Once Again for spend your valuable for my issue. Issue is resolved now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.