azure.azcollection.azure_rm_virtualmachinescaleset 模块 – 管理 Azure 虚拟机规模集

注意

此模块是 azure.azcollection 集合(版本 3.1.0)的一部分。

如果您正在使用 ansible 包,您可能已经安装了这个集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用:ansible-galaxy collection install azure.azcollection。您需要进一步的要求才能使用此模块,请参阅 要求 了解详细信息。

要在 playbook 中使用它,请指定:azure.azcollection.azure_rm_virtualmachinescaleset

azure.azcollection 0.1.2 中的新增功能

概要

  • 创建和更新虚拟机规模集。

  • 请注意,在 Ansible 2.8 之前,此模块名为 azure.azcollection.azure_rm_virtualmachine_scaleset。用法没有改变。

要求

执行此模块的主机上需要以下要求。

  • python >= 2.7

  • 执行此模块的主机必须通过 galaxy 安装 azure.azcollection 集合

  • 必须通过 pip 在执行来自 azure.azcollection 的模块的主机上安装集合的 requirements.txt 中列出的所有 python 包

  • 完整的安装说明可以在 https://galaxy.ansible.com/azure/azcollection 中找到

参数

参数

注释

ad_user

字符串

Active Directory 用户名。当使用 Active Directory 用户而不是服务主体进行身份验证时使用。

adfs_authority_url

字符串

在 azure.azcollection 0.0.1 中添加

Azure AD 授权 URL。当使用用户名/密码进行身份验证,并且具有自己的 ADFS 授权时使用。

admin_password

字符串

管理员用户名的密码。

如果 os_type 为 Linux 并且通过设置 ssh_password_enabled=false 禁用 SSH 密码身份验证,则不是必需的。

admin_username

字符串

用于在创建后访问主机的管理员用户名。创建 VM 时必需。

api_profile

字符串

在 azure.azcollection 0.0.1 中添加

选择与 Azure 服务通信时要使用的 API 配置文件。默认值 latest 适用于公共云;未来的值将允许与 Azure Stack 一起使用。

默认值: "latest"

append_tags

布尔值

用于控制 tags 字段是规范的还是仅附加到现有标签。

当为规范时,任何在 tags 参数中未找到的标签都将从对象的元数据中删除。

选项

  • false

  • true ← (默认)

application_gateway

字符串

应用程序网关名称。

auth_source

字符串

在 azure.azcollection 0.0.1 中添加

控制用于身份验证的凭据的来源。

也可以通过 ANSIBLE_AZURE_AUTH_SOURCE 环境变量设置。

当设置为 auto(默认值)时,优先级为模块参数 -> env -> credential_file -> cli

当设置为 env 时,凭据将从环境变量中读取

当设置为 credential_file 时,它将从 ~/.azure/credentials 读取配置文件。

当设置为 cli 时,凭据将来自 Azure CLI 配置文件。subscription_id 或环境变量 AZURE_SUBSCRIPTION_ID 可用于标识订阅 ID(如果存在多个订阅),否则使用默认的 az cli 订阅。

当设置为 msi 时,主机必须是启用了 MSI 扩展的 Azure 资源。subscription_id 或环境变量 AZURE_SUBSCRIPTION_ID 可用于标识订阅 ID(如果资源被授予对多个订阅的访问权限),否则选择第一个订阅。

msi 在 Ansible 2.6 中添加。

选项

  • "auto" ← (默认)

  • "cli"

  • "credential_file"

  • "env"

  • "msi"

capacity

整数

VMSS 的容量。

默认值: 1

cert_validation_mode

字符串

在 azure.azcollection 0.0.1 中添加

控制 Azure 端点的证书验证行为。默认情况下,所有模块都将验证服务器证书,但是当使用 HTTPS 代理或针对 Azure Stack 时,可能需要通过传递 ignore 来禁用此行为。也可以通过凭据文件配置文件或 AZURE_CERT_VALIDATION 环境变量设置。

选项

  • "ignore"

  • "validate"

client_id

字符串

Azure 客户端 ID。当使用服务主体或托管标识 (msi) 进行身份验证时使用。

也可以通过 AZURE_CLIENT_ID 环境变量设置。

cloud_environment

字符串

