本文內(nèi)容
問題描述
希望通過 Python 批量創(chuàng)建 ARM 虛擬機(jī)秤掌,并且在虛擬機(jī)命名時(shí)加入固定 IP 信息,方便管理維護(hù)静尼。
問題分析
在創(chuàng)建 ARM 虛擬機(jī)之前瑰排,先創(chuàng)建固定 IP,然后獲取固定 IP 地址霹菊,創(chuàng)建虛擬機(jī)時(shí)通過該 IP 地址格式化虛擬機(jī)名稱。然后將固定 IP 配置到網(wǎng)絡(luò)接口支竹,基于該網(wǎng)絡(luò)接口配置創(chuàng)建 ARM 虛擬機(jī)旋廷。
解決方法
模塊安裝
本文在 Windows Python 環(huán)境下進(jìn)行測(cè)試,環(huán)境及模塊依賴如下:
官網(wǎng)下載 msi 安裝包礼搁,管理員命令行執(zhí)行以下安裝腳本
復(fù)制
msiexec /package python-xxx.msi
使用 PIP 安裝 Azure(需要 pip 9+ 支持饶碘,Python 2.7 環(huán)境已內(nèi)置 pip 9+ 版本,不需更新)
復(fù)制
pip install azure
安裝程序依賴的模塊
復(fù)制
pip install azure-mgmt-network==0.30.0rc6
pip install azure-mgmt-compute==0.30.0rc6
pip install azure-mgmt-resource==0.30.2
如何查看模塊的版本
復(fù)制
pip install azure-mgmt-network==
Collecting azure-mgmt-network==
Could not find a version that satisfies the requirement azure-mgmt-network==
from versions: 0.20.0rc1, 0.20.0rc2, 0.20.0, 0.20.1, 0.30.0a1, 0.30.0rc1, 0.30.
rc2, 0.30.0rc3, 0.30.0rc4, 0.30.0rc5, 0.30.0rc6, 0.30.0)
No matching distribution found for azure-mgmt-network==
代碼實(shí)現(xiàn)
復(fù)制
from azure import *
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.resource import ResourceManagementClient
from azure.common.credentials import UserPassCredentials
from azure.mgmt.compute.models import *
from msrest.serialization import *
credentials = UserPassCredentials(
"訂閱賬戶",
"賬戶密碼",
china=True
)
resource_client = ResourceManagementClient(
credentials,
'訂閱 ID',
base_url = 'https://management.chinacloudapi.cn'
)
resource_client.providers.register('Microsoft.Compute')
resource_client.providers.register('Microsoft.Network')
compute_client = ComputeManagementClient(
credentials,
'訂閱 ID',
base_url = 'https://management.chinacloudapi.cn'
)
network_client = NetworkManagementClient(
credentials,
'訂閱 ID',
base_url = 'https://management.chinacloudapi.cn'
)
# Create Public IP
# result = network_client.public_ip_addresses.create_or_update(
#? ? 'geogroup',? ? #group_name
#? ? 'geo-ip-01',? ? ? ? #ip_name
#? ? PublicIPAddress(
#? ? ? ? location='China North',
#? ? ? ? public_ip_allocation_method=IPAllocationMethod.static,
#? ? ? ? idle_timeout_in_minutes=4,
#? ? ? ? ),
#? ? )
# result.wait()
public_ip_addresses = 'public_ip_name'
group_name = ''public_ip_group'
result = network_client.public_ip_addresses.get(group_name,public_ip_addresses)
print result.__dict__.items()
print result.ip_address
print result.ip_address.replace(".","-")
storageName = "storage account name"
vmName = "geovm-"+result.ip_address.replace(".","-")
print vmName
location = "chinanorth"
print location
os_profile = OSProfile(
computer_name= vmName,
admin_username='username',
admin_password='password,
)
print os_profile
hardware_profile = HardwareProfile(
vm_size=VirtualMachineSizeTypes.standard_a0
)
print hardware_profile
storage_profile = StorageProfile(
os_disk=OSDisk(
caching=CachingTypes.none,
create_option=DiskCreateOptionTypes.from_image,
name=vmName,
vhd=VirtualHardDisk(
uri='https://'+storageName+'.blob.core.chinacloudapi.cn/vhds/'+vmName+'.vhd',
),
),
)
storage_profile.image_reference = ImageReference(
publisher='Canonical',
offer='UbuntuServer',
sku='16.04.0-LTS',
version='latest'
)
print storage_profile
network_profile = NetworkProfile(
network_interfaces=[
NetworkInterfaceReference(
id="在新門戶馒吴,網(wǎng)絡(luò)接口-屬性中獲取資源 ID,該網(wǎng)絡(luò)接口需要配置固定 IP",
),
],
)
print network_profile
params_create = VirtualMachine(
location=location,
os_profile=os_profile,
hardware_profile=hardware_profile,
network_profile=network_profile,
storage_profile=storage_profile,
)
print params_create
result_create = compute_client.virtual_machines.create_or_update(
group_name,
vmName,
params_create
)
result_create.wait()
print 'ok'
立即訪問http://market.azure.cn