azure_rm_securitygroup – Manage Azure network security groups — Ansible Documentation

- AnsibleFest
- Products
- Community
- Webinars & Training
- Blog

Documentation

# azure_rm_securitygroup – Manage Azure network security groups

- Synopsis

- Requirements

- Parameters

- Notes

- See Also

- Examples

- Return Values

- Status

## Synopsis

- Create, update or delete a network security group.

- A security group contains Access Control List (ACL) rules that allow or deny network traffic to subnets or individual network interfaces.

- A security group is created with a set of default security rules and an empty set of security rules.

- Shape traffic flow by adding rules to the empty set of security rules.

## 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 string |  | Active Directory username. Use when authenticating with an Active Directory user rather than service principal.
adfs_authority_url string added in 2.6 |  | Azure AD authority url. Use when authenticating with Username/password, and has your own ADFS authority.
api_profile string 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 boolean | 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 string 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 string added in 2.5 | Choices: ignore validate | 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 string |  | Azure client ID. Use when authenticating with a Service Principal.
cloud_environment string 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 string |  | Active Directory user password. Use when authenticating with an Active Directory user rather than service principal.
profile string |  | Security profile found in ~/.azure/credentials file.
purge_default_rules boolean | Choices: no ← yes | Remove any existing rules not matching those defined in the default_rules parameter.
purge_rules boolean | 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.
| access - | Choices: Allow ← Deny | Whether or not to allow the traffic flow.
| description - |  | Short description of the rule's purpose.
| destination_address_prefix - | Default: "*" | The destination address prefix. CIDR or destination IP range. Asterisk `*` 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.
| destination_application_security_groups list |  | List of the destination application security groups. It could be list of resource id. It could be list of names in same resource group. It could be list of dict containing resource_group and name. It is mutually exclusive with `destination_address_prefix` and `destination_address_prefixes`.
| 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.
| direction - | Choices: Inbound ← Outbound | Indicates the direction of the traffic flow.
| name - / required |  | Unique name for the rule.
| priority - / required |  | Order in which to apply the rule. Must a unique integer between 100 and 4096 inclusive.
| protocol - | Choices: Udp Tcp * ← | Accepted traffic protocol.
| source_address_prefix - | Default: "*" | The CIDR or source IP range. Asterisk `*` 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.
| source_application_security_groups list |  | List of the source application security groups. It could be list of resource id. It could be list of names in same resource group. It could be list of dict containing resource_group and name. It is mutually exclusive with `source_address_prefix` and `source_address_prefixes`.
| source_port_range - | Default: "*" | Port or range of ports from which traffic originates. It can accept string type or a list of string type.
secret string |  | 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 string |  | Your Azure subscription Id.
tags dictionary |  | 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 string |  | Azure tenant ID. Use when authenticating with a Service Principal.

## Notes

Note

- For authentication with Azure you can pass parameters, set environment variables, use a profile stored in ~/.azure/credentials, or log in before you run your tasks or playbook with `az login`.

- Authentication is also 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.

## See Also

See also

Sign in with Azure CLI

  How to authenticate using the `az login` command.

## Examples

    # Create a security group
    - azure_rm_securitygroup:
          resource_group: myResourceGroup
          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: 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

    # Delete security group
    - azure_rm_securitygroup:
          resource_group: myResourceGroup
          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 complex | always | Current state of the security group.
| default_rules list | always | The default security rules of network security group. Sample: [{'access': 'Allow', 'description': 'Allow inbound traffic from all VMs in VNET', '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': 'Allow inbound traffic from azure load balancer', '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': 'Deny all inbound traffic', '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': 'Allow outbound traffic from all VMs to all VMs in VNET', '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': 'Allow outbound traffic from all VMs to Internet', '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': 'Deny all outbound traffic', '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 string | always | The resource ID. Sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroup/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mysecgroup
| location string | always | The resource location. Sample: westus
| name string | always | Name of the security group. Sample: mysecgroup
| network_interfaces list | always | A collection of references to network interfaces.
| rules list | always | A collection of security rules of the network security group. Sample: [{'access': 'Deny', 'description': None, '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': None, '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 | always | A collection of references to subnets.
| tags dictionary | always | Tags to assign to the security group. Sample: {'delete': 'on-exit', 'foo': 'bar', 'testing': 'testing'}
| type string | always | The resource type. Sample: Microsoft.Network/networkSecurityGroups

## Status

- This module is not guaranteed to have a backwards compatible interface. [preview]

- This module is maintained by the Ansible Community. [community]

### Authors

- Chris Houseknecht (@chouseknecht)

- Matt Davis (@nitzmahone)

Hint

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