Adding userdata on create VM operation with Python SDK for Azure(使用Python SDK for Azure在创建虚拟机操作中添加用户数据)
本文介绍了使用Python SDK for Azure在创建虚拟机操作中添加用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Python SDK进行Azure创建虚拟机操作。我希望在VM启动时执行一些脚本。因此,我尝试在使用Python创建VM时添加自定义数据。
我的目录:
- user-data.sh
- create_VM.py
user-data.sh文件类似于:
#!/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
Message: Custom data in OSProfile must be in Base64 encoding and with a maximum length of 87380 characters.
Target: customData
如何解决此问题?
推荐答案
请试试这件,它在我的身上起作用。
import base64
...
file = open("user-data.sh", "r")
a = file.read().encode()
...
CUSTOM_DATA = base64.b64encode(a.encode('utf-8')).decode('latin-1')
这篇关于使用Python SDK for Azure在创建虚拟机操作中添加用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用Python SDK for Azure在创建虚拟机操作中添加用户数据


基础教程推荐
猜你喜欢
- 症状类型错误:无法确定关系的真值 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01