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"
microset
  • 276
  • 1
  • 2
  • 12

1 Answers1

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.

Venkatesan
  • 3,748
  • 1
  • 3
  • 15
  • 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]) – microset Jan 02 '23 at 10:01
  • Hi @microset I tried with above mentioned commands in my environment, and it executed successfully.here is my [Snapshot](https://i.imgur.com/n8XpNBK.png) – Venkatesan Jan 02 '23 at 11:10
  • Thanks again Venkatesan , Can you please send me line no 4 from the you send the link of snapshot https://i.imgur.com/n8XpNBK.png , I need to check what is wrong with my code. – microset Jan 02 '23 at 11:14
  • yeah, here it is cmd=az_cli.invoke(['aks','command','invoke','--resource-group','myResourceGroup','--name','myAKSCluster','--command','kubectl delete namespaces namespace1']) – Venkatesan Jan 02 '23 at 11:15
  • 1
    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) – Venkatesan Jan 02 '23 at 11:21
  • 1
    Thanks Once Again for spend your valuable for my issue. Issue is resolved now. – microset Jan 02 '23 at 11:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250788/discussion-between-microset-and-venkatesan). – microset Jan 02 '23 at 12:01