azure_rm_securitygroup - Manage Azure network security groups.

New in version 2.1.

Synopsis

Requirements

The below requirements are needed on the host that executes this module.

  • python >= 2.7
  • azure >= 2.0.0

Parameters

Parameter Choices/Defaults Comments
ad_user
Active Directory username. Use when authenticating with an Active Directory user rather than service principal.
adfs_authority_url
(added in 2.6)
Default:
None
Azure AD authority url. Use when authenticating with Username/password, and has your own ADFS authority.
api_profile
(added in 2.5)
Default:
latest
Selects an API profile to use when communicating with Azure services. Default value of latest is appropriate for public clouds; future values will allow use with Azure Stack.
append_tags
bool
    Choices:
  • no
  • yes ←
Use to control if tags field is canonical or just appends to existing tags. When canonical, any tags not found in the tags parameter will be removed from the object's metadata.
auth_source
(added in 2.5)
    Choices:
  • auto
  • cli
  • credential_file
  • env
  • msi
Controls the source of the credentials to use for authentication.
If not specified, ANSIBLE_AZURE_AUTH_SOURCE environment variable will be used and default to auto if variable is not defined.
auto will follow the default precedence of module parameters -> environment variables -> default profile in credential file ~/.azure/credentials.
When set to cli, the credentials will be sources from the default Azure CLI profile.
Can also be set via the ANSIBLE_AZURE_AUTH_SOURCE environment variable.
When set to msi, the host machine must be an azure resource with an enabled MSI extension. subscription_id or the environment variable AZURE_SUBSCRIPTION_ID can be used to identify the subscription ID if the resource is granted access to more than one subscription, otherwise the first subscription is chosen.
The msi was added in Ansible 2.6.
cert_validation_mode
(added in 2.5)
    Choices:
  • validate
  • ignore
Controls the certificate validation behavior for Azure endpoints. By default, all modules will validate the server certificate, but when an HTTPS proxy is in use, or against Azure Stack, it may be necessary to disable this behavior by passing ignore. Can also be set via credential file profile or the AZURE_CERT_VALIDATION environment variable.
client_id
Azure client ID. Use when authenticating with a Service Principal.
cloud_environment
(added in 2.4)
Default:
AzureCloud
For cloud environments other than the US public cloud, the environment name (as defined by Azure Python SDK, eg, AzureChinaCloud, AzureUSGovernment), or a metadata discovery endpoint URL (required for Azure Stack). Can also be set via credential file profile or the AZURE_CLOUD_ENVIRONMENT environment variable.
default_rules
The set of default rules automatically added to a security group at creation. In general default rules will not be modified. Modify rules to shape the flow of traffic to or from a subnet or NIC. See rules below for the makeup of a rule dict.
location
Valid azure location. Defaults to location of the resource group.
name
Name of the security group to operate on.
password
Active Directory user password. Use when authenticating with an Active Directory user rather than service principal.
profile
Security profile found in ~/.azure/credentials file.
purge_default_rules
bool
    Choices:
  • no ←
  • yes
Remove any existing rules not matching those defined in the default_rules parameter.
purge_rules
bool
    Choices:
  • no ←
  • yes
Remove any existing rules not matching those defined in the rules parameters.
resource_group
required
Name of the resource group the security group belongs to.
rules
Set of rules shaping traffic flow to or from a subnet or NIC. Each rule is a dictionary.
source_address_prefix Default:
*
The CIDR or source IP range.
Asterix * can also be used to match all source IPs.
Default tags such as VirtualNetwork, AzureLoadBalancer and Internet can also be used.
If this is an ingress rule, specifies where network traffic originates from.
It can accept string type or a list of string type.
destination_address_prefix Default:
*
The destination address prefix.
CIDR or destination IP range.
Asterix * can also be used to match all source IPs.
Default tags such as VirtualNetwork, AzureLoadBalancer and Internet can also be used.
It can accept string type or a list of string type.
protocol
    Choices:
  • Udp
  • Tcp
  • * ←