在 azure.azcollection 0.0.1 中添加

对于美国公共云以外的云环境,环境名称(由 Azure Python SDK 定义,例如,AzureChinaCloudAzureUSGovernment),或者元数据发现终结点 URL(Azure Stack 需要)。也可以通过凭据文件配置文件或 AZURE_CLOUD_ENVIRONMENT 环境变量设置。

默认值: "AzureCloud"

custom_data

字符串

提供给虚拟机并被例如 cloud-init 使用的数据。

市场中的许多镜像并未准备好使用 cloud-init。因此,发送到 custom_data 的数据将被忽略。

如果您尝试使用的镜像未在 https://docs.microsoft.com/en-us/azure/virtual-machines/linux/using-cloud-init#cloud-init-overview 中列出,请按照以下步骤 https://docs.microsoft.com/en-us/azure/virtual-machines/linux/cloudinit-prepare-custom-image 操作。

data_disks

列表 / 元素=字典

描述数据磁盘列表。

caching

字符串

数据磁盘缓存类型。

选项

  • "ReadOnly" ← (默认)

  • "ReadWrite"

disk_size_gb

整数

空白数据磁盘的初始磁盘大小(以 GB 为单位)。

lun

字符串

数据磁盘的逻辑单元号。

默认值: "0"

managed_disk_type

字符串

托管数据磁盘类型。

选项

  • "Standard_LRS"

  • "Premium_LRS"

  • "StandardSSD_LRS"

  • "UltraSSD_LRS"

  • "Premium_ZRS"

  • "StandardSSD_ZRS"

disable_instance_discovery

布尔值

在 azure.azcollection 2.3.0 中添加

确定在尝试进行身份验证时是否执行实例发现。将其设置为 true 将完全禁用实例发现和授权验证。此功能旨在用于无法访问元数据端点的情况,例如在私有云或 Azure Stack 中。实例发现的过程包括从 https://login.microsoft.com/ 检索授权元数据以验证授权。通过将此设置为 **True**,将禁用对授权的验证。因此,至关重要的是要确保配置的授权主机是有效且可信的。

通过凭据文件配置文件或 AZURE_DISABLE_INSTANCE_DISCOVERY 环境变量设置。

选项

  • false ← (默认)

  • true

enable_accelerated_networking

布尔值

指示用户是否希望为正在创建的虚拟机规模集启用加速网络。

选项

  • false

  • true

ephemeral_os_disk

布尔值

可以为操作系统磁盘指定的临时磁盘设置的参数。

仅在使用托管磁盘的虚拟机实例中支持临时操作系统磁盘。

选项

  • false

  • true

eviction_policy

字符串

指定 Azure Spot 虚拟机的逐出策略。

要求将优先级设置为 Spot。

选项

  • "Deallocate"

  • "Delete"

identity

字典

对象的身份

type

字符串

托管标识的类型

选项

  • "SystemAssigned"

  • "UserAssigned"

  • "SystemAssigned, UserAssigned"

  • "None" ← (默认)

user_assigned_identities

字典

用户分配的托管标识及其选项

默认值: {}

append

布尔值

如果身份列表必须附加到当前身份(true)还是必须替换当前身份(false)

选项

  • false

  • true ← (默认)

id

列表 / 元素=字符串

与对象关联的用户分配的标识 ID 列表

默认值: []

image

任意

指定用于构建虚拟机的镜像。

如果是一个字符串,则镜像来自基于名称的自定义镜像。

如果是一个带有键 publisherofferskuversion 的字典,则镜像来自市场镜像。请注意,设置 version=latest 以获取给定镜像的最新版本。

如果是一个带有键 nameresource_group 的字典,则镜像来自基于设置的 nameresource_group 的自定义镜像。请注意,键 resource_group 是可选的,如果省略,将在订阅中搜索所有具有 name 的镜像。

Ansible 2.5 中添加了自定义镜像支持。

对于社区镜像,可以使用带有键 community_gallery_image_id 的字典。指定用于虚拟机部署的社区库镜像唯一 ID。

community_gallery_image_id 可以从共享库镜像 GET 调用中获取。

创建时是必需的。

load_balancer

字符串

负载均衡器名称。

location

字符串

