From 125e905408f225a98b8322185ba4451f9d385171 Mon Sep 17 00:00:00 2001 From: Sagar Gupta <92421383+Sagargupta16@users.noreply.github.com> Date: Thu, 20 Jul 2023 07:36:15 +0530 Subject: [PATCH] Update aws.yml --- playbooks/providers/aws.yml | 66 ++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/playbooks/providers/aws.yml b/playbooks/providers/aws.yml index 856a90c..eb924e9 100644 --- a/playbooks/providers/aws.yml +++ b/playbooks/providers/aws.yml @@ -37,6 +37,7 @@ butane_input_template: "../templates/forem.yml.j2" butane_aws_s3: true butane_aws_s3_url: "https://forem-selfhost-{{ app_domain |replace('.', '-') }}-ign.s3.{{ fcos_aws_region }}.amazonaws.com/forem.ign" + register: butane_ignition_stdout - amazon.aws.ec2_vpc_net_info: filters: @@ -48,6 +49,7 @@ - name: Set forem_vpc_id fact ansible.builtin.set_fact: forem_vpc_id: "{{ forem_vpc_info['vpcs'][0]['vpc_id'] }}" + register: forem_vpc_id_fact - name: Gather info about VPC subnets amazon.aws.ec2_vpc_subnet_info: @@ -75,9 +77,10 @@ - name: "Generate list of route tables for {{ forem_vpc_id }}" set_fact: forem_vpcd_route_table_ids: "{{ forem_vpc_route_table.route_tables|map(attribute='id')|list }}" + register: forem_vpcd_route_table_ids_fact - name: "Create S3 VPC endpoint in {{ forem_vpc_id }}" - community.aws.ec2_vpc_endpoint: + amazon.aws.ec2_vpc_endpoint: state: present region: "{{ fcos_aws_region }}" vpc_id: "{{ forem_vpc_id }}" @@ -89,6 +92,7 @@ - name: Set forem_vpc_s3_endpoint_id fact set_fact: forem_vpc_s3_endpoint_id: "{{ forem_vpc_s3_endpoint.result.vpc_endpoint_id }}" + register: forem_vpc_s3_endpoint_id_fact - name: Wait for S3 VPC Endpoint pause: @@ -120,16 +124,17 @@ } ] } + register: forem_ign_bucket - name: "Upload butane_ignition_stdout to forem-selfhost-{{ app_domain |replace('.', '-') }}-ign" - amazon.aws.aws_s3: + amazon.aws.s3_object: bucket: "forem-selfhost-{{ app_domain |replace('.', '-') }}-ign" - object: "/forem.ign" + object: "forem.ign" content: "{{ butane_ignition_stdout | to_json | string }}" mode: put region: "{{ fcos_aws_region }}" profile: "{{ fcos_aws_profile }}" - register: forem_ign_s3 + register: forem_ign_s3 - name: Create Forem SSH key amazon.aws.ec2_key: @@ -137,6 +142,7 @@ key_material: "{{ ssh_key }}" profile: "{{ fcos_aws_profile }}" region: "{{ fcos_aws_region }}" + register: forem_ssh_key - name: "Create Forem security group for {{ app_domain }}" amazon.aws.ec2_group: @@ -168,30 +174,28 @@ register: forem_security_group - name: "Launch Forem instance for {{ app_domain }}" - amazon.aws.ec2: + amazon.aws.ec2_instance: key_name: "forem-{{ app_domain }}" region: "{{ fcos_aws_region }}" profile: "{{ fcos_aws_profile }}" - group: "forem-{{ app_domain }}" + security_groups: forem-{{ app_domain }} instance_type: "{{ fcos_aws_size }}" - image: "{{ fcos_aws_image }}" + image_id: "{{ fcos_aws_image }}" wait: yes wait_timeout: 500 vpc_subnet_id: "{{ forem_subnet_info.subnets | map(attribute='id') | list | first }}" volumes: - device_name: /dev/xvda - volume_type: gp2 - volume_size: "{{ fcos_aws_ebs_size }}" - encrypted: yes - delete_on_termination: no - monitoring: yes - assign_public_ip: yes + ebs: + volume_type: gp2 + volume_size: "{{ fcos_aws_ebs_size }}" + delete_on_termination: no + detailed_monitoring: yes + network: + assign_public_ip: yes + groups: forem-{{ app_domain }} user_data: "{{ butane_boot_ignition_stdout | to_json | string }}" - instance_tags: - App: "forem" - Domain: "{{ app_domain }}" - Name: "forem-{{ app_domain }}" - count_tag: + tags: App: "forem" Domain: "{{ app_domain }}" Name: "forem-{{ app_domain }}" @@ -201,23 +205,39 @@ - name: Wait 300 seconds for port 22 to become open wait_for: port: 22 - host: "{{ forem_ec2_instance.tagged_instances | map(attribute='public_ip') | list | first }}" + host: "{{ forem_ec2_instance.instances[0].public_ip_address }}" delay: 10 connection: local + - name: Associate Elastic IP with Forem instance + amazon.aws.ec2_eip: + device_id: "{{ forem_ec2_instance.instances[0].instance_id }}" + region: "{{ fcos_aws_region }}" + profile: "{{ fcos_aws_profile }}" + in_vpc: yes + reuse_existing_ip_allowed: yes + register: forem_ec2_eip + + - name: Wait 30 seconds for Elastic IP to be associated + pause: + seconds: 30 + - name: "Delete object forem-selfhost-{{ app_domain |replace('.', '-') }}-ign/forem.ign from S3" amazon.aws.aws_s3: bucket: "forem-selfhost-{{ app_domain |replace('.', '-') }}-ign" - object: "/forem.ign" + object: "forem.ign" mode: delobj region: "{{ fcos_aws_region }}" profile: "{{ fcos_aws_profile }}" + register: forem_ign_s3_delete - name: Output EC2 setup message ansible.builtin.debug: msg: - - "The public IPv4 IP Address for {{ app_domain }} is {{ forem_ec2_instance.tagged_instances | map(attribute='public_ip') | list | first }}" - - "Please add an A entry for {{ app_domain }} that points to {{ forem_ec2_instance.tagged_instances | map(attribute='public_ip') | list | first }}" + - "The public IPv4 IP Address for {{ app_domain }} is {{ forem_ec2_eip.public_ip }}" + - "Please add an A entry for {{ app_domain }} that points to {{ forem_ec2_eip.public_ip }}" - "Example:" - - " {{ app_domain }} IN A {{ forem_ec2_instance.tagged_instances | map(attribute='public_ip') | list | first }}" + - " {{ app_domain }} IN A {{ forem_ec2_eip.public_ip }}" - "Once you have DNS resolving to this EC2 instance please read the Forem Admin Docs: https://admin.forem.com/" + register: forem_ec2_instance_setup_message +