Accepted traffic protocol.
name
required
Unique name for the rule.
description
Short description of the rule's purpose.
direction
    Choices:
  • Inbound ←
  • Outbound
Indicates the direction of the traffic flow.
access
    Choices:
  • Allow ←
  • Deny
Whether or not to allow the traffic flow.
source_port_range Default:
*
Port or range of ports from which traffic originates.
It can accept string type or a list of string type.
destination_port_range Default:
*
Port or range of ports to which traffic is headed.
It can accept string type or a list of string type.
priority
required
Order in which to apply the rule. Must a unique integer between 100 and 4096 inclusive.
secret
Azure client secret. Use when authenticating with a Service Principal.
state
    Choices:
  • absent
  • present ←
Assert the state of the security group. Set to 'present' to create or update a security group. Set to 'absent' to remove a security group.
subscription_id
Your Azure subscription Id.
tags
Dictionary of string:string pairs to assign as metadata to the object. Metadata tags on the object will be updated with any provided values. To remove tags set append_tags option to false.
tenant
Azure tenant ID. Use when authenticating with a Service Principal.

Notes

Note

  • For authentication with Azure you can pass parameters, set environment variables or use a profile stored in ~/.azure/credentials. Authentication is possible using a service principal or Active Directory user. To authenticate via service principal, pass subscription_id, client_id, secret and tenant or set environment variables AZURE_SUBSCRIPTION_ID, AZURE_CLIENT_ID, AZURE_SECRET and AZURE_TENANT.
  • To authenticate via Active Directory user, pass ad_user and password, or set AZURE_AD_USER and AZURE_PASSWORD in the environment.
  • Alternatively, credentials can be stored in ~/.azure/credentials. This is an ini file containing a [default] section and the following keys: subscription_id, client_id, secret and tenant or subscription_id, ad_user and password. It is also possible to add additional profiles. Specify the profile by passing profile or setting AZURE_PROFILE in the environment.

Examples

# Create a security group
- azure_rm_securitygroup:
      resource_group: mygroup
      name: mysecgroup
      purge_rules: yes
      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

# Update rules on existing security group
- azure_rm_securitygroup:
      resource_group: mygroup
      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

# Delete security group
- azure_rm_securitygroup:
      resource_group: mygroup
      name: mysecgroup
      state: absent

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key Returned Description
state
dict
always
Current state of the security group.