有效的 Azure 位置。默认为资源组的位置。

log_mode

字符串

父参数。

log_path

字符串

父参数。

managed_disk_type

字符串

托管磁盘类型。

选项

  • "Standard_LRS"

  • "Premium_LRS"

  • "StandardSSD_LRS"

  • "UltraSSD_LRS"

  • "Premium_ZRS"

  • "StandardSSD_ZRS"

max_price

浮点数

指定您愿意为 Azure Spot VM/VMSS 支付的最高价格。

此价格以美元为单位。

-1 表示默认价格为按需价格。

要求将优先级设置为 Spot。

默认值: -1.0

name

字符串 / 必需

虚拟机的名称。

orchestration_mode

字符串

指定虚拟机规模集的编排模式。

orchestration_mode=Flexible 时,必须设置 public_ip_per_vm=True

orchestration_mode=Flexible 时,必须设置 platform_fault_domain_count

orchestration_mode=Flexible 时,必须设置 single_placement_group=False

orchestration_mode=Flexible 时,无法配置 overprovision

orchestration_mode=Flexible 时,无法配置 upgrade_policy,并且在 orchestration_mode=Uniform 时进行配置。

选项

  • "Flexible" ← (默认)

  • "Uniform"

os_disk_caching

别名: disk_caching

字符串

操作系统磁盘缓存的类型。

选项

  • "ReadOnly" ← (默认)

  • "ReadWrite"

os_disk_size_gb

整数

指定操作系统磁盘的大小(以 GB 为单位)。

这可以用来覆盖虚拟机镜像中磁盘的大小。

os_type

字符串

操作系统的基本类型。

选项

  • "Windows"

  • "Linux" ← (默认)

overprovision

布尔值

指定是否应过度预配虚拟机规模集。

选项

  • false

  • true

password

字符串

Active Directory 用户密码。当使用 Active Directory 用户而不是服务主体进行身份验证时使用。

plan

字典

虚拟机的第三方计费计划。

name

字符串 / 必需

计费计划名称。

product

字符串 / 必需

产品名称。

promotion_code

字符串

可选促销代码。

publisher

字符串 / 必需

提供该计划的发布者。

platform_fault_domain_count

整数

每个放置组的容错域计数。

默认值: 1

priority

字符串

VMSS 的优先级。

None 等效于常规 VM。

选项

  • "None"

  • "Spot"

profile

字符串

在 ~/.azure/credentials 文件中找到的安全配置文件。

public_ip_per_vm

布尔值

为规模集的每个虚拟机分配一个公共 IP

选项

  • false ← (默认)

  • true

remove_on_absent

列表 / 元素=字符串

当使用 state=absent 删除 VM 时,也删除关联的资源。

它可以是 all 或包含以下任何项的列表 [‘network_interfaces’, ‘virtual_storage’, ‘public_ips’]。

任何其他输入都将被忽略。

默认值: ["all"]

resource_group

字符串 / 必需

包含虚拟机规模集的资源组的名称。

scale_in_policy

字符串

定义缩减 vmss 实例的顺序

选项

  • "Default"

  • "NewestVM"

  • "OldestVM"

secret

字符串

Azure 客户端密钥。当使用服务主体进行身份验证时使用。

security_group

别名: security_group_name

任意

用于关联子网的现有安全组。

它可以是同一资源组中的安全组名称。

它可以是资源 ID。

它可以是一个包含安全组的 nameresource_group 的字典。

security_profile

字典

指定虚拟机规模集的安全相关配置文件设置。

encryption_at_host

布尔值

用户可以在请求中使用此属性来启用或禁用虚拟机规模集的主机加密。

这将为所有磁盘(包括主机本身的资源/临时磁盘)启用加密。

选项

  • false

  • true

security_type

字符串

指定虚拟机规模集的 SecurityType。

设置为 TrustedLaunch 以启用 UefiSettings。

选项

  • "TrustedLaunch"

uefi_settings

字典

指定创建虚拟机规模集时使用的安全设置,例如安全启动和 vTPM。

secure_boot_enabled

布尔值

指定是否应在虚拟机规模集上启用安全启动。

选项

  • false

  • true

v_tpm_enabled

布尔值

指定是否应在虚拟机规模集上启用 vTPM。

