问题描述

希望通过 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.0rc6
    pip install azure-mgmt-compute==0.30.0rc6
    pip 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 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="在新门户,网络接口-属性中获取资源 ID,该网络接口需要配置固定 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

Python 基于固定 IP 来命名 ARM 虚拟机的实现的更多相关文章

  1. 学习Mysql过程中拓展的其他技术栈:设置linux虚拟机的固定ip和克隆linux虚拟机

    一.设置linux虚拟机的固定ip 1. 安装好虚拟机后在菜单栏选择编辑→ 虚拟网络编辑器,打开虚拟网络编辑器对话框,选择Vmnet8 Net网络连接方式,随意设置子网IP,点击NAT设置页面,查看子 ...

  2. 完整部署CentOS7.2+OpenStack+kvm 云平台环境(3)--为虚拟机指定固定ip

    之前在测试环境(centos7.2)上部署了openstack云平台(完整部署CentOS7.2+OpenStack+kvm 云平台环境(1)--基础环境搭建),openstack在neutron组网 ...

  3. 给虚拟机中的CentOS7配置固定ip

    在虚拟机中安装完了CentOS7之后,使用了DHCP来获取ip,vmware的网络连接使用了NAT模式.但是在把Linux设置为固定ip地址后,虚拟机里的linux可以ping通全网段的ip地址,但是 ...

  4. VMware虚拟机固定IP后克隆出现无法访问网卡问题

    通常我们现在都喜欢使用虚拟机进行实验,进行集群搭建等,在这个过程中,会遇到克隆虚拟机问题,当没有修改任何IP的情况下,克隆后,在逐台修改IP地址是没有问题的,但是,如果我们先设置了固定IP地址后,克隆 ...

  5. CentOS6.4虚拟机设置固定IP、安装JDK、Tomcat、Redis并部署web项目

    一.CentOS设置固定IP 1.直接修改配置文件的方式,原文地址:http://www.cnblogs.com/zhja/p/3964159.html (1)首先获取你的GATEWAY 方便后面在c ...

  6. VMware虚拟机安装CentOS6.4、部署web项目全过程(设置固定IP、安装JDK、Tomcat、Redis、部署项目)

    概述:该篇随笔介绍了在VMware上安装centOS.在centOS上安装JDK.安装Tomcat.安装Redis并部署项目的全过程,虽然参考了很多优秀的文章,但实践.整理.补充都很用心,若要复制粘贴 ...

  7. 虚拟机操作系统内设置固定IP以及克隆虚拟机

    以下为我自己整理的克隆虚拟机和设置固定IP的方法,记录一下,以防忘记: 桥接模式网络配置 1.配置ip地址等信息在/etc/sysconfig/network-scripts/ifcfg-ens33文 ...

  8. 虚拟机环境搭建/修改VMware虚拟机固定IP

    VMware Workstation安装CentOS7.0 详情教程: centos7.0下载地址:http://isoredirect.centos.org/centos/7/isos/x86_64 ...

  9. VMWare虚拟机设置固定ip上网方法

    转自:http://blog.csdn.net/cyberrusher/article/details/7269795 1. 在VMWare工具栏中打开:编辑--->虚拟机网络编辑器, 打开VM ...

随机推荐

  1. 分享一个大型进销存供应链项目(多层架构、分布式WCF多服务器部署、微软企业库架构)

    项目源码下载:  WWW.DI81.COM 分享一个大型进销存供应链项目(多层架构.分布式WCF多服务器部署.微软企业库架构) 这是一个比较大型的项目,准备开源了.支持N家门店同时操作.远程WCF+企 ...

  2. Luogu P1131 [ZJOI2007]时态同步 树形DP

    要自下向上调整,尽可能用一个道具修改多个: 搜的时候记录叶节点的最大深度,减一下就好了. #include<cstdio> #include<iostream> #includ ...

  3. 118th LeetCode Weekly Contest Pancake Sorting

    Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, t ...

  4. 深入剖析PHP输入流 php://input

    另附一个一个连接: http://www.nowamagic.net/academy/detail/12220520 ///////////////////////////////////////// ...

  5. MariaDB 密码,新用户添加

    修改root密码1.以root身份在终端登陆(必须)2.输入 mysqladmin -u root -p password ex后面的 ex 是要设置的密码3.回车后出现 Enter password ...

  6. 关于Ehcache缓存中timeToLiveSeconds和timeToIdleSeconds

    [From] http://blog.csdn.net/vtopqx/article/details/8522333 闲来无事测试了下Ehcache与MemCache比较,在此发现了Ehcache中一 ...

  7. HTML5必须知道的那些事

    [转自] http://www.cnblogs.com/hamy/archive/2012/02/21/2362110.html 再普及一次HTML5基础,HTML5必须知道的那些事,HTML5扫盲. ...

  8. PIE SDK矢量数据编辑的撤销和回退

    1.功能简介 在数据的编辑过程中难免会出现失误,撤销和回退可以更好的编辑,下面对矢量数据编辑的撤销和回退功能进行介绍. 2.功能实现说明 2.1. 实现思路及原理说明 第一步 调用UndoComman ...

  9. Navicat Premium Mac 12 破解(CV别人的,但是亲测能用)

    第一步:这部分暂时存到文本编辑器中 公钥: -----BEGIN PUBLIC KEY-----MIIBITANBgkqhkiG9w0BAQEFAAOCAQ4AMIIBCQKCAQB8vXG0ImYh ...

  10. 树莓派开启wlan功能

    烧好系统之后,通过网线连接树莓派到路由器.通过ip登入系统,修改interfaces文件,添加下面内容 sudo nano /etc/network/interfacesauto wlan0allow ...