azure.azcollection.azure_rm_securitygroup 模块 – 管理 Azure 网络安全组

注意

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

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

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

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

azure.azcollection 0.1.0 中的新增功能

概要

  • 创建、更新或删除网络安全组。

  • 安全组包含访问控制列表 (ACL) 规则,这些规则允许或拒绝进出子网或各个网络接口的网络流量。

  • 创建安全组时会包含一组默认安全规则和一组空的自定义安全规则。

  • 通过向空的自定义安全规则集中添加规则来塑造流量流。

要求

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

  • 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 授权时使用。

api_profile

字符串

在 azure.azcollection 0.0.1 中添加

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

默认值: "latest"

append_tags

布尔值

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

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

选项

  • false

  • true ←(默认)

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"

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"

default_rules

list / elements=dictionary

在创建安全组时自动添加到安全组的默认规则集。

通常,默认规则不会被修改。修改规则以塑造进出子网或 NIC 的流量。

有关规则字典的组成,请参见下面的规则。

access

字符串

是否允许流量。

选项

  • "Allow" ← (默认)

  • "Deny"

description

字符串

规则用途的简短描述。

destination_address_prefix

any

目标地址前缀。

CIDR 或目标 IP 范围。

星号 * 也可用于匹配所有源 IP。

还可以使用诸如 VirtualNetworkAzureLoadBalancerInternet 等默认标签。

它可以接受字符串类型或字符串类型列表。

星号 * 和默认标签只能指定为单个字符串类型,而不是字符串列表。

默认值: "*"

destination_application_security_groups

list / elements=any

目标应用程序安全组的列表。

它可以是资源 ID 的列表。

它可以是同一资源组中名称的列表。

它可以是包含 resource_groupname 的字典列表。

它与 destination_address_prefixdestination_address_prefixes 互斥。

destination_port_range

any

流量所指向的端口或端口范围。

它可以接受字符串类型或字符串类型列表。

默认值: "*"

direction

字符串

指示流量的方向。

选项

  • "Inbound" ← (默认)

  • "Outbound"

name

string / required

规则的唯一名称。

priority

integer / required

应用规则的顺序。必须是 100 到 4096 之间(包括 100 和 4096)的唯一整数。

protocol

字符串

接受的流量协议。

选项

  • "Udp"

  • "UDP"

  • "Tcp"

  • "TCP"

  • "Icmp"

  • "ICMP"

  • "*" ← (默认)

source_address_prefix

any

CIDR 或源 IP 范围。

星号 * 也可用于匹配所有源 IP。

还可以使用诸如 VirtualNetworkAzureLoadBalancerInternet 等默认标签。

如果这是入口规则,则指定网络流量的来源。

它可以接受字符串类型或字符串类型列表。

星号 * 和默认标签只能指定为单个字符串类型,而不是字符串列表。

默认值: "*"

source_application_security_groups

list / elements=any

源应用程序安全组的列表。

它可以是资源 ID 的列表。

它可以是同一资源组中名称的列表。

它可以是包含 resource_group 和 name 的字典列表。

它与 source_address_prefixsource_address_prefixes 互斥。

source_port_range

any

流量的起始端口或端口范围。

它可以接受字符串类型或字符串类型列表。

默认值: "*"

disable_instance_discovery

布尔值

在 azure.azcollection 2.3.0 中添加

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

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

选项

  • false ← (默认)

  • true

location

字符串

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

log_mode

字符串

父参数。

log_path

字符串

父参数。

name

string / required

要操作的安全组的名称。

password

字符串

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

profile

字符串

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

purge_default_rules

布尔值

删除任何与 default_rules 参数中定义的规则不匹配的现有规则。

选项

  • false ← (默认)

  • true

purge_rules

布尔值

删除任何与 rules 参数中定义的规则不匹配的现有规则。

选项

  • false ← (默认)

  • true

resource_group

string / required

安全组所属的资源组的名称。

rules

list / elements=dictionary

塑造进出子网或 NIC 流量的规则集。每个规则都是一个字典。

access

字符串

是否允许流量。

选项

  • "Allow" ← (默认)

  • "Deny"

description

字符串

规则用途的简短描述。

destination_address_prefix

any

目标地址前缀。

CIDR 或目标 IP 范围。

星号 * 也可用于匹配所有源 IP。

还可以使用诸如 VirtualNetworkAzureLoadBalancerInternet 等默认标签。

它可以接受字符串类型或字符串类型列表。

星号 * 和默认标签只能指定为单个字符串类型,而不是字符串列表。

默认值: "*"

destination_application_security_groups

list / elements=any

目标应用程序安全组的列表。

它可以是资源 ID 的列表。

它可以是同一资源组中名称的列表。

