在此先感谢,我正在尝试更新 Azure VM。最终,我的代码从 Azure 密钥保管库获取证书并将其保存在本地证书存储中。我已经在 Bash 上成功使用 Azure CLI。代码在下面
secret=$(az keyvault secret list-versions --vault-name aqrahyhkeyvault --name certificatename --query "[?attributes.enabled].id" --output tsv)
vm_secret=$(az vm secret format --secrets "$secret" --resource-group RAH-AQ --keyvault aqrahyhkeyvault --certificate-store My)
az vm update -g Archive-WSL -n win10new --set osProfile.secrets="$vm_secret"
我通过将它包装在 Python 中来使用相同的命令,因为我的大部分代码都是这种格式。但它抛出了无效的语法错误。我已经用双引号尝试了所有可能的更改,但没有运气
import subprocess
import json
def Update_vm(vault_name,certificate_name,rscgroup_name):
Secret_command=["az","keyvault","secret","list-versions","--vault-name",vault_name,"--name",certificate_name,"--query","[?attributes.enabled].id","--output","tsv"]
create_vm=subprocess.run(Secret_command, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
print(create_vm.stdout)
vm_secret=["az","vm","secret","format","--secrets",create_vm.stdout,"--resource-group",rscgroup_name,"--keyvault",vault_name,"--certificate-store","My"]
vm_new_secret=subprocess.run(vm_secret, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
print(vm_new_secret.stdout)
update_vm_cmd=["az","vm","update","-g",rscgroup_name,"-n",avm_name,"--set","osProfile.secrets"=vm_new_secret.stdout] //Error is present here saying invalid syntax
vm_update=subprocess.run(update_vm_cmd, stdout=subprocess.PIPE, stderr = subprocess.PIPE)
if __name__=="__main__":
rscgroup_name="vm-test-group"
avm_name="testvm1"
avm_image="Win2019Datacenter"
avm_username="azuretest"
avm_password="mypass"
avm_size="Standard_D2_V3"
vault_name = "aqrahkeyvault"
certificate_name = "staticwebsite"
Update_vm(vault_name,certificate_name,rscgroup_name)