使用Python SDK for Azure在创建VM操作中添加userdata
我正在使用Python sdk进行azure创建虚拟机操作。我希望在VM启动时执行一些脚本。因此,我尝试在使用Python创建VM时添加自定义数据 我的目录: user-data.sh 创建_VM.py user-data.sh文件与此类似:使用Python SDK for Azure在创建VM操作中添加userdata,python,azure,cloud,azure-cli,azure-sdk-python,Python,Azure,Cloud,Azure Cli,Azure Sdk Python,我正在使用Python sdk进行azure创建虚拟机操作。我希望在VM启动时执行一些脚本。因此,我尝试在使用Python创建VM时添加自定义数据 我的目录: user-data.sh 创建_VM.py user-data.sh文件与此类似: #!/bin/bash sudo apt install apache2 -y sudo apt install git -y 我在create_VM.py文件中使用了base64包,它看起来像: import base64 ... file = o
#!/bin/bash
sudo apt install apache2 -y
sudo apt install git -y
我在create_VM.py文件中使用了base64包,它看起来像:
import base64
...
file = open("user-data.sh", "r")
a = file.read().encode()
encoded_string = base64.b64encode(a)
...
poller = compute_client.virtual_machines.create_or_update(RESOURCE_GROUP_NAME, VM_NAME,
{
"location": LOCATION,
"storage_profile": {
"image_reference": {
"publisher": 'Canonical',
"offer": "UbuntuServer",
"sku": "16.04.0-LTS",
"version": "latest"
}
},
"hardware_profile": {
"vm_size": "Standard_DS1_v2"
},
"os_profile": {
"computer_name": VM_NAME,
"admin_username": USERNAME,
"admin_password": PASSWORD,
"custom_data": encoded_string
},
"network_profile": {
"network_interfaces": [{
"id": nic_result.id,
}]
}
}
)
...
我得到的错误是:
Azure Error: InvalidParameter\nMessage: Custom data in OSProfile must be in Base64 encoding and with a maximum length of 87380 characters.\nTarget: customData
我如何解决这个问题?请试试这个,它对我的有效
import base64
...
file = open("user-data.sh", "r")
a = file.read().encode()
...
CUSTOM_DATA = base64.b64encode(a.encode('utf-8')).decode('latin-1')
为什么要使用这个属性?似乎你应该为此目的使用云计算init。我是azure新手。你能帮我做一下云初始化吗?一些示例代码和文档对我来说就足够了。谢谢。使用“azure VM Linux cloud init”查询,在Google上尝试第一次成功