选项

  • false

  • true

short_hostname

字符串

短主机名。

single_placement_group

布尔值

如果为 true,则将规模集限制为单个放置组,最大大小为 100 台虚拟机。

选项

  • false ← (默认)

  • true

ssh_password_enabled

布尔值

当 os_type 为 Linux 时,设置 ssh_password_enabled=false 将禁用 SSH 密码验证,并要求使用 SSH 密钥。

选项

  • false

  • true ← (默认)

ssh_public_keys

列表 / 元素=字典

对于 os_type=Linux,请提供 SSH 密钥列表。

列表中的每个项目都应该是一个字典,该字典包含两个键:pathkey_data

path 设置为 authorized_keys 文件的默认位置。

例如,在企业 Linux 主机上,path=/home/<管理员用户名>/.ssh/authorized_keys。将 key_data 设置为公钥的实际值。

state

字符串

声明虚拟机规模集的状态。

状态 present 将检查机器是否存在并具有请求的配置。如果现有机器的配置不匹配,则将更新该机器。

状态 absent 将删除虚拟机规模集。

选项

  • "absent"

  • "present" ← (默认)

subnet_name

别名: subnet

字符串

子网名称。

subscription_id

字符串

您的 Azure 订阅 ID。

tags

字典

要作为元数据分配给对象的字符串:字符串对字典。

对象上的元数据标签将使用任何提供的值进行更新。

要删除标签,请将 append_tags 选项设置为 false。

当前,Azure DNS 区域和流量管理器服务也不允许在标签中使用空格。

Azure Front Door 不支持使用

Azure Automation 和 Azure CDN 仅支持资源上的 15 个标签。

tenant

字符串

Azure 租户 ID。当使用服务主体进行身份验证时使用。

terminate_event_timeout_minutes

整数

终止通知事件的超时时间

范围在 5 到 15 之间

thumbprint

字符串

在 azure.azcollection 1.14.0 中添加

x509_certificate_path 中指定的私钥的指纹。

当使用服务主体进行身份验证时使用。

如果定义了 x509_certificate_path,则为必需项。

tier

字符串

SKU 层。

选项

  • "Basic"

  • "Standard"

upgrade_policy

字符串

升级策略。

在创建 Azure 虚拟机规模集时为必需项。

选项

  • "Manual"

  • "Automatic"

virtual_network_name

别名: virtual_network

字符串

虚拟网络名称。

virtual_network_resource_group

字符串

在创建虚拟机时,如果要使用来自另一个资源组的特定虚拟网络。

使用此参数指定要使用的资源组。

vm_size

字符串

有效的 Azure VM 大小值。例如,Standard_D4

选择列表因订阅和位置而异。检查您的订阅以获取可用的选择。

x509_certificate_path

path

在 azure.azcollection 1.14.0 中添加

用于创建 PEM 格式的服务主体的 X509 证书的路径。

证书必须附加到私钥。

当使用服务主体进行身份验证时使用。

zones

列表 / 元素=字符串

虚拟机规模集的可用性区域列表。

注释

注意

  • 要使用 Azure 进行身份验证,您可以传递参数、设置环境变量、使用存储在 ~/.azure/credentials 中的配置文件,或者在使用 az login 运行任务或剧本之前登录。

  • 也可以使用服务主体或 Active Directory 用户进行身份验证。

  • 要通过服务主体进行身份验证,请传递 subscription_id、client_id、secret 和 tenant 或设置环境变量 AZURE_SUBSCRIPTION_ID、AZURE_CLIENT_ID、AZURE_SECRET 和 AZURE_TENANT。

  • 要通过 Active Directory 用户进行身份验证,请传递 ad_user 和 password,或者在环境中设置 AZURE_AD_USER 和 AZURE_PASSWORD。

  • 或者,凭据可以存储在 ~/.azure/credentials 中。这是一个 ini 文件,包含一个 [default] 部分和以下键:subscription_id、client_id、secret 和 tenant 或 subscription_id、ad_user 和 password。也可以添加其他配置文件。通过传递 profile 或在环境中设置 AZURE_PROFILE 来指定配置文件。

另请参阅

另请参阅

使用 Azure CLI 登录

