working on AWS

This commit is contained in:
n0mad1k
2025-05-02 10:01:07 -04:00
parent e3fe2b0509
commit c7348a952a
4 changed files with 117 additions and 19 deletions
+5
View File
@@ -73,6 +73,11 @@
aws_c2_region: "{{ aws_region_choices | random }}" aws_c2_region: "{{ aws_region_choices | random }}"
when: aws_region_choices is defined and aws_region_choices|length > 0 and aws_c2_region is not defined when: aws_region_choices is defined and aws_region_choices|length > 0 and aws_c2_region is not defined
- name: Set AMI ID for the selected region
set_fact:
ami_id: "{{ ami_map[aws_redirector_region] }}"
when: ami_map is defined and aws_redirector_region in ami_map
- name: Launch EC2 instance for C2 server - name: Launch EC2 instance for C2 server
amazon.aws.ec2_instance: amazon.aws.ec2_instance:
name: "{{ c2_name }}" name: "{{ c2_name }}"
+84 -1
View File
@@ -117,7 +117,87 @@
ignore_errors: yes ignore_errors: yes
when: vpc_info.vpcs is defined and vpc_info.vpcs | length > 0 when: vpc_info.vpcs is defined and vpc_info.vpcs | length > 0
# Delete in proper order (dependencies first) - name: Ensure all internet gateways are detached and deleted
block:
- name: Get all internet gateways for the VPC
command: >
aws ec2 describe-internet-gateways
--region {{ aws_region }}
--filters "Name=attachment.vpc-id,Values={{ vpc_info.vpcs[0].id }}"
--query "InternetGateways[*].InternetGatewayId"
--output text
environment:
AWS_ACCESS_KEY_ID: "{{ aws_access_key }}"
AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}"
register: igw_ids
when: vpc_info.vpcs is defined and vpc_info.vpcs | length > 0
- name: Detach internet gateways
command: >
aws ec2 detach-internet-gateway
--region {{ aws_region }}
--internet-gateway-id {{ item }}
--vpc-id {{ vpc_info.vpcs[0].id }}
environment:
AWS_ACCESS_KEY_ID: "{{ aws_access_key }}"
AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}"
with_items: "{{ igw_ids.stdout_lines | default([]) }}"
when: vpc_info.vpcs is defined and vpc_info.vpcs | length > 0 and igw_ids.stdout != ""
ignore_errors: yes
- name: Delete internet gateways
command: >
aws ec2 delete-internet-gateway
--region {{ aws_region }}
--internet-gateway-id {{ item }}
environment:
AWS_ACCESS_KEY_ID: "{{ aws_access_key }}"
AWS_SECRET_ACCESS_KEY: "{{ aws_secret_key }}"
with_items: "{{ igw_ids.stdout_lines | default([]) }}"
when: igw_ids.stdout is defined and igw_ids.stdout != ""
ignore_errors: yes
when: vpc_info.vpcs is defined and vpc_info.vpcs | length > 0
ignore_errors: yes
# Add this task to the cleanup script to get all VPCs with redirector tags
- name: Get all VPCs with redirector tags
amazon.aws.ec2_vpc_net_info:
region: "{{ aws_region }}"
register: all_vpcs_info
# Add a task to identify VPCs to cleanup based on tags
- name: Identify redirector VPCs for cleanup
set_fact:
vpcs_to_cleanup: "{{ all_vpcs_info.vpcs | selectattr('tags.redirector_name', 'defined') | list }}"
when: all_vpcs_info.vpcs is defined
# Enhanced VPC deletion with better dependency handling
- name: Delete all VPC resources
block:
- name: Delete associated subnets
amazon.aws.ec2_vpc_subnet:
vpc_id: "{{ item.id }}"
state: absent
region: "{{ aws_region }}"
with_items: "{{ vpcs_to_cleanup }}"
ignore_errors: yes
- name: Ensure all internet gateways are detached
shell: >
aws ec2 describe-internet-gateways --region {{ aws_region }} --filters "Name=attachment.vpc-id,Values={{ item.id }}" --query "InternetGateways[*].InternetGatewayId" --output text |
xargs -I % aws ec2 detach-internet-gateway --region {{ aws_region }} --internet-gateway-id % --vpc-id {{ item.id }}
with_items: "{{ vpcs_to_cleanup }}"
ignore_errors: yes
- name: Delete all VPCs
amazon.aws.ec2_vpc_net:
vpc_id: "{{ item.id }}"
state: absent
region: "{{ aws_region }}"
with_items: "{{ vpcs_to_cleanup }}"
ignore_errors: yes
when: vpcs_to_cleanup is defined and vpcs_to_cleanup | length > 0
- name: Delete route tables - name: Delete route tables
amazon.aws.ec2_vpc_route_table: amazon.aws.ec2_vpc_route_table:
route_table_id: "{{ item.id }}" route_table_id: "{{ item.id }}"
@@ -171,6 +251,9 @@
ignore_errors: yes ignore_errors: yes
when: vpc_info.vpcs is defined and vpc_info.vpcs | length > 0 when: vpc_info.vpcs is defined and vpc_info.vpcs | length > 0
- name: Terminate redirector instance - name: Terminate redirector instance
amazon.aws.ec2_instance: amazon.aws.ec2_instance:
state: absent state: absent
+27 -17
View File
@@ -114,31 +114,41 @@
aws_redirector_region: "{{ aws_region_choices | random }}" aws_redirector_region: "{{ aws_region_choices | random }}"
when: aws_region_choices is defined and aws_region_choices|length > 0 and aws_redirector_region is not defined when: aws_region_choices is defined and aws_region_choices|length > 0 and aws_redirector_region is not defined
- name: Launch EC2 instance for redirector - name: Set AMI ID for the selected region
amazon.aws.ec2_instance: set_fact:
name: "{{ redirector_name }}" ami_id: "{{ ami_map[aws_redirector_region] }}"
key_name: "{{ aws_keypair_name }}" when: ami_map is defined and aws_redirector_region in ami_map
instance_type: "{{ instance_type | default('t2.micro') }}"
security_group: "{{ redirector_name }}-sg" - name: Create security group for redirector
image_id: "{{ ami_id }}" amazon.aws.ec2_security_group:
name: "{{ redirector_name }}-sg"
description: "Security group for redirector {{ redirector_name }}"
vpc_id: "{{ vpc_id }}"
region: "{{ aws_redirector_region }}" region: "{{ aws_redirector_region }}"
rules:
- proto: tcp
ports:
- 22
- 80
- 443
- "{{ shell_handler_port | default(4444) }}"
cidr_ip: 0.0.0.0/0
rules_egress:
- proto: -1
cidr_ip: 0.0.0.0/0
state: present state: present
wait: yes register: security_group
tags:
Name: "{{ redirector_name }}"
register: ec2
- name: Launch EC2 instance for redirector - name: Launch EC2 instance for redirector
amazon.aws.ec2_instance: amazon.aws.ec2_instance:
name: "{{ redirector_name }}" name: "{{ redirector_name }}"
key_name: "{{ aws_keypair_name }}" key_name: "{{ redirector_name }}"
instance_type: "{{ redirector_instance_type | default('t2.micro') }}"
vpc_subnet_id: "{{ subnet_result.subnet.id | default(omit) }}" vpc_subnet_id: "{{ subnet_result.subnet.id | default(omit) }}"
instance_type: "{{ instance_type | default('t2.micro') }}" security_group_ids:
security_group: "{{ redirector_name }}-sg" - "{{ security_group.group_id }}"
network:
assign_public_ip: true
image_id: "{{ ami_id }}" image_id: "{{ ami_id }}"
region: "{{ aws_region }}" region: "{{ aws_redirector_region }}"
state: present state: present
wait: yes wait: yes
tags: tags:
+1 -1
View File
@@ -34,7 +34,7 @@
community.general.linode_v4: community.general.linode_v4:
access_token: "{{ linode_token }}" access_token: "{{ linode_token }}"
label: "{{ redirector_name }}" label: "{{ redirector_name }}"
type: "{{ plan }}" type: "{{ redirector_plan | default('g6-nanode-1') }}"
region: "{{ deployment_region }}" region: "{{ deployment_region }}"
image: "linode/debian12" image: "linode/debian12"
root_pass: "{{ lookup('password', '/dev/null length=16') }}" root_pass: "{{ lookup('password', '/dev/null length=16') }}"