Skip to content

Commit 54652e2

Browse files
authored
Merge pull request #126 from hakbailey/update-ec2-networking-role
Updates to networking role
2 parents 829bcf0 + 7fece3b commit 54652e2

File tree

11 files changed

+472
-147
lines changed

11 files changed

+472
-147
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- ec2_networking_resources - Add optional networking resources and ability to delete resources created by role. (https://github.com/redhat-cop/cloud.aws_ops/pull/126)

roles/ec2_networking_resources/README.md

+34-15
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,28 @@ An AWS account with the following permissions:
2626
Role Variables
2727
--------------
2828

29-
* **ec2_networking_resources_vpc_name**: (Required) The name of the VPC to create.
30-
* **ec2_networking_resources_vpc_cidr_block**: (Required) The CIDR block to use for the VPC being created.
31-
* **ec2_networking_resources_subnet_cidr_block**: (Required) The CIDR block to use for subnet being created.
32-
* **ec2_networking_resources_sg_internal_name**: (Required) The name of the security group to create.
33-
* **ec2_networking_resources_sg_internal_description**: (Required) The description of the security group being created.
34-
* **ec2_networking_resources_sg_internal_rules**: (Optional) List of rules to apply to the security group being created. By default, a rule allowing SSH access from within the VPC will be added. A rule should contain the following keys:
35-
* **proto** (str): The IP protocol name.
36-
* **ports** (str): A list of ports traffic is going to. Can be a single port, or a range of ports, for example, 8000-8010.
37-
* **cidr_ip** (str): The CIDR block traffic is coming from.
29+
* **ec2_networking_resources_operation**: (Optional) Target operation for the networking resources role. Choices are ["create", "delete"]. Defaults to "create".
30+
* **ec2_networking_resources_vpc_name**: (Required) The name of the VPC to create or delete.
31+
* **ec2_networking_resources_vpc_cidr_block**: (Optional) The CIDR block to use for the VPC being created. Required if `ec2_networking_resources_operation` is "create".
32+
* **ec2_networking_resources_subnet_cidr_block**: (Optional) The CIDR block to use for subnet being created. Required if `ec2_networking_resources_operation` is "create".
33+
* **ec2_networking_resources_sg_name**: (Optional) The name of the security group to create. Required if `ec2_networking_resources_operation` is "create".
34+
* **ec2_networking_resources_sg_description**: (Optional) The description of the security group being created. Defaults to "Security group for EC2 instance".
35+
* **ec2_networking_resources_sg_rules**: (Optional) List of rules to apply to the security group being created. By default, a rule allowing SSH access from within the VPC will be added. A rule should contain the following keys:
36+
* **proto** (str): The IP protocol name.
37+
* **ports** (list): A list of ports traffic is going to. Can be a single port or a range of ports, for example 8000-8010.
38+
* **cidr_ip** (str): The CIDR block traffic is coming from.
39+
* **ec2_networking_resources_create_igw**: (Optional) Whether to create an internet gateway and route traffic to it. Defaults to `false`.
3840

3941
Dependencies
4042
------------
4143

4244
- role: [aws_setup_credentials](../aws_setup_credentials/README.md)
4345

44-
Example Playbook
46+
Examples
4547
----------------
4648

49+
Create networking resources with an internet gateway and allow HTTP/HTTPS traffic:
50+
4751
```yaml
4852
- hosts: localhost
4953
roles:
@@ -52,15 +56,30 @@ Example Playbook
5256
ec2_networking_resources_vpc_name: my-vpn
5357
ec2_networking_resources_vpc_cidr_block: 10.0.1.0/16
5458
ec2_networking_resources_subnet_cidr_block: 10.0.1.0/26
55-
ec2_networking_resources_sg_internal_name: my-sg
56-
ec2_networking_resources_sg_internal_description: My internal security group
57-
ec2_networking_resources_sg_internal_rules:
59+
ec2_networking_resources_sg_name: my-sg
60+
ec2_networking_resources_sg_description: My security group
61+
ec2_networking_resources_sg_rules:
5862
- proto: tcp
5963
ports: 22
6064
cidr_ip: 10.0.1.0/16
6165
- ports: tcp
62-
ports: 8000-8010
63-
cidr_ip: 10.0.1.0/16
66+
ports: 80
67+
cidr_ip: 0.0.0.0/0
68+
- proto: tcp
69+
ports: 443
70+
cidr_ip: 0.0.0.0/0
71+
ec2_networking_resources_create_igw: true
72+
```
73+
74+
Delete networking resources:
75+
76+
```yaml
77+
- hosts: localhost
78+
roles:
79+
- role: cloud.aws_ops.ec2_networking_resources
80+
vars:
81+
ec2_networking_resources_operation: delete
82+
ec2_networking_resources_vpc_name: my-vpn
6483
```
6584
6685
License
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
---
2-
ec2_networking_resources_sg_internal_rules:
2+
ec2_networking_resources_operation: create
3+
ec2_networking_resources_vpc_cidr_block: "{{ ec2_networking_resources_operation == 'delete' | ternary('', omit) }}"
4+
ec2_networking_resources_sg_description: Security group for EC2 instance
5+
ec2_networking_resources_sg_rules:
36
- proto: tcp
47
ports: 22
58
cidr_ip: "{{ ec2_networking_resources_vpc_cidr_block }}"
9+
ec2_networking_resources_create_igw: false

roles/ec2_networking_resources/meta/argument_specs.yml

+28-14
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,39 @@ argument_specs:
44
short_description: A role to create a basic networking environment for an EC2 instance.
55
description:
66
- A role to create a basic networking environment for an EC2 instance.
7-
- Creates a VPC, subnet, route table and security groups.
7+
- Creates a VPC, subnet, route table, and security group.
8+
- Can optionally create an internet gateway.
9+
- Can also delete networking resources created by this role using the "delete" operation.
810
options:
11+
ec2_networking_resources_operation:
12+
description:
13+
- Whether to create or delete the resources.
14+
choices: [create, delete]
15+
default: create
916
ec2_networking_resources_vpc_name:
1017
description:
11-
- The name of the VPC to create.
18+
- The name of the VPC to create or delete.
1219
required: true
1320
ec2_networking_resources_vpc_cidr_block:
1421
description:
15-
- The CIDR block for the VPC being created.
16-
required: true
22+
- The CIDR block for the VPC being created. Required when creating resources.
23+
required: false
1724
ec2_networking_resources_subnet_cidr_block:
1825
description:
19-
- The CIDR block for the subnet being created.
20-
required: true
21-
ec2_networking_resources_sg_internal_name:
26+
- The CIDR block for the subnet being created. Required when creating resources.
27+
required: false
28+
ec2_networking_resources_sg_name:
2229
description:
23-
- The name of the security group to create for internal access to the EC2 instance.
24-
required: true
25-
ec2_networking_resources_sg_internal_description:
30+
- The name of the security group to create. Required when creating resources.
31+
required: false
32+
ec2_networking_resources_sg_description:
2633
description:
27-
- The description of the security group for internal access to the EC2 instance.
28-
required: true
29-
ec2_networking_resources_sg_internal_rules:
34+
- The description of the security group.
35+
required: false
36+
default: Security group for EC2 instance
37+
ec2_networking_resources_sg_rules:
3038
description:
31-
- A list of security group rules to apply to the security group for internal access.
39+
- A list of security group rules to apply to the security group.
3240
- By default, will add a rule to allow SSH access from within the VPC created by the role.
3341
required: false
3442
type: list
@@ -49,3 +57,9 @@ argument_specs:
4957
elements: str
5058
cidr_ip:
5159
description: The CIDR range traffic is coming from.
60+
ec2_networking_resources_create_igw:
61+
description:
62+
- Whether to create an internet gateway and route traffic to it.
63+
required: false
64+
type: bool
65+
default: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
- name: Validate options
3+
ansible.builtin.fail:
4+
msg: "When creating resources, all of the following options must be provided: ec2_networking_resources_vpc_cidr_block, ec2_networking_resources_subnet_cidr_block, ec2_networking_resources_sg_name"
5+
when: ec2_networking_resources_vpc_cidr_block | default("", true) == "" or
6+
ec2_networking_resources_subnet_cidr_block | default("", true) == "" or
7+
ec2_networking_resources_sg_name | default("", true) == ""
8+
9+
- name: Create VPC
10+
amazon.aws.ec2_vpc_net:
11+
name: "{{ ec2_networking_resources_vpc_name }}"
12+
cidr_block: "{{ ec2_networking_resources_vpc_cidr_block }}"
13+
register: ec2_networking_resources_vpc_result
14+
15+
- name: Create VPC subnet
16+
amazon.aws.ec2_vpc_subnet:
17+
vpc_id: "{{ ec2_networking_resources_vpc_result.vpc.id }}"
18+
cidr: "{{ ec2_networking_resources_subnet_cidr_block }}"
19+
register: ec2_networking_resources_subnet_result
20+
21+
- name: Create security group
22+
amazon.aws.ec2_security_group:
23+
vpc_id: "{{ ec2_networking_resources_vpc_result.vpc.id }}"
24+
name: "{{ ec2_networking_resources_sg_name }}"
25+
description: "{{ ec2_networking_resources_sg_description }}"
26+
rules: "{{ ec2_networking_resources_sg_rules }}"
27+
register: ec2_networking_resources_sg_result
28+
29+
- name: Create internet gateway and route traffic to it
30+
when: ec2_networking_resources_create_igw is true
31+
block:
32+
- name: Create internet gateway
33+
amazon.aws.ec2_vpc_igw:
34+
state: present
35+
vpc_id: "{{ ec2_networking_resources_vpc_result.vpc.id }}"
36+
register: ec2_networking_resources_internet_gateway_result
37+
38+
- name: Create route table
39+
amazon.aws.ec2_vpc_route_table:
40+
state: present
41+
vpc_id: "{{ ec2_networking_resources_vpc_result.vpc.id }}"
42+
subnets:
43+
- "{{ ec2_networking_resources_subnet_result.subnet.id }}"
44+
routes:
45+
- dest: "0.0.0.0/0"
46+
gateway_id: "{{ ec2_networking_resources_internet_gateway_result.gateway_id }}"
47+
register: ec2_networking_resources_route_table_result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
---
2+
- name: Get VPC info
3+
amazon.aws.ec2_vpc_net_info:
4+
filters:
5+
"tag:Name": "{{ ec2_networking_resources_vpc_name }}"
6+
register: vpc_info
7+
8+
- name: Set VPC ID
9+
ansible.builtin.set_fact:
10+
vpc_id: "{{ vpc_info.vpcs[0].vpc_id }}"
11+
12+
- name: Get VPC security groups
13+
amazon.aws.ec2_security_group_info:
14+
filters:
15+
vpc-id: "{{ vpc_id }}"
16+
register: vpc_security_groups
17+
18+
- name: Delete VPC security groups
19+
amazon.aws.ec2_security_group:
20+
state: absent
21+
group_id: "{{ item.group_id }}"
22+
loop: "{{ vpc_security_groups.security_groups }}"
23+
when: item.group_name != "default"
24+
25+
- name: Get VPC subnets
26+
amazon.aws.ec2_vpc_subnet_info:
27+
filters:
28+
vpc-id: "{{ vpc_id }}"
29+
register: vpc_subnets
30+
31+
- name: Delete VPC subnets
32+
amazon.aws.ec2_vpc_subnet:
33+
state: absent
34+
vpc_id: "{{ vpc_id }}"
35+
cidr: "{{ item.cidr_block }}"
36+
loop: "{{ vpc_subnets.subnets }}"
37+
38+
- name: Delete VPC internet gateways
39+
amazon.aws.ec2_vpc_igw:
40+
state: absent
41+
vpc_id: "{{ vpc_id }}"
42+
43+
- name: Get VPC route tables
44+
amazon.aws.ec2_vpc_route_table_info:
45+
filters:
46+
vpc-id: "{{ vpc_id }}"
47+
register: vpc_route_tables
48+
49+
- name: Delete VPC route tables
50+
amazon.aws.ec2_vpc_route_table:
51+
state: absent
52+
vpc_id: "{{ vpc_id }}"
53+
lookup: id
54+
route_table_id: "{{ item.id }}"
55+
loop: "{{ vpc_route_tables.route_tables }}"
56+
when: item.associations | length == 0 or true not in item.associations | map(attribute='main')
57+
58+
- name: Delete VPC
59+
amazon.aws.ec2_vpc_net:
60+
vpc_id: "{{ vpc_id }}"
61+
state: absent

roles/ec2_networking_resources/tasks/main.yml

+6-23
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,10 @@
33
module_defaults:
44
group/aws: "{{ aws_setup_credentials__output }}"
55
block:
6-
- name: Create VPC
7-
amazon.aws.ec2_vpc_net:
8-
name: "{{ ec2_networking_resources_vpc_name }}"
9-
cidr_block: "{{ ec2_networking_resources_vpc_cidr_block }}"
10-
register: ec2_networking_resources_vpc_result
6+
- name: Include create operations
7+
ansible.builtin.include_tasks: create.yml
8+
when: ec2_networking_resources_operation == 'create'
119

12-
- name: Create VPC subnet
13-
amazon.aws.ec2_vpc_subnet:
14-
vpc_id: "{{ ec2_networking_resources_vpc_result.vpc.id }}"
15-
cidr: "{{ ec2_networking_resources_subnet_cidr_block }}"
16-
register: ec2_networking_resources_subnet_result
17-
18-
- name: Create route table
19-
amazon.aws.ec2_vpc_route_table:
20-
vpc_id: "{{ ec2_networking_resources_vpc_result.vpc.id }}"
21-
subnets:
22-
- "{{ ec2_networking_resources_subnet_result.subnet.id }}"
23-
24-
- name: Create security group for internal access
25-
amazon.aws.ec2_security_group:
26-
vpc_id: "{{ ec2_networking_resources_vpc_result.vpc.id }}"
27-
name: "{{ ec2_networking_resources_sg_internal_name }}"
28-
description: "{{ ec2_networking_resources_sg_internal_description }}"
29-
rules: "{{ ec2_networking_resources_sg_internal_rules }}"
10+
- name: Include delete operations
11+
ansible.builtin.include_tasks: delete.yml
12+
when: ec2_networking_resources_operation == 'delete'

0 commit comments

Comments
 (0)