Sample:
{'subnets': [], 'name': 'mysecgroup', 'tags': {'foo': 'bar', 'testing': 'testing', 'delete': 'on-exit'}, 'rules': [{'access': 'Deny', 'destination_address_prefix': '*', 'protocol': 'Tcp', 'description': None, 'direction': 'Inbound', 'provisioning_state': 'Succeeded', 'priority': 100, 'etag': 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', 'source_port_range': '*', 'source_address_prefix': '*', 'id': '/subscriptions/3f7e29ba-24e0-42f6-8d9c-5149a14bda37/resourceGroups/Testing/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/securityRules/DenySSH', 'destination_port_range': '22', 'name': 'DenySSH'}, {'access': 'Allow', 'destination_address_prefix': '*', 'protocol': 'Tcp', 'description': None, 'direction': 'Inbound', 'provisioning_state': 'Succeeded', 'priority': 101, 'etag': 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', 'source_port_range': '*', 'source_address_prefix': '174.109.158.0/24', 'id': '/subscriptions/3f7e29ba-24e0-42f6-8d9c-5149a14bda37/resourceGroups/Testing/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/securityRules/AllowSSH', 'destination_port_range': '22', 'name': 'AllowSSH'}], 'network_interfaces': [], 'default_rules': [{'access': 'Allow', 'destination_address_prefix': 'VirtualNetwork', 'protocol': '*', 'description': 'Allow inbound traffic from all VMs in VNET', 'direction': 'Inbound', 'provisioning_state': 'Succeeded', 'priority': 65000, 'etag': 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', 'source_port_range': '*', 'source_address_prefix': 'VirtualNetwork', 'id': '/subscriptions/3f7e29ba-24e0-42f6-8d9c-5149a14bda37/resourceGroups/Testing/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowVnetInBound', 'destination_port_range': '*', 'name': 'AllowVnetInBound'}, {'access': 'Allow', 'destination_address_prefix': '*', 'protocol': '*', 'description': 'Allow inbound traffic from azure load balancer', 'direction': 'Inbound', 'provisioning_state': 'Succeeded', 'priority': 65001, 'etag': 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', 'source_port_range': '*', 'source_address_prefix': 'AzureLoadBalancer', 'id': '/subscriptions/3f7e29ba-24e0-42f6-8d9c-5149a14bda37/resourceGroups/Testing/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowAzureLoadBalancerInBound', 'destination_port_range': '*', 'name': 'AllowAzureLoadBalancerInBound'}, {'access': 'Deny', 'destination_address_prefix': '*', 'protocol': '*', 'description': 'Deny all inbound traffic', 'direction': 'Inbound', 'provisioning_state': 'Succeeded', 'priority': 65500, 'etag': 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', 'source_port_range': '*', 'source_address_prefix': '*', 'id': '/subscriptions/3f7e29ba-24e0-42f6-8d9c-5149a14bda37/resourceGroups/Testing/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/DenyAllInBound', 'destination_port_range': '*', 'name': 'DenyAllInBound'}, {'access': 'Allow', 'destination_address_prefix': 'VirtualNetwork', 'protocol': '*', 'description': 'Allow outbound traffic from all VMs to all VMs in VNET', 'direction': 'Outbound', 'provisioning_state': 'Succeeded', 'priority': 65000, 'etag': 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', 'source_port_range': '*', 'source_address_prefix': 'VirtualNetwork', 'id': '/subscriptions/3f7e29ba-24e0-42f6-8d9c-5149a14bda37/resourceGroups/Testing/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowVnetOutBound', 'destination_port_range': '*', 'name': 'AllowVnetOutBound'}, {'access': 'Allow', 'destination_address_prefix': 'Internet', 'protocol': '*', 'description': 'Allow outbound traffic from all VMs to Internet', 'direction': 'Outbound', 'provisioning_state': 'Succeeded', 'priority': 65001, 'etag': 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', 'source_port_range': '*', 'source_address_prefix': '*', 'id': '/subscriptions/3f7e29ba-24e0-42f6-8d9c-5149a14bda37/resourceGroups/Testing/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/AllowInternetOutBound', 'destination_port_range': '*', 'name': 'AllowInternetOutBound'}, {'access': 'Deny', 'destination_address_prefix': '*', 'protocol': '*', 'description': 'Deny all outbound traffic', 'direction': 'Outbound', 'provisioning_state': 'Succeeded', 'priority': 65500, 'etag': 'W/"edf48d56-b315-40ca-a85d-dbcb47f2da7d"', 'source_port_range': '*', 'source_address_prefix': '*', 'id': '/subscriptions/3f7e29ba-24e0-42f6-8d9c-5149a14bda37/resourceGroups/Testing/providers/Microsoft.Network/networkSecurityGroups/mysecgroup/defaultSecurityRules/DenyAllOutBound', 'destination_port_range': '*', 'name': 'DenyAllOutBound'}], 'location': 'westus', 'type': 'Microsoft.Network/networkSecurityGroups', 'id': '/subscriptions/3f7e29ba-24e0-42f6-8d9c-5149a14bda37/resourceGroups/Testing/providers/Microsoft.Network/networkSecurityGroups/mysecgroup'}


Status

This module is flagged as preview which means that it is not guaranteed to have a backwards compatible interface.

Maintenance

This module is flagged as certified which means that it is maintained by an Ansible Partner. See Module Maintenance & Support for more info.

For a list of other modules that are also maintained by an Ansible Partner, see here.

Author

  • Chris Houseknecht (@chouseknecht)
  • Matt Davis (@nitzmahone)

Hint

If you notice any issues in this documentation you can edit this document to improve it.