2018-05-29 08:22:59 +02:00
|
|
|
- name: Prepare random number
|
|
|
|
set_fact:
|
|
|
|
secgroupname: "sg{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
2019-04-01 10:56:16 +02:00
|
|
|
asg_name1: "asg1{{ resource_group | hash('md5') | truncate(7, True, '') }}"
|
|
|
|
asg_name2: "asg2{{ resource_group | hash('md5') | truncate(7, True, '') }}"
|
|
|
|
sg_name1: "sgasg{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
2018-05-29 08:22:59 +02:00
|
|
|
run_once: yes
|
|
|
|
|
|
|
|
|
2017-08-12 01:16:39 +02:00
|
|
|
- name: Create security group
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-05-29 08:22:59 +02:00
|
|
|
name: "{{ secgroupname }}"
|
2017-08-12 01:16:39 +02:00
|
|
|
tags:
|
|
|
|
testing: testing
|
|
|
|
delete: on-exit
|
|
|
|
foo: bar
|
|
|
|
purge_rules: yes
|
|
|
|
rules:
|
|
|
|
- name: DenySSH
|
|
|
|
protocol: Tcp
|
|
|
|
destination_port_range: 22
|
|
|
|
access: Deny
|
|
|
|
priority: 100
|
|
|
|
direction: Inbound
|
2017-11-22 04:20:00 +01:00
|
|
|
- name: AllowSSH
|
2017-08-12 01:16:39 +02:00
|
|
|
protocol: Tcp
|
2017-11-22 04:20:00 +01:00
|
|
|
source_address_prefix: 174.109.158.0/24
|
2017-08-12 01:16:39 +02:00
|
|
|
destination_port_range: 22
|
|
|
|
access: Allow
|
|
|
|
priority: 101
|
|
|
|
direction: Inbound
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert: { that: "{{ output.state.rules | length }} == 2" }
|
|
|
|
|
|
|
|
- name: Gather facts by tags
|
|
|
|
azure_rm_securitygroup_facts:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
tags:
|
|
|
|
- testing
|
|
|
|
- foo:bar
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that: azure_securitygroups | length == 1
|
|
|
|
|
|
|
|
- name: Add/Update rules on existing security group
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-05-29 08:22:59 +02:00
|
|
|
name: "{{ secgroupname }}"
|
2017-08-12 01:16:39 +02:00
|
|
|
rules:
|
2017-11-22 04:20:00 +01:00
|
|
|
- name: AllowSSH
|
2017-08-12 01:16:39 +02:00
|
|
|
protocol: Tcp
|
2017-11-22 04:20:00 +01:00
|
|
|
source_address_prefix: 174.108.158.0/24
|
|
|
|
destination_port_range: 22
|
|
|
|
access: Allow
|
|
|
|
priority: 101
|
2017-08-12 01:16:39 +02:00
|
|
|
- name: AllowSSHFromHome
|
|
|
|
protocol: Tcp
|
2017-11-22 04:20:00 +01:00
|
|
|
source_address_prefix: 174.109.158.0/24
|
2017-08-12 01:16:39 +02:00
|
|
|
destination_port_range: 22-23
|
|
|
|
priority: 102
|
|
|
|
register: output
|
|
|
|
|
2017-11-22 04:20:00 +01:00
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- "{{ output.state.rules | length }} == 3"
|
2018-05-16 10:28:14 +02:00
|
|
|
- output.state.rules[0].source_address_prefix == '174.108.158.0/24'
|
2017-08-12 01:16:39 +02:00
|
|
|
|
|
|
|
- name: Test idempotence
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-05-29 08:22:59 +02:00
|
|
|
name: "{{ secgroupname }}"
|
2017-08-12 01:16:39 +02:00
|
|
|
rules:
|
2017-11-22 04:20:00 +01:00
|
|
|
- name: AllowSSH
|
2017-08-12 01:16:39 +02:00
|
|
|
protocol: Tcp
|
2017-11-22 04:20:00 +01:00
|
|
|
source_address_prefix: 174.108.158.0/24
|
|
|
|
destination_port_range: 22
|
|
|
|
access: Allow
|
|
|
|
priority: 101
|
2017-08-12 01:16:39 +02:00
|
|
|
- name: AllowSSHFromHome
|
|
|
|
protocol: Tcp
|
2017-11-22 04:20:00 +01:00
|
|
|
source_address_prefix: 174.109.158.0/24
|
2017-08-12 01:16:39 +02:00
|
|
|
destination_port_range: 22-23
|
|
|
|
priority: 102
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that: not output.changed
|
|
|
|
|
|
|
|
- name: Update tags
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-05-29 08:22:59 +02:00
|
|
|
name: "{{ secgroupname }}"
|
2017-08-12 01:16:39 +02:00
|
|
|
tags:
|
|
|
|
testing: testing
|
|
|
|
delete: never
|
|
|
|
baz: bar
|
2018-08-16 05:35:39 +02:00
|
|
|
append_tags: false
|
2017-08-12 01:16:39 +02:00
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- output.state.tags | length == 3
|
|
|
|
- output.state.tags.delete == 'never'
|
|
|
|
|
|
|
|
- name: Purge tags
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-05-29 08:22:59 +02:00
|
|
|
name: "{{ secgroupname }}"
|
2018-08-16 05:35:39 +02:00
|
|
|
append_tags: false
|
2017-08-12 01:16:39 +02:00
|
|
|
tags:
|
|
|
|
testing: testing
|
|
|
|
delete: on-exit
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- output.state.tags | length == 2
|
|
|
|
- output.state.tags.delete == 'on-exit'
|
|
|
|
|
|
|
|
- name: Gather facts for one accounts
|
|
|
|
azure_rm_securitygroup_facts:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-05-29 08:22:59 +02:00
|
|
|
name: "{{ secgroupname }}"
|
2017-08-12 01:16:39 +02:00
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- azure_securitygroups | length == 1
|
|
|
|
|
|
|
|
- name: Gather facts for all accounts
|
|
|
|
azure_rm_securitygroup_facts:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- azure_securitygroups | length > 0
|
|
|
|
|
2018-05-16 10:28:14 +02:00
|
|
|
- name: Create security group with source_address_prefixes
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-05-29 08:22:59 +02:00
|
|
|
name: "{{ secgroupname }}"
|
2018-05-16 10:28:14 +02:00
|
|
|
tags:
|
|
|
|
testing: testing
|
|
|
|
delete: on-exit
|
|
|
|
foo: bar
|
|
|
|
purge_rules: yes
|
|
|
|
rules:
|
|
|
|
- name: AllowSSH
|
|
|
|
protocol: Tcp
|
|
|
|
source_address_prefix:
|
|
|
|
- 52.100.120.240
|
|
|
|
- 53.100.250.190
|
|
|
|
- 54.110.200.200
|
|
|
|
destination_port_range: 22
|
|
|
|
access: Allow
|
|
|
|
priority: 101
|
|
|
|
direction: Inbound
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- "{{ output.state.rules | length }} == 1"
|
|
|
|
- "{{ output.state.rules[0].source_address_prefixes | length }} == 3"
|
|
|
|
- not output.state.rules[0].source_address_prefix
|
|
|
|
|
|
|
|
- name: Create security group with source_address_prefixes(idempontent)
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-05-29 08:22:59 +02:00
|
|
|
name: "{{ secgroupname }}"
|
2018-05-16 10:28:14 +02:00
|
|
|
tags:
|
|
|
|
testing: testing
|
|
|
|
delete: on-exit
|
|
|
|
foo: bar
|
|
|
|
purge_rules: yes
|
|
|
|
rules:
|
|
|
|
- name: AllowSSH
|
|
|
|
protocol: Tcp
|
|
|
|
source_address_prefix:
|
|
|
|
- 52.100.120.240
|
|
|
|
- 53.100.250.190
|
|
|
|
- 54.110.200.200
|
|
|
|
destination_port_range: 22
|
|
|
|
access: Allow
|
|
|
|
priority: 101
|
|
|
|
direction: Inbound
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that: not output.changed
|
|
|
|
|
2018-08-08 05:41:46 +02:00
|
|
|
- name: Add a single one group
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
name: "{{ secgroupname }}"
|
|
|
|
tags:
|
|
|
|
testing: testing
|
|
|
|
delete: on-exit
|
|
|
|
foo: bar
|
|
|
|
rules:
|
|
|
|
- name: DenySSH
|
|
|
|
protocol: Tcp
|
|
|
|
source_address_prefix:
|
|
|
|
- 54.120.120.240
|
|
|
|
destination_port_range: 22
|
|
|
|
access: Deny
|
|
|
|
priority: 102
|
|
|
|
direction: Inbound
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- output.changed
|
|
|
|
- "{{ output.state.rules | length }} == 2"
|
|
|
|
|
2019-04-01 10:56:16 +02:00
|
|
|
- name: Create Application security group 1
|
|
|
|
azure_rm_applicationsecuritygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
name: "{{ asg_name1 }}"
|
|
|
|
tags:
|
|
|
|
testing: testing
|
|
|
|
register: asg1
|
|
|
|
|
|
|
|
- name: Create Application security group 2
|
|
|
|
azure_rm_applicationsecuritygroup:
|
|
|
|
resource_group: "{{ resource_group_secondary }}"
|
|
|
|
name: "{{ asg_name2 }}"
|
|
|
|
tags:
|
|
|
|
testing: testing
|
|
|
|
register: asg2
|
|
|
|
|
|
|
|
- name: Create security group with application security group
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
name: "{{ sg_name1 }}"
|
|
|
|
purge_rules: yes
|
|
|
|
rules:
|
|
|
|
- name: AsgToAsg
|
|
|
|
protocol: Tcp
|
|
|
|
source_application_security_groups:
|
|
|
|
- "{{ asg1.id }}"
|
|
|
|
destination_application_security_groups:
|
|
|
|
- resource_group: "{{ resource_group_secondary }}"
|
|
|
|
name: "{{ asg_name2 }}"
|
|
|
|
destination_port_range: 22
|
|
|
|
access: Allow
|
|
|
|
priority: 101
|
|
|
|
direction: Inbound
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- output.changed
|
|
|
|
|
|
|
|
- name: Create security group with application security group - Idempotent
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
name: "{{ sg_name1 }}"
|
|
|
|
purge_rules: yes
|
|
|
|
rules:
|
|
|
|
- name: AsgToAsg
|
|
|
|
protocol: Tcp
|
|
|
|
source_application_security_groups:
|
|
|
|
- "{{ asg_name1 }}"
|
|
|
|
destination_application_security_groups:
|
|
|
|
- resource_group: "{{ resource_group_secondary }}"
|
|
|
|
name: "{{ asg_name2 }}"
|
|
|
|
destination_port_range: 22
|
|
|
|
access: Allow
|
|
|
|
priority: 101
|
|
|
|
direction: Inbound
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- not output.changed
|
|
|
|
|
|
|
|
|
|
|
|
- name: Delete security group
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
name: "{{ sg_name1 }}"
|
|
|
|
state: absent
|
|
|
|
|
2017-08-12 01:16:39 +02:00
|
|
|
- name: Delete all security groups
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
name: "{{ item.name }}"
|
|
|
|
state: absent
|
|
|
|
with_items: "{{ azure_securitygroups }}"
|
|
|
|
|
|
|
|
- name: Should have no security groups remaining
|
|
|
|
azure_rm_securitygroup_facts:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- azure_securitygroups | length == 0
|