'Required parameter 'adminPassword' is missing (null)", while creating vmss with ssh key auth via azure python sdk

180 Views Asked by At

I am trying to create virtual machine scale set via azure python sdk and I am using the following JSON patter object argument

{
   "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"
   }
}

I would expect the vmss to be created with ssh key authentication, as far as I have specified disable_password_authentication as true, but after running my script I get the following error message.

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

I found a github issue related to the same problem. However for OP in github the problem was resolved by setting disable_password_authentication as true. My question is how to create vmss with azure python sdk with just ssh-key authentication option.

1

There are 1 best solutions below

4
On BEST ANSWER

My question is how to create vmss with Azure python sdk with just ssh-key authentication option.

You can use the below code to create a virtual machine scale set with ssh-key authentication option by disable_password_authentication as true using Azure Python SDK.

Code:

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)

Output:

{'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

Portal:

enter image description here

Reference:

  1. Virtual Machine Scale Sets - Create Or Update - REST API (Azure Compute) | Microsoft Learn
  2. How to create a VM with an existing/stored SSH key pair? · Issue #25990 · Azure/azure-sdk-for-python · GitHub by Wzb123456789.

Update:

 "osProfile": {
                    "adminUsername": "azureuser",
                    "computerNamePrefix": "vmss326",
                    "linux_configuration": {
                        "disable_password_authentication": True,
                        "ssh": {
                             "publicKeys": [
                                 {
                                     "keyData": "xxxx",
                                     "path": "xxxx"
                                 }
                            ]
                        }
                    }
                }, 

Without passing adminPassword parameters in the code the virtual machine scale set is created successfully with SSH authentication.

enter image description here

Portal:

enter image description here