Python 基于固定 IP 來命名 ARM 虛擬機(jī)的實(shí)現(xiàn)

問題描述

希望通過 Python 批量創(chuàng)建 ARM 虛擬機(jī)舟陆,并且在虛擬機(jī)命名時加入固定 IP 信息,方便管理維護(hù)浮创。

問題分析

在創(chuàng)建 ARM 虛擬機(jī)之前,先創(chuàng)建固定 IP养筒,然后獲取固定 IP 地址曾撤,創(chuàng)建虛擬機(jī)時通過該 IP 地址格式化虛擬機(jī)名稱。然后將固定 IP 配置到網(wǎng)絡(luò)接口晕粪,基于該網(wǎng)絡(luò)接口配置創(chuàng)建 ARM 虛擬機(jī)挤悉。

解決方法

模塊安裝

本文在 Windows Python 環(huán)境下進(jìn)行測試,環(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末诀诊,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子阅嘶,更是在濱河造成了極大的恐慌属瓣,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件讯柔,死亡現(xiàn)場離奇詭異抡蛙,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)魂迄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進(jìn)店門粗截,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人捣炬,你說我怎么就攤上這事熊昌。” “怎么了湿酸?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵浴捆,是天一觀的道長。 經(jīng)常有香客問我稿械,道長,這世上最難降的妖魔是什么冲粤? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任美莫,我火速辦了婚禮,結(jié)果婚禮上梯捕,老公的妹妹穿的比我還像新娘厢呵。我一直安慰自己,他們只是感情好傀顾,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布襟铭。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪寒砖。 梳的紋絲不亂的頭發(fā)上赐劣,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天,我揣著相機(jī)與錄音哩都,去河邊找鬼魁兼。 笑死,一個胖子當(dāng)著我的面吹牛漠嵌,可吹牛的內(nèi)容都是我干的咐汞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼儒鹿,長吁一口氣:“原來是場噩夢啊……” “哼化撕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起约炎,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤植阴,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后章钾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體墙贱,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年贱傀,在試婚紗的時候發(fā)現(xiàn)自己被綠了惨撇。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡府寒,死狀恐怖魁衙,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情株搔,我是刑警寧澤剖淀,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站纤房,受9級特大地震影響纵隔,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜炮姨,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一捌刮、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧舒岸,春花似錦绅作、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽个少。三九已至,卻和暖如春眯杏,著一層夾襖步出監(jiān)牢的瞬間夜焦,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工役拴, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留糊探,地道東北人。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓河闰,卻偏偏與公主長得像科平,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子姜性,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容