still working on aws

This commit is contained in:
n0mad1k
2025-05-02 12:06:09 -04:00
parent a86f9a9d25
commit f6d08e84eb
5 changed files with 510 additions and 239 deletions
+91 -22
View File
@@ -1,5 +1,6 @@
---
# AWS C2 Deployment Playbook
# AWS C2 Deployment Playbook (Fixed)
- name: Deploy AWS C2 server
hosts: localhost
@@ -23,13 +24,23 @@
- aws_access_key is defined and aws_access_key != ""
- aws_secret_key is defined and aws_secret_key != ""
fail_msg: "AWS credentials are required. Set aws_access_key and aws_secret_key."
# Select the region first - CRITICAL FIX
- name: Set region for AWS C2 server
set_fact:
aws_c2_region: "{{ aws_region | default(aws_region_choices | random if aws_region_choices is defined else 'us-east-1') }}"
- name: Set AMI ID for the selected region
set_fact:
ami_id: "{{ ami_map[aws_c2_region] }}"
when: ami_map is defined and aws_c2_region in ami_map
- name: Create EC2 key pair for C2
amazon.aws.ec2_key:
access_key: "{{ aws_access_key }}"
secret_key: "{{ aws_secret_key }}"
name: "{{ c2_name }}"
region: "{{ aws_region }}"
region: "{{ aws_c2_region }}"
state: present
register: c2_key_pair
@@ -40,12 +51,68 @@
mode: "0600"
when: c2_key_pair.changed and c2_key_pair.key.private_key is defined
- name: Create VPC
amazon.aws.ec2_vpc_net:
name: "{{ c2_name }}-vpc"
cidr_block: "10.0.0.0/16"
region: "{{ aws_c2_region }}"
tags:
Name: "{{ c2_name }}-vpc"
c2_name: "{{ c2_name }}"
state: present
register: vpc_result
- name: Store VPC ID
set_fact:
vpc_id: "{{ vpc_result.vpc.id }}"
- name: Wait for VPC to be fully available
pause:
seconds: 10
when: vpc_result.changed
- name: Create internet gateway for VPC
amazon.aws.ec2_vpc_igw:
vpc_id: "{{ vpc_id }}"
region: "{{ aws_c2_region }}"
state: present
tags:
Name: "{{ c2_name }}-igw"
c2_name: "{{ c2_name }}"
register: igw_result
- name: Create subnet in VPC
amazon.aws.ec2_vpc_subnet:
vpc_id: "{{ vpc_id }}"
cidr: "10.0.1.0/24"
region: "{{ aws_c2_region }}"
az: "{{ aws_c2_region }}a"
map_public: yes
tags:
Name: "{{ c2_name }}-subnet"
c2_name: "{{ c2_name }}"
register: subnet_result
- name: Create routing table for internet access
amazon.aws.ec2_vpc_route_table:
vpc_id: "{{ vpc_id }}"
region: "{{ aws_c2_region }}"
tags:
Name: "{{ c2_name }}-rtb"
c2_name: "{{ c2_name }}"
routes:
- dest: "0.0.0.0/0"
gateway_id: "{{ igw_result.gateway_id }}"
subnets:
- "{{ subnet_result.subnet.id }}"
register: route_table_result
- name: Create a security group for the C2 server
amazon.aws.ec2_security_group:
name: "{{ c2_name }}-sg"
description: "Security group for C2 server {{ c2_name }}"
vpc_id: "{{ vpc_id }}"
region: "{{ aws_region }}"
region: "{{ aws_c2_region }}"
rules:
- proto: tcp
ports:
@@ -63,40 +130,37 @@
state: present
register: security_group
- name: Set region for AWS C2 server
set_fact:
aws_c2_region: "{{ aws_region | default(region, true) | default('us-east-1', true) }}"
when: aws_region_choices is not defined or aws_region_choices|length == 0
- name: Set random region for AWS C2 server
set_fact:
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
- 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
amazon.aws.ec2_instance:
name: "{{ c2_name }}"
key_name: "{{ aws_keypair_name }}"
key_name: "{{ c2_name }}"
instance_type: "{{ instance_type | default('t2.medium') }}"
security_group: "{{ c2_name }}-sg"
vpc_subnet_id: "{{ subnet_result.subnet.id }}"
security_groups:
- "{{ security_group.group_id }}"
image_id: "{{ ami_id }}"
region: "{{ aws_c2_region }}"
state: present
wait: yes
volumes:
- device_name: "/dev/xvda"
ebs:
volume_size: 100
delete_on_termination: true
tags:
Name: "{{ c2_name }}"
register: ec2
register: c2_instance
- name: Set c2_ip for later use
set_fact:
c2_ip: "{{ c2_instance.instances[0].public_ip_address }}"
c2_instance_id: "{{ c2_instance.instances[0].instance_id }}"
- name: Wait for C2 instance initialization (longer delay)
pause:
seconds: 180
when: c2_instance.changed
- name: Wait for C2 SSH to be available
wait_for:
host: "{{ c2_ip }}"
@@ -105,6 +169,11 @@
timeout: 300
state: started
- name: Set correct permissions on SSH key
file:
path: "~/.ssh/{{ c2_name }}.pem"
mode: "0600"
- name: Add C2 to inventory
add_host:
name: "c2"
@@ -112,7 +181,7 @@
ansible_host: "{{ c2_ip }}"
ansible_user: "{{ ssh_user }}"
ansible_ssh_private_key_file: "~/.ssh/{{ c2_name }}.pem"
ansible_ssh_common_args: "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
ansible_ssh_common_args: "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes"
- name: Configure C2 server
hosts: c2servers