working on aws

This commit is contained in:
n0mad1k
2025-05-02 11:01:04 -04:00
parent c7348a952a
commit a86f9a9d25
2 changed files with 119 additions and 192 deletions
+36 -22
View File
@@ -10,7 +10,6 @@
vars:
# Default values for required variables
ssh_user: "{{ ssh_user | default('kali') }}"
aws_region: "{{ aws_region | default(aws_region_choices | random) }}"
instance_type: "{{ aws_instance_type | default('t2.micro') }}"
# Generate random instance name if not provided
@@ -26,11 +25,18 @@
- 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."
# Set a single consistent region variable early
- name: Set deployment region
set_fact:
aws_region: "{{ aws_region | default(aws_region_choices | random if aws_region_choices is defined and aws_region_choices | length > 0 else 'us-east-1') }}"
- name: Display selected region
debug:
msg: "Deploying in AWS region: {{ aws_region }}"
- name: Create EC2 key pair for redirector
amazon.aws.ec2_key:
access_key: "{{ aws_access_key }}"
secret_key: "{{ aws_secret_key }}"
name: "{{ redirector_name }}"
region: "{{ aws_region }}"
state: present
@@ -63,7 +69,7 @@
- name: Set VPC ID from existing or newly created VPC
set_fact:
vpc_id: "{{ vpc_id if (vpc_id is defined and vpc_id | length > 0 and vpc_info.vpcs | length > 0) else vpc_result.vpc.id }}"
vpc_id: "{{ vpc_id if (vpc_id is defined and vpc_id | length > 0 and vpc_info.vpcs | default([]) | length > 0) else vpc_result.vpc.id }}"
- name: Create internet gateway for VPC
amazon.aws.ec2_vpc_igw:
@@ -104,27 +110,17 @@
register: route_table_result
when: vpc_result.changed | default(false) and igw_result.changed | default(false) and subnet_result.changed | default(false)
- name: Set region for AWS redirector
set_fact:
aws_redirector_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 redirector
set_fact:
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
- 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
ami_id: "{{ ami_map[aws_region] }}"
when: ami_map is defined and aws_region in ami_map
- name: Create security group for redirector
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_region }}"
rules:
- proto: tcp
ports:
@@ -145,29 +141,47 @@
key_name: "{{ redirector_name }}"
instance_type: "{{ redirector_instance_type | default('t2.micro') }}"
vpc_subnet_id: "{{ subnet_result.subnet.id | default(omit) }}"
security_group_ids:
- "{{ security_group.group_id }}"
security_groups:
- "{{ redirector_name }}-sg"
image_id: "{{ ami_id }}"
region: "{{ aws_redirector_region }}"
region: "{{ aws_region }}"
state: present
wait: yes
tags:
Name: "{{ redirector_name }}"
register: ec2
redirector_name: "{{ redirector_name }}"
aws_region: "{{ aws_region }}"
register: redirector_instance
- name: Set redirector_ip for later use
set_fact:
redirector_ip: "{{ redirector_instance.instances[0].public_ip_address }}"
redirector_instance_id: "{{ redirector_instance.instances[0].instance_id }}"
# Added longer wait time for SSH to be available
- name: Wait for redirector SSH to be available
wait_for:
host: "{{ redirector_ip }}"
port: 22
delay: 30
delay: 60
timeout: 300
state: started
# Added SSH connection test to verify credentials
- name: Test SSH connection to instance
command: >
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ConnectTimeout=10
-i ~/.ssh/{{ redirector_name }}.pem {{ ssh_user }}@{{ redirector_ip }} echo "SSH connection test"
register: ssh_test
retries: 5
delay: 20
until: ssh_test.rc == 0
ignore_errors: yes
- name: Display SSH connection status
debug:
msg: "{{ 'SSH connection successful' if ssh_test.rc == 0 else 'SSH connection failed - check key permissions' }}"
- name: Add redirector to inventory
add_host:
name: "redirector"