如何通过Ansible创建Azure VM且不自动生成存储账户?
问题:使用Ansible创建Azure VM时避免自动生成不必要的存储账户
在使用Ansible创建Azure Windows虚拟机时,Azure会自动生成一个不需要的存储账户,如何配置Ansible任务来避免这个问题?原任务代码如下:
- name: Create windows vms azure_rm_virtualmachine: resource_group: "{{ RG_VMS }}" name: "{{ item.key }}" vm_size: Standard_B2ms admin_username: user admin_password: "{{ password }}" network_interfaces: "{{ item.key }}-nic" os_type: Windows os_disk_name: disk{{ item.key }} os_disk_size_gb: 127 os_disk_caching: ReadWrite managed_disk_type: StandardSSD_LRS image: offer: WindowsServer publisher: MicrosoftWindowsServer sku: 2016-Datacenter version: latest tags: otapenvironment: "{{ OTAPENVIRONMENT }}" systemtype: "{{ item.value.systemtype }}" loop: "{{ WINDOWS_VMS | dict2items }}"
解决方案
核心原因
即使配置了托管磁盘,azure_rm_virtualmachine模块默认会自动创建存储账户,用于存放VM部署过程中的临时资源(如引导配置文件)。要阻止该行为,需明确指定一个已存在的存储账户供模块复用。
修改步骤
- 提前创建目标存储账户:在执行VM创建任务前,确保已在同一Azure区域或资源组下创建好存储账户。
- 添加
storage_account_name参数:在Ansible任务中指定已创建的存储账户名称,模块将复用该账户,不再自动生成新的。
修改后的代码
- name: Create windows vms azure_rm_virtualmachine: resource_group: "{{ RG_VMS }}" name: "{{ item.key }}" vm_size: Standard_B2ms admin_username: user admin_password: "{{ password }}" network_interfaces: "{{ item.key }}-nic" os_type: Windows os_disk_name: disk{{ item.key }} os_disk_size_gb: 127 os_disk_caching: ReadWrite managed_disk_type: StandardSSD_LRS # 指定已存在的存储账户,避免自动生成 storage_account_name: "your-existing-storage-account" image: offer: WindowsServer publisher: MicrosoftWindowsServer sku: 2016-Datacenter version: latest tags: otapenvironment: "{{ OTAPENVIRONMENT }}" systemtype: "{{ item.value.systemtype }}" loop: "{{ WINDOWS_VMS | dict2items }}"
额外说明
- 确保指定的存储账户与VM处于同一Azure区域,否则会导致部署失败。
- 若使用的Ansible Azure模块版本较旧,建议升级至最新稳定版,避免默认行为的兼容性问题。
内容的提问来源于stack exchange,提问作者Ukchat
相关产品推荐
相关产品推荐

