Skip to content

Refactoring-SG-creation-tasks #2439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions roles/aws/aws_ami/tasks/repack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
register: aws_ami_running_instances

- name: Create a Security Group to access the controller.
amazon.aws.ec2_security_group:
ansible.builtin.include_role:
name: aws/aws_vpc
tasks_from: security_group
vars:
aws_vpc:
profile: "{{ aws_ami.aws_profile }}"
region: "{{ aws_ami.region }}"
name: "{{ aws_ami.repack.cluster_name }}-repacker"
tags: "{{ aws_ami.tags }}"
state: present
vpc_id: "{{ aws_ami.repack.vpc_id }}"
id: "{{ aws_ami.repack.vpc_id }}"
description: "Allow controller to access the {{ aws_ami.ami_name }}-repacking instance"
rules:
- proto: tcp
Expand Down
45 changes: 26 additions & 19 deletions roles/aws/aws_backup_validation/tasks/testing_resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,32 @@
register: _main_subnets_info

- name: Create SG for restored instances.
amazon.aws.ec2_security_group:
name: Restore_testing
description: This SG is used to allow SSM and SSH access to the server
region: "{{ _aws_region }}"
vpc_id: "{{ _main_vpc_info.vpcs[0].vpc_id }}"
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
register: _restore_testing_sg
ansible.builtin.include_role:
name: aws/aws_vpc
tasks_from: security_group
vars:
aws_vpc:
name: "Restore_testing"
region: "{{ aws_ec2_autoscale_cluster.region }}"
id: "{{ _main_vpc_info.vpcs[0].vpc_id }}"
description: "This SG is used to allow SSM and SSH access to the server"
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0

- name: Construct AWS instance type dict.
ansible.builtin.set_fact:
_restore_testing_sg: "{{ aws_vpc._result['Restore_testing'] }}"

- name: Remove restore testing query file.
ansible.builtin.file:
Expand Down
31 changes: 19 additions & 12 deletions roles/aws/aws_ec2_autoscale_cluster/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,25 @@
when: (aws_ec2_autoscale_cluster.vpc_name is not defined or aws_ec2_autoscale_cluster.vpc_name | length < 0)

- name: Create matching Security Group.
amazon.aws.ec2_security_group:
profile: "{{ aws_ec2_autoscale_cluster.aws_profile }}"
region: "{{ aws_ec2_autoscale_cluster.region }}"
name: "{{ aws_ec2_autoscale_cluster.name }}"
tags: "{{ aws_ec2_autoscale_cluster.tags | combine({'Name': aws_ec2_autoscale_cluster.name}) }}"
state: "{{ aws_ec2_autoscale_cluster.state }}"
vpc_id: "{{ _aws_ec2_autoscale_cluster_vpc_id }}"
description: "Allow internal traffic for cluster {{ aws_ec2_autoscale_cluster.name }}"
rules:
- proto: all
group_name: "{{ aws_ec2_autoscale_cluster.name }}"
register: _aws_ec2_autoscale_cluster_security_group
ansible.builtin.include_role:
name: aws/aws_vpc
tasks_from: security_group
vars:
aws_vpc:
name: "{{ aws_ec2_autoscale_cluster.name }}"
profile: "{{ aws_ec2_autoscale_cluster.aws_profile }}"
region: "{{ aws_ec2_autoscale_cluster.region }}"
tags: "{{ aws_ec2_autoscale_cluster.tags | combine({'Name': aws_ec2_autoscale_cluster.name}) }}"
state: "{{ aws_ec2_autoscale_cluster.state }}"
id: "{{ _aws_ec2_autoscale_cluster_vpc_id }}"
description: "Allow internal traffic for cluster {{ aws_ec2_autoscale_cluster.name }}"
rules:
- proto: all
group_name: "{{ aws_ec2_autoscale_cluster.name }}"

- name: Set _aws_ec2_autoscale_cluster_security_group variable.
ansible.builtin.set_fact:
_aws_ec2_autoscale_cluster_security_group: "{{ aws_vpc._result[aws_ec2_autoscale_cluster.name] }}"

- name: Reset subnets lists.
ansible.builtin.set_fact:
Expand Down
31 changes: 22 additions & 9 deletions roles/aws/aws_vpc/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,31 @@
register: _aws_vpc_vpc

