'必需的参数'adminPassword'丢失(空)”,同时通过azure python sdk使用ssh密钥身份验证创建vmss

问题描述 投票:0回答:1

我正在尝试通过 azure python sdk 创建虚拟机规模集,并且我正在使用以下 JSON 模式对象参数

{
   "name":"TEST_CLUSTER",
   "location":"westeurope",
   "sku":{
      "tier":"Standard",
      "capacity":1,
      "name":"Standard_B1s"
   },
   "virtual_machine_profile":{
      "storage_profile":{
         "image_reference":{
            "id":"some_id",
            "offer":null,
            "sku":null,
            "version":null
         },
         "os_disk":{
            "caching":"ReadWrite",
            "create_option":"FromImage",
            "disk_size_gb":30,
            "managed_disk":{
               "storage_account_type":"Premium_LRS"
            }
         }
      },
      "os_profile":{
         "computer_name_prefix":"TESTAMSETUP",
         "admin_username":"testuser",
         "admin_password":null,
         "linux_profile":{
            "disable_password_authentication":true,
            "provision_vm_agent":true,
            "ssh":{
               "public_keys":[
                  {
                     "path":"/some/key/path",
                     "key_daya":"some-key-data"
                  }
               ]
            },
            "patch_settings":{
               "patch_mode":"ImageDefault",
               "assessment_mode":"ImageDefault"
            }
         }
      },
      "network_profile":{
         "network_interface_configurations":[
            {
               "name":"testnetworkinterface",
               "primary":null,
               "ip_configurations":[
                  {
                     "name":"testnetworkinterface",
                     "properties":{
                        "subnet":{
                           "id":"some_id"
                        }
                     }
                  }
               ]
            }
         ]
      }
   },
   "upgrade_mode":"Manual",
   "upgrade_policy":{
      "mode":"Manual"
   }
}

我希望使用 ssh 密钥身份验证创建 vmss,因为我已将

disable_password_authentication
指定为
true
,但运行我的脚本后,我收到以下错误消息。

Error azure_create_scale_set.py: (InvalidParameter) Required parameter 'adminPassword' is missing (null).
Code: InvalidParameter
Message: Required parameter 'adminPassword' is missing (null).
Target: adminPassword

我发现了与同一问题相关的github问题。然而,对于 github 中的 OP,通过将

disable_password_authentication
设置为
true
解决了问题。我的问题是如何使用 azure python sdk 创建 vmss,仅使用 ssh-key 身份验证选项。

azure ssh-keys azure-python-sdk virtual-machine-scale-set
1个回答
0
投票

我的问题是如何使用 Azure python sdk 创建 vmss ssh-key 身份验证选项。

您可以使用以下代码使用 Azure Python SDK 通过 ssh-key

 作为 
disable_password_authentication 创建具有
true
 身份验证选项的虚拟机规模集。

代码:

from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient

credential=DefaultAzureCredential()
subscription_id="xxxx"
client=ComputeManagementClient(credential=credential,subscription_id=subscription_id)

response = client.virtual_machine_scale_sets.begin_create_or_update(
        resource_group_name="xxxx",
        vm_scale_set_name="xxxx",
        parameters={
            "location": "eastus",
            "properties": {
                "overprovision": True,
                "upgradePolicy": {"mode": "Manual"},
                "virtualMachineProfile": {
                    "networkProfile": {
                        "networkInterfaceConfigurations": [
                            {
                                "name": "<Your vmss name>",
                                "properties": {
                                    "enableIPForwarding": True,
                                    "ipConfigurations": [
                                        {
                                            "name": "<Your vmss name>",
                                            "properties": {
                                                "subnet": {
                                                    "id": f"/subscriptions/{subscription_id}/resourceGroups/{resource_grp_name}/providers/Microsoft.Network/virtualNetworks/<Virtualnetwork name>/subnets/< Subnet1 >"
                                                }
                                            },
                                        }
                                    ],
                                    "primary": True,
                                },
                            }
                        ]
                    },
                    "osProfile": {
                        "adminPassword": "xxxx",
                        "adminUsername": "xxxx",
                        "computerNamePrefix": "xxx",
                        "linux_configuration": {
                            "disable_password_authentication": True,
                            "ssh": {
                                 "publicKeys": [
                                     {
                                         "keyData": xxxx",
                                         "path": "xxxxx"
                                     }
                                ]
                            }
                        }
                    },
                    "storageProfile": {
                        "imageReference": {
                            "offer": "UbuntuServer",
                            "publisher": "Canonical",
                            "sku": "16.04-LTS",
                            "version": "latest",
                        },
                        "osDisk": {
                            "caching": "ReadWrite",
                            "createOption": "FromImage",
                            "managedDisk": {"storageAccountType": "Standard_LRS"},
                        },
                    },
                },
            },
            "sku": {"capacity": 3, "name": "Standard_D1_v2", "tier": "Standard"},
        },
    ).result()
print(response)

输出:

{'additional_properties': {}, 'id': '/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Compute/virtualMachineScaleSets/vmss326', 'name': 'vmss326', 'type': 'Microsoft.Compute/virtualMachineScaleSets', 'location': 'eastus', 'tags': {'azsecpack': 'nonprod', 'platformsettings.host_environment.service.platform_optedin_for_rootcerts': 'true'}, 'sku': <azure.mgmt.compute.v2023_03_01.models._models_py3.Sku object at 0x000002658300F2D0>, 'plan': None, 'identity': None, 'zones': None, 'extended_location': None, 'upgrade_policy': <azure.mgmt.compute.v2023_03_01.models._models_py3.UpgradePolicy object at 0x000002658300D5D0>, 'automatic_repairs_policy': None, 'virtual_machine_profile': <azure.mgmt.compute.v2023_03_01.models._models_py3.VirtualMachineScaleSetVMProfile object at 0x000002658300EC90>, 'provisioning_state': 'Succeeded', 'overprovision': True, 'do_not_run_extensions_on_overprovisioned_v_ms': False, 'unique_id': '40ebf654-e85a-4a7b-932a-b65e0c8b4a14', 'single_placement_group': True, 'zone_balance': None, 'platform_fault_domain_count': None, 'proximity_placement_group': None, 'host_group': None, 'additional_capabilities': None, 'scale_in_policy': None, 'orchestration_mode': 'Uniform', 'spot_restore_policy': None, 'priority_mix_policy': None, 'time_created': datetime.datetime(2023, 12, 13, 15, 11, 17, 573683, tzinfo=<FixedOffset '+00:00'>), 'constrained_maximum_capacity': None}

传送门:

enter image description here

参考:

  1. 虚拟机规模集 - 创建或更新 - REST API(Azure 计算)|微软学习
  2. 如何使用现有/存储的 SSH 密钥对创建虚拟机? · 问题 #25990 · Azure/azure-sdk-for-python · GitHub,作者:Wzb123456789。
© www.soinside.com 2019 - 2024. All rights reserved.