它可以是包含 resource_groupname 的字典列表。

它与 destination_address_prefixdestination_address_prefixes 互斥。

destination_port_range

any

流量所指向的端口或端口范围。

它可以接受字符串类型或字符串类型列表。

默认值: "*"

direction

字符串

指示流量的方向。

选项

  • "Inbound" ← (默认)

  • "Outbound"

name

string / required

规则的唯一名称。

priority

integer / required

应用规则的顺序。必须是 100 到 4096 之间(包括 100 和 4096)的唯一整数。

protocol

字符串

接受的流量协议。

选项

  • "Udp"

  • "UDP"

  • "Tcp"

  • "TCP"

  • "Icmp"

  • "ICMP"

  • "*" ← (默认)

source_address_prefix

any

CIDR 或源 IP 范围。

星号 * 也可用于匹配所有源 IP。

还可以使用诸如 VirtualNetworkAzureLoadBalancerInternet 等默认标签。

如果这是入口规则,则指定网络流量的来源。

它可以接受字符串类型或字符串类型列表。

星号 * 和默认标签只能指定为单个字符串类型,而不是字符串列表。

默认值: "*"

source_application_security_groups

list / elements=any

源应用程序安全组的列表。

它可以是资源 ID 的列表。

它可以是同一资源组中名称的列表。

它可以是包含 resource_group 和 name 的字典列表。

它与 source_address_prefixsource_address_prefixes 互斥。

source_port_range

any

流量的起始端口或端口范围。

它可以接受字符串类型或字符串类型列表。

默认值: "*"

secret

字符串

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

state

字符串

断言安全组的状态。设置为 present 以创建或更新安全组。设置为 absent 以删除安全组。

选项

  • "absent"

  • "present" ← (默认)

subscription_id

字符串

您的 Azure 订阅 ID。

tags

dictionary

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

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

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

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

Azure Front Door 不支持使用

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

tenant

字符串

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

thumbprint

字符串

在 azure.azcollection 1.14.0 中添加

x509_certificate_path 中指定的私钥的指纹。

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

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

x509_certificate_path

path

在 azure.azcollection 1.14.0 中添加

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

证书必须附加到私钥。

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

备注

注意

  • 为了使用 Azure 进行身份验证,您可以传递参数、设置环境变量、使用存储在 ~/.azure/credentials 中的配置文件,或者在运行任务或 playbook 之前使用 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 中。这是一个包含 [default] 部分和以下键的 ini 文件:subscription_id、client_id、secret 和 tenant 或 subscription_id、ad_user 和 password。也可以添加其他配置文件。通过传递配置文件或在环境中设置 AZURE_PROFILE 来指定配置文件。

另请参阅

另请参阅

使用 Azure CLI 登录

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

示例

- name: Create a security group
  azure_rm_securitygroup:
    resource_group: myResourceGroup
    name: mysecgroup
    purge_rules: true
    rules:
      - name: DenySSH
        protocol: Tcp
        destination_port_range: 22
        access: Deny
        priority: 100
        direction: Inbound
      - name: 'AllowSSH'
        protocol: Tcp
        source_address_prefix:
          - '174.109.158.0/24'
          - '174.109.159.0/24'
        destination_port_range: 22
        access: Allow
        priority: 101
        direction: Inbound
      - name: 'AllowMultiplePorts'
        protocol: Tcp
        source_address_prefix:
          - '174.109.158.0/24'
          - '174.109.159.0/24'
        destination_port_range:
          - 80
          - 443
        access: Allow
        priority: 102

- name: Update rules on existing security group
  azure_rm_securitygroup:
    resource_group: myResourceGroup
    name: mysecgroup
    rules:
      - name: DenySSH
        protocol: Tcp
        destination_port_range: 22-23
        access: Deny
        priority: 100
        direction: Inbound
      - name: AllowSSHFromHome
        protocol: Tcp
        source_address_prefix: '174.109.158.0/24'
        destination_port_range: 22-23
        access: Allow
        priority: 102
        direction: Inbound
    tags:
      testing: testing
      delete: on-exit

- name: Create a securiy group with I(protocol=Icmp)
  azure_rm_securitygroup:
    name: mysecgroup
    resource_group: myResourceGroup
    rules:
      - name: SSH
        protocol: Tcp
        destination_port_range: 22
        access: Allow
        priority: 105
        direction: Inbound
      - name: ICMP
        protocol: Icmp
        priority: 106

- name: Delete security group
  azure_rm_securitygroup:
    resource_group: myResourceGroup
    name: mysecgroup
    state: absent

返回值

常用返回值记录在 这里,以下是此模块特有的字段

Key

描述

state

complex

安全组的当前状态。

返回: 始终

default_rules