- name: Ensure default Security group is tagged.
amazon.aws.ec2_security_group:
name: "default"
profile: "{{ aws_vpc.aws_profile }}"
region: "{{ aws_vpc.region }}"
tags: "{{ aws_vpc.tags }}"
state: "{{ aws_vpc.state }}"
vpc_id: "{{ _aws_vpc_vpc.vpc.id }}"
description: "default VPC security group"
purge_rules: false
ansible.builtin.include_tasks: "security_group.yml"
vars:
aws_vpc:
name: "default"
profile: "{{ aws_vpc.aws_profile }}"
region: "{{ aws_vpc.region }}"
tags: "{{ aws_vpc.tags }}"
state: "{{ aws_vpc.state }}"
id: "{{ _aws_vpc_vpc.vpc.id }}"
description: "default VPC security group"
purge_rules: false

- name: Create VPC Security groups.
ansible.builtin.include_tasks: "security_group.yml"
vars:
aws_vpc:
name: "{{ security_group.name }}"
profile: "{{ aws_vpc.aws_profile }}"
region: "{{ aws_vpc.region }}"
tags: "{{ aws_vpc.tags | combine({'Name': security_group.name}) }}"
state: "{{ aws_vpc.state }}"
id: "{{ _aws_vpc_vpc.vpc.id }}"
description: "default VPC security group"
rules: "{{ security_group.rules | default(omit) }}"
rules_egress: "{{ security_group.rules_egress | default(omit) }}"
with_items: "{{ aws_vpc.security_groups }}"
loop_control:
loop_var: security_group
Expand Down
18 changes: 12 additions & 6 deletions roles/aws/aws_vpc/tasks/security_group.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
- name: Create Security Group.
amazon.aws.ec2_security_group:
name: "{{ security_group.name }}"
name: "{{ aws_vpc.name }}"
profile: "{{ aws_vpc.aws_profile }}"
region: "{{ aws_vpc.region }}"
tags: "{{ aws_vpc.tags | combine({'Name': security_group.name}) }}"
tags: "{{ aws_vpc.tags | combine({'Name': aws_vpc.name}) }}"
state: "{{ aws_vpc.state }}"
vpc_id: "{{ _aws_vpc_vpc.vpc.id }}"
description: "{{ security_group.description }}"
rules: "{{ security_group.rules | default(omit) }}"
rules_egress: "{{ security_group.rules_egress | default(omit) }}"
vpc_id: "{{ aws_vpc.id }}"
description: "{{ aws_vpc.description }}"
rules: "{{ aws_vpc.rules | default(omit) }}"
rules_egress: "{{ aws_vpc.rules_egress | default(omit) }}"
purge_rules: "{{ aws_vpc.purge_rules | default(omit) }}"
register: _aws_vpc_result

- name: Register aws_vpc SG results.
ansible.builtin.set_fact:
aws_vpc: "{{ aws_vpc | combine({'_result': {aws_vpc.name: _aws_vpc_result}}, recursive=True) }}"
26 changes: 15 additions & 11 deletions roles/aws/aws_vpc_subnet/tasks/subnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@
when: subnet.nat_ipv4 is defined and subnet.nat_ipv4

- name: Create matching Security Group.
amazon.aws.ec2_security_group:
name: "{{ subnet.name }}"
profile: "{{ aws_vpc_subnet.aws_profile }}"
region: "{{ aws_vpc_subnet.region }}"
tags: "{{ aws_vpc_subnet.tags | combine({'Name': subnet.name}) }}"
state: "{{ aws_vpc_subnet.state }}"
vpc_id: "{{ _aws_vpc_subnet_vpc_id }}"
description: "Allow internal traffic for subnet {{ subnet.name }}"
rules:
- proto: all
group_name: "{{ subnet.name }}"
ansible.builtin.include_role:
name: aws/aws_vpc
tasks_from: security_group
vars:
aws_vpc:
name: "{{ subnet.name }}"
profile: "{{ aws_vpc_subnet.aws_profile }}"
region: "{{ aws_vpc_subnet.region }}"
tags: "{{ aws_vpc_subnet.tags | combine({'Name': subnet.name}) }}"
state: "{{ aws_vpc_subnet.state }}"
id: "{{ _aws_vpc_subnet_vpc_id }}"
description: "Allow internal traffic for subnet {{ subnet.name }}"
rules:
- proto: all
group_name: "{{ subnet.name }}"
when:
- subnet.security_group is defined
- subnet.security_group
Loading