Python 基于固定 IP 来命名 ARM 虚拟机的实现
来源:互联网 发布:广联达软件怎么使用 编辑:程序博客网 时间:2024/06/06 01:19
问题描述
希望通过 Python 批量创建 ARM 虚拟机,并且在虚拟机命名时加入固定 IP 信息,方便管理维护。
问题分析
在创建 ARM 虚拟机之前,先创建固定 IP,然后获取固定 IP 地址,创建虚拟机时通过该 IP 地址格式化虚拟机名称。然后将固定 IP 配置到网络接口,基于该网络接口配置创建 ARM 虚拟机。
解决方法
模块安装
本文在 Windows Python 环境下进行测试,环境及模块依赖如下:
官网下载 msi 安装包,管理员命令行执行以下安装脚本
msiexec /package python-xxx.msi
使用 PIP 安装 Azure(需要 pip 9+ 支持,Python 2.7 环境已内置 pip 9+ 版本,不需更新)
pip install azure
安装程序依赖的模块
pip install azure-mgmt-network==0.30.0rc6pip install azure-mgmt-compute==0.30.0rc6pip install azure-mgmt-resource==0.30.2
如何查看模块的版本
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==
代码实现
from azure import *from azure.mgmt.compute import ComputeManagementClientfrom azure.mgmt.network import NetworkManagementClientfrom azure.mgmt.resource import ResourceManagementClientfrom azure.common.credentials import UserPassCredentialsfrom 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_addressprint result.ip_address.replace(".","-")storageName = "storage account name"vmName = "geovm-"+result.ip_address.replace(".","-")print vmNamelocation = "chinanorth"print locationos_profile = OSProfile( computer_name= vmName, admin_username='username', admin_password='password,)print os_profilehardware_profile = HardwareProfile( vm_size=VirtualMachineSizeTypes.standard_a0)print hardware_profilestorage_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_profilenetwork_profile = NetworkProfile( network_interfaces=[ NetworkInterfaceReference( id="在新门户,网络接口-属性中获取资源 ID,该网络接口需要配置固定 IP", ), ],)print network_profileparams_create = VirtualMachine( location=location, os_profile=os_profile, hardware_profile=hardware_profile, network_profile=network_profile, storage_profile=storage_profile,)print params_createresult_create = compute_client.virtual_machines.create_or_update( group_name, vmName, params_create)result_create.wait()print 'ok'
立即访问http://market.azure.cn
阅读全文
0 0
- Python 基于固定 IP 来命名 ARM 虚拟机的实现
- Python 基于固定 IP 来命名 ARM 虚拟机的实现
- vmware虚拟机实现双网卡固定ip
- OpenStack如何创建固定IP的虚拟机
- ubuntu 虚拟机设置固定的ip
- 虚拟机centos配置nat的固定IP
- 虚拟机下实现:固定ip+能上网+主机虚拟机互连
- 基于ARM的嵌入式TCP/IP协议的实现
- 基于ARM的嵌入式TCP/IP协议的实现
- 基于ARM的嵌入式TCP/IP协议的实现
- 基于ARM的嵌入式TCP/IP协议的实现方法
- 基于ARM的嵌入式TCP/IP协议的实现
- 虚拟机设定固定ip
- 虚拟机固定ip配置
- VM虚拟机固定IP
- 虚拟机设定固定ip
- Mac固定虚拟机IP
- Nginx实现虚拟机(1.基于ip地址的虚拟主机)
- js文件中引用另一个js文件
- MySQL的读写分离
- LeetCode-Easy-Java-Maximum Average Subarray I
- iOS开发手机号正则表达式
- 使用WKWebView注入js的详细用法及实时获取到WKWebWiew高度的方法
- Python 基于固定 IP 来命名 ARM 虚拟机的实现
- git 常用命令简写
- JMeter二次开发(2)-编写 JSON Assertion 插件
- Nosql是什么
- SpringMVC Controller介绍及常用注解
- java 中StringBuilder类的常用方法
- 用Nginx实现https转http
- sd卡tf卡进入spi模式
- 创建一个springboot项目