list / elements=string

网络安全组的默认安全规则。

返回: 始终

示例: [{"access": "Allow", "description": "允许来自 VNET 中所有 VM 的入站流量", "destination_address_prefix": "VirtualNetwork", "destination_port_range": "*", "direction": "Inbound", "etag": "W/\"edf48d56-b315-40ca-a85d-dbcb47f2da7d\"", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowVnetInBound", "name": "AllowVnetInBound", "priority": 65000, "protocol": "*", "provisioning_state": "Succeeded", "source_address_prefix": "VirtualNetwork", "source_port_range": "*"}, {"access": "Allow", "description": "允许来自 azure 负载均衡器的入站流量", "destination_address_prefix": "*", "destination_port_range": "*", "direction": "Inbound", "etag": "W/\"edf48d56-b315-40ca-a85d-dbcb47f2da7d\"", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowAzureLoadBalancerInBound", "name": "AllowAzureLoadBalancerInBound", "priority": 65001, "protocol": "*", "provisioning_state": "Succeeded", "source_address_prefix": "AzureLoadBalancer", "source_port_range": "*"}, {"access": "Deny", "description": "拒绝所有入站流量", "destination_address_prefix": "*", "destination_port_range": "*", "direction": "Inbound", "etag": "W/\"edf48d56-b315-40ca-a85d-dbcb47f2da7d\"", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/DenyAllInBound", "name": "DenyAllInBound", "priority": 65500, "protocol": "*", "provisioning_state": "Succeeded", "source_address_prefix": "*", "source_port_range": "*"}, {"access": "Allow", "description": "允许所有 VM VNET 中所有 VM 的出站流量", "destination_address_prefix": "VirtualNetwork", "destination_port_range": "*", "direction": "Outbound", "etag": "W/\"edf48d56-b315-40ca-a85d-dbcb47f2da7d\"", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowVnetOutBound", "name": "AllowVnetOutBound", "priority": 65000, "protocol": "*", "provisioning_state": "Succeeded", "source_address_prefix": "VirtualNetwork", "source_port_range": "*"}, {"access": "Allow", "description": "允许所有 VM 到互联网的出站流量", "destination_address_prefix": "Internet", "destination_port_range": "*", "direction": "Outbound", "etag": "W/\"edf48d56-b315-40ca-a85d-dbcb47f2da7d\"", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowInternetOutBound", "name": "AllowInternetOutBound", "priority": 65001, "protocol": "*", "provisioning_state": "Succeeded", "source_address_prefix": "*", "source_port_range": "*"}, {"access": "Deny", "description": "拒绝所有出站流量", "destination_address_prefix": "*", "destination_port_range": "*", "direction": "Outbound", "etag": "W/\"edf48d56-b315-40ca-a85d-dbcb47f2da7d\"", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/DenyAllOutBound", "name": "DenyAllOutBound", "priority": 65500, "protocol": "*", "provisioning_state": "Succeeded", "source_address_prefix": "*", "source_port_range": "*"}]

id

字符串

资源 ID。

返回: 始终

示例: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup"

location

字符串

资源位置。

返回: 始终

示例: "westus"

name

字符串

安全组的名称。

返回: 始终

示例: "mysecgroup"

network_interfaces

list / elements=string

对网络接口的引用集合。

返回: 始终

示例: []

rules

list / elements=string

网络安全组的安全规则集合。

返回: 始终

示例: [{"access": "Deny", "description": null, "destination_address_prefix": "*", "destination_port_range": "22", "direction": "Inbound", "etag": "W/\"edf48d56-b315-40ca-a85d-dbcb47f2da7d\"", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/securityRules/DenySSH", "name": "DenySSH", "priority": 100, "protocol": "Tcp", "provisioning_state": "Succeeded", "source_address_prefix": "*", "source_port_range": "*"}, {"access": "Allow", "description": null, "destination_address_prefix": "*", "destination_port_range": "22", "direction": "Inbound", "etag": "W/\"edf48d56-b315-40ca-a85d-dbcb47f2da7d\"", "id": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/securityRules/AllowSSH", "name": "AllowSSH", "priority": 101, "protocol": "Tcp", "provisioning_state": "Succeeded", "source_address_prefix": "174.109.158.0/24", "source_port_range": "*"}]

subnets

list / elements=string

对子网的引用集合。

返回: 始终

示例: []

tags

dictionary

要分配给安全组的标签。

返回: 始终

示例: {"delete": "on-exit", "foo": "bar", "testing": "testing"}

type

字符串

资源类型。

返回: 始终

示例: "Microsoft.Network/networkSecurityGroups"

作者

  • Chris Houseknecht (@chouseknecht)

  • Matt Davis (@nitzmahone)