如何使用 az login 命令进行身份验证。

示例

- name: Create VMSS
  azure_rm_virtualmachinescaleset:
    resource_group: myResourceGroup
    name: testvmss
    vm_size: Standard_DS1_v2
    capacity: 2
    virtual_network_name: testvnet
    upgrade_policy: Manual
    subnet_name: testsubnet
    terminate_event_timeout_minutes: 10
    scale_in_policy: NewestVM
    admin_username: "{{ username }}"
    ssh_password_enabled: false
    ssh_public_keys:
      - path: /home/adminUser/.ssh/authorized_keys
        key_data: < insert your ssh public key here... >
    managed_disk_type: Standard_LRS
    image:
      offer: 0001-com-ubuntu-server-focal
      publisher: canonical
      sku: 20_04-lts-gen2
      version: latest
    data_disks:
      - lun: 0
        disk_size_gb: 64
        caching: ReadWrite
        managed_disk_type: Standard_LRS

- name: Create VMSS with an image that requires plan information
  azure_rm_virtualmachinescaleset:
    resource_group: myResourceGroup
    name: testvmss
    vm_size: Standard_DS1_v2
    capacity: 3
    virtual_network_name: testvnet
    upgrade_policy: Manual
    subnet_name: testsubnet
    admin_username: "{{ username }}"
    ssh_password_enabled: false
    ssh_public_keys:
      - path: /home/adminUser/.ssh/authorized_keys
        key_data: < insert your ssh public key here... >
    managed_disk_type: Standard_LRS
    image:
      offer: cis-ubuntu-linux-1804-l1
      publisher: center-for-internet-security-inc
      sku: Stable
      version: latest
    plan:
      name: cis-ubuntu-linux-1804-l1
      product: cis-ubuntu-linux-1804-l1
      publisher: center-for-internet-security-inc
    data_disks:
      - lun: 0
        disk_size_gb: 64
        caching: ReadWrite
        managed_disk_type: Standard_LRS

- name: Create a VMSS with a custom image
  azure_rm_virtualmachinescaleset:
    resource_group: myResourceGroup
    name: testvmss
    vm_size: Standard_DS1_v2
    capacity: 2
    virtual_network_name: testvnet
    upgrade_policy: Manual
    subnet_name: testsubnet
    admin_username: "{{ username }}"
    admin_password: "{{ password }}"
    managed_disk_type: Standard_LRS
    image: customimage001

- name: Create a VMSS with a community gallery image ID
  azure_rm_virtualmachinescaleset:
    resource_group: myResourceGroup
    name: testvmss
    vm_size: Standard_D8_v4
    admin_username: testuser
    single_placement_group: false
    platform_fault_domain_count: 1
    public_ip_per_vm: true
    ssh_password_enabled: false
    ssh_public_keys:
      - path: /home/testuser/.ssh/authorized_keys
        key_data: "ssh-rsa ********* [email protected]"
    virtual_network_name: VMSStestVnet
    subnet_name: VMSStestSubnet
    managed_disk_type: Standard_LRS
    orchestration_mode: Flexible
    os_disk_caching: ReadWrite
    image:
      community_gallery_image_id: "/CommunityGalleries/yellowbrick-fc7e81f1-87dd-4989-9ca8-03743762e873/Images/Ubuntu-5.15.0-1035-azure_22.04"
    data_disks:
      - lun: 0
        disk_size_gb: 64
        caching: ReadWrite
        managed_disk_type: Standard_LRS

- name: Create a VMSS with over 100 instances
  azure_rm_virtualmachinescaleset:
    resource_group: myResourceGroup
    name: testvmss
    vm_size: Standard_DS1_v2
    capacity: 120
    single_placement_group: false
    virtual_network_name: testvnet
    upgrade_policy: Manual
    subnet_name: testsubnet
    admin_username: "{{ username }}"
    admin_password: "{{ password }}"
    managed_disk_type: Standard_LRS
    image: customimage001

- name: Create a VMSS with a custom image from a particular resource group
  azure_rm_virtualmachinescaleset:
    resource_group: myResourceGroup
    name: testvmss
    vm_size: Standard_DS1_v2
    capacity: 2
    virtual_network_name: testvnet
    upgrade_policy: Manual
    subnet_name: testsubnet
    admin_username: "{{ username }}"
    admin_password: "{{ password }}"
    managed_disk_type: Standard_LRS
    image:
      name: customimage001
      resource_group: myResourceGroup

- name: Create a VMSS with Spot Instance
  azure_rm_virtualmachinescaleset:
    resource_group: myResourceGroup
    name: testvmss
    vm_size: Standard_DS1_v2
    capacity: 5
    priority: Spot
    eviction_policy: Deallocate
    virtual_network_name: testvnet
    upgrade_policy: Manual
    subnet_name: testsubnet
    admin_username: "{{ username }}"
    admin_password: "{{ password }}"
    managed_disk_type: Standard_LRS
    image: customimage001

- name: Create VMSS with security group
  azure_rm_virtualmachinescaleset:
    resource_group: "{{ resource_group }}"
    name: testVMSS{{ rpfx }}
    vm_size: Standard_D4s_v3
    admin_username: testuser
    single_placement_group: false
    platform_fault_domain_count: 1
    public_ip_per_vm: true
    ssh_password_enabled: false
    ssh_public_keys:
      - path: /home/testuser/.ssh/authorized_keys
        key_data: "ssh-rsa ****"
    virtual_network_name: VMSStestVnet
    subnet_name: VMSStestSubnet
    managed_disk_type: Standard_LRS
    orchestration_mode: Flexible
    os_disk_caching: ReadWrite
    security_profile:
      uefi_settings:
        secure_boot_enabled: true
        v_tpm_enabled: false
      encryption_at_host: false
      security_type: TrustedLaunch
    image:
      offer: 0001-com-ubuntu-server-jammy
      publisher: Canonical
      sku: 22_04-lts-gen2
      version: latest
    data_disks:
      - lun: 0
        disk_size_gb: 64
        caching: ReadWrite
        managed_disk_type: Standard_LRS

返回值

常见返回值记录在此处 here,以下是此模块独有的字段

描述

azure_vmss

字典

有关对象当前状态的事实。

请注意,事实不是注册输出的一部分,而是可以直接使用的。

已返回: 总是

示例: {"properties": {"orchestrationMode": "Flexible", "overprovision": true, "platformFaultDomainCount": 1, "scaleInPolicy": {"rules": ["NewestVM"]}, "singlePlacementGroup": true, "upgradePolicy": {"mode": "Manual"}, "virtualMachineProfile": {"networkProfile": {"networkInterfaceConfigurations": [{"name": "testvmss", "properties": {"dnsSettings": {"dnsServers": []}, "enableAcceleratedNetworking": false, "ipConfigurations": [{"name": "default", "properties": {"privateIPAddressVersion": "IPv4", "subnet": {"id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/virtualNetworks/testvnet/subnets/testsubnet"}}}], "primary": true}}]}, "osProfile": {"adminUsername": "testuser", "computerNamePrefix": "testvmss", "linuxConfiguration": {"disablePasswordAuthentication": true, "ssh": {"publicKeys": [{"keyData": "", "path": "/home/testuser/.ssh/authorized_keys"}]}}, "secrets": []}, "scheduledEventsProfile": {"terminateNotificationProfile": {"enable": true, "notBeforeTimeout": "PT10M"}}, "storageProfile": {"dataDisks": [{"caching": "ReadWrite", "createOption": "empty", "diskSizeGB": 64, "lun": 0, "managedDisk": {"storageAccountType": "Standard_LRS"}}], "imageReference": {"offer": "0001-com-ubuntu-server-focal", "publisher": "canonical", "sku": "20_04-lts-gen2", "version": "20.04.202111210"}, "osDisk": {"caching": "ReadWrite", "createOption": "fromImage", "managedDisk": {"storageAccountType": "Standard_LRS"}}, "securityProfile": {"encryptionAtHost": false, "securityType": "TrustedLaunch", "uefiSettings": {"secureBootEnabled": true, "vTpmEnabled": false}}}}}, "sku": {"capacity": 2, "name": "Standard_DS1_v2", "tier": "Standard"}, "tags": null, "type": "Microsoft.Compute/virtualMachineScaleSets"}

作者

  • Sertac Ozercan (@sozercan)