From 86841c9313acccfe416a5fb2cbb9f011698afa83 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Thu, 28 Sep 2023 21:49:54 -0400 Subject: [PATCH] Deploy app in 2 regions --- playbooks/webapp/files/run_app.yaml | 3 + .../tasks/create_aurora_db_cluster.yaml | 31 ++- .../webapp/tasks/create_aurora_setup.yaml | 2 +- playbooks/webapp/tasks/delete.yaml | 16 +- .../tasks/delete_aurora_db_cluster.yaml | 21 ++- playbooks/webapp/vars/main.yaml | 18 +- playbooks/webapp/webapp_ha_aurora.yaml | 178 +++++++++--------- roles/deploy_flask_app/files/run_app.yaml | 6 + roles/deploy_flask_app/meta/main.yaml | 1 + .../deploy_flask_app/tasks/bastion_setup.yaml | 6 + roles/deploy_flask_app/tasks/main.yaml | 5 + roles/deploy_flask_app/templates/vars.yaml.j2 | 1 + 12 files changed, 162 insertions(+), 126 deletions(-) diff --git a/playbooks/webapp/files/run_app.yaml b/playbooks/webapp/files/run_app.yaml index 080d3d26..62033421 100644 --- a/playbooks/webapp/files/run_app.yaml +++ b/playbooks/webapp/files/run_app.yaml @@ -24,6 +24,9 @@ ansible.builtin.yum: name: - podman + sslverify: false + validate_certs: false + update_cache: true state: present - name: Pull image from private registry diff --git a/playbooks/webapp/tasks/create_aurora_db_cluster.yaml b/playbooks/webapp/tasks/create_aurora_db_cluster.yaml index 37968854..45a11980 100644 --- a/playbooks/webapp/tasks/create_aurora_db_cluster.yaml +++ b/playbooks/webapp/tasks/create_aurora_db_cluster.yaml @@ -16,8 +16,8 @@ - name: Create rds global database amazon.cloud.rds_global_cluster: global_cluster_identifier: "{{ test_global_cluster_name }}" - engine: "aurora-postgresql" - engine_version: "15.2" + engine: "{{ rds_engine }}" + engine_version: "{{ rds_engine_version }}" region: "{{ test_primary_cluster_region }}" state: present register: create_global_result @@ -26,11 +26,13 @@ amazon.aws.rds_cluster: global_cluster_identifier: "{{ test_global_cluster_name }}" db_cluster_identifier: "{{ test_primary_cluster_name }}" + db_name: "{{ rds_instance_name }}" region: "{{ test_primary_cluster_region }}" - engine: "aurora-postgresql" - engine_version: "15.2" - username: "{{ deploy_flask_app_rds_master_username }}" - password: "{{ deploy_flask_app_rds_master_password }}" + engine: "{{ rds_engine }}" + engine_version: "{{ rds_engine_version }}" + #enable_global_write_forwarding: True + master_user_password: "{{ deploy_flask_app_rds_master_password }}" + master_username: "{{ deploy_flask_app_rds_master_username }}" db_subnet_group_name: "{{ rds_subnet_group_name }}" vpc_security_group_ids: - "{{ rds_primary_sg.security_groups[0].group_id }}" @@ -40,11 +42,9 @@ amazon.aws.rds_instance: db_cluster_identifier: "{{ test_primary_cluster_name }}" db_instance_identifier: "{{ test_primary_cluster_name }}-instance" - db_name: "{{ rds_instance_name }}" region: "{{ test_primary_cluster_region }}" - engine: "aurora-postgresql" - db_instance_class: "{{ test_instance_class }}" - monitoring_interval: 0 + engine: "{{ rds_engine }}" + db_instance_class: "db.r6g.large" skip_final_snapshot: true - name: Get primary instance info @@ -84,9 +84,10 @@ amazon.aws.rds_cluster: global_cluster_identifier: "{{ test_global_cluster_name }}" db_cluster_identifier: "{{ test_replica_cluster_name }}" - engine: "aurora-postgresql" - engine_version: "{{ global_cluster_info.GlobalClusters[0].EngineVersion }}" # replica cluster engine version needs to be exact same as global db engine version db_subnet_group_name: "{{ rds_subnet_group_name }}" + engine: "{{ rds_engine }}" + engine_version: "{{ global_cluster_info.GlobalClusters[0].EngineVersion }}" # replica cluster engine version needs to be exact same as global db engine version + #enable_global_write_forwarding: True vpc_security_group_ids: - "{{ rds_replica_sg.security_groups[0].group_id }}" region: "{{ test_replica_cluster_region }}" @@ -96,11 +97,9 @@ amazon.aws.rds_instance: db_cluster_identifier: "{{ test_replica_cluster_name }}" db_instance_identifier: "{{ test_replica_cluster_name }}-instance" - db_name: "{{ rds_instance_name }}" region: "{{ test_replica_cluster_region }}" - engine: "aurora-postgresql" - db_instance_class: "{{ test_instance_class }}" - monitoring_interval: 0 + engine: "{{ rds_engine }}" + db_instance_class: "db.r6g.large" skip_final_snapshot: true - name: Get replica instance info diff --git a/playbooks/webapp/tasks/create_aurora_setup.yaml b/playbooks/webapp/tasks/create_aurora_setup.yaml index d816d4ed..8c49c6aa 100644 --- a/playbooks/webapp/tasks/create_aurora_setup.yaml +++ b/playbooks/webapp/tasks/create_aurora_setup.yaml @@ -149,7 +149,7 @@ - name: Set 'sshkey_file' variable ansible.builtin.set_fact: - sshkey_file: ~/private-key-{{ deploy_flask_app_sshkey_pair_name }}-{{ region | default(aws_region) }} + sshkey_file: ~/private-key-{{ deploy_flask_app_sshkey_pair_name }}-{{ region }} - name: Create key pair to connect to the VM amazon.aws.ec2_key: diff --git a/playbooks/webapp/tasks/delete.yaml b/playbooks/webapp/tasks/delete.yaml index 57a07e0d..999707d0 100644 --- a/playbooks/webapp/tasks/delete.yaml +++ b/playbooks/webapp/tasks/delete.yaml @@ -1,10 +1,13 @@ --- - name: Delete resources playbook + module_defaults: + group/aws: + aws_access_key: "{{ aws_access_key | default(omit) }}" + aws_secret_key: "{{ aws_secret_key | default(omit) }}" + security_token: "{{ security_token | default(omit) }}" + region: "{{ region }}" block: - - name: Set 'region' variable - ansible.builtin.set_fact: - region: "{{ region | default(aws_region) }}" - name: Get vpc information amazon.aws.ec2_vpc_net_info: @@ -25,6 +28,7 @@ instance-type: "{{ bastion_host_type }}" key-name: "{{ deploy_flask_app_sshkey_pair_name }}" vpc-id: "{{ vpc_id }}" + instance-state-name: running register: bastion - name: Delete EC2 instances with dependant Resources @@ -81,6 +85,7 @@ amazon.aws.rds_subnet_group: name: "{{ rds_subnet_group_name }}" state: absent + ignore_errors: true - name: List Security group from VPC amazon.aws.ec2_security_group_info: @@ -94,6 +99,7 @@ state: absent group_id: "{{ item }}" with_items: "{{ secgroups.security_groups | map(attribute='group_id') | list }}" + ignore_errors: true - name: List routes table from VPC amazon.aws.ec2_vpc_route_table_info: @@ -109,6 +115,7 @@ lookup: id state: absent with_items: "{{ route_table.route_tables | map(attribute='id') | list }}" + ignore_errors: true - name: Get NAT gateway amazon.aws.ec2_vpc_nat_gateway_info: @@ -122,11 +129,13 @@ state: absent wait: true with_items: "{{ nat_gw.result | map(attribute='nat_gateway_id') | list }}" + ignore_errors: true - name: Delete internet gateway amazon.aws.ec2_vpc_igw: vpc_id: "{{ vpc_id }}" state: absent + ignore_errors: true - name: Delete subnets amazon.aws.ec2_vpc_subnet: @@ -134,6 +143,7 @@ state: absent vpc_id: "{{ vpc_id }}" with_items: "{{ subnet_cidr }}" + ignore_errors: true # As ec2_vpc_route_table can't delete route table, the vpc still has dependencies and cannot be deleted. # You need to do it delete it manually using either the console or the cli. diff --git a/playbooks/webapp/tasks/delete_aurora_db_cluster.yaml b/playbooks/webapp/tasks/delete_aurora_db_cluster.yaml index 1f7e494f..21394306 100644 --- a/playbooks/webapp/tasks/delete_aurora_db_cluster.yaml +++ b/playbooks/webapp/tasks/delete_aurora_db_cluster.yaml @@ -10,18 +10,20 @@ amazon.aws.rds_instance: db_cluster_identifier: "{{ test_replica_cluster_name }}" db_instance_identifier: "{{ test_replica_cluster_name }}-instance" - engine: "aurora-postgresql" + engine: "{{ rds_engine }}" + engine_version: "{{ rds_engine_version }}" db_instance_class: "{{ test_instance_class }}" skip_final_snapshot: true region: "{{ test_replica_cluster_region }}" + wait: True state: absent - name: Delete replica cluster amazon.aws.rds_cluster: db_cluster_identifier: "{{ test_replica_cluster_name }}" global_cluster_identifier: "{{ test_global_cluster_name }}" - engine: "aurora-postgresql" - engine_version: "15.2" + engine: "{{ rds_engine }}" + engine_version: "{{ rds_engine_version }}" skip_final_snapshot: true remove_from_global_db: true region: "{{ test_replica_cluster_region }}" @@ -31,7 +33,8 @@ amazon.aws.rds_instance: db_cluster_identifier: "{{ test_primary_cluster_name }}" db_instance_identifier: "{{ test_primary_cluster_name }}-instance" - engine: "aurora-postgresql" + engine: "{{ rds_engine }}" + engine_version: "{{ rds_engine_version }}" db_instance_class: "{{ test_instance_class }}" skip_final_snapshot: true region: "{{ test_primary_cluster_region }}" @@ -41,19 +44,19 @@ amazon.aws.rds_cluster: db_cluster_identifier: "{{ test_primary_cluster_name }}" global_cluster_identifier: "{{ test_global_cluster_name }}" - engine: "aurora-postgresql" - engine_version: "15.2" + engine: "{{ rds_engine }}" + engine_version: "{{ rds_engine_version }}" username: "{{ deploy_flask_app_rds_master_username }}" password: "{{ deploy_flask_app_rds_master_password }}" skip_final_snapshot: true - region: "{{ test_replica_cluster_region }}" + region: "{{ test_primary_cluster_region }}" state: absent - name: Delete global db amazon.cloud.rds_global_cluster: global_cluster_identifier: "{{ test_global_cluster_name }}" - engine: "aurora-postgresql" - engine_version: "15.2" + engine: "{{ rds_engine }}" + engine_version: "{{ rds_engine_version }}" region: "{{ test_primary_cluster_region }}" state: absent diff --git a/playbooks/webapp/vars/main.yaml b/playbooks/webapp/vars/main.yaml index 143d2531..d4c84411 100644 --- a/playbooks/webapp/vars/main.yaml +++ b/playbooks/webapp/vars/main.yaml @@ -1,6 +1,6 @@ --- # Variables for create.yaml -aws_region: us-east-1 +# aws_region: us-west-2 dest_region: us-east-2 resource_prefix: "ansible-test" vpc_name: "{{ resource_prefix }}-vpc" @@ -13,7 +13,10 @@ resource_tags: prefix: "{{ resource_prefix }}" operation: create -image_filter: Fedora-Cloud-Base-35-* +# image_filter: Fedora-Cloud-Base-37-* +# image_filter: RHEL-8.7.0_HVM-20221101-x86_64-0-Hourly2-GP2 +# image_filter: RHEL_HA-9.0.0_HVM-20230822-x86_64-17-Hourly2-GP2 - podman 503 error +image_filter: RHEL_HA-9.2.0_HVM-20230905-x86_64-38-Hourly2-GP2 public_secgroup_name: "{{ resource_prefix }}-sg" rds_subnet_group_name: "{{ resource_prefix }}-rds-sg" rds_secgroup_name: "{{ resource_prefix }}-rds-sec" @@ -30,17 +33,17 @@ rds_listening_port: 5432 # Variables for the deploy_flask_app role deploy_flask_app_sshkey_pair_name: "{{ resource_prefix }}-key" deploy_flask_app_bastion_host_name: "{{ resource_prefix }}-bastion" -deploy_flask_app_bastion_host_username: fedora +deploy_flask_app_bastion_host_username: ec2-user deploy_flask_app_bastion_host_required_packages: - python3 - - python-virtualenv - sshpass - git + - gcc - podman - httpd-tools - - ansible + - ansible-core deploy_flask_app_workers_instance_type: t3.micro -deploy_flask_app_workers_user_name: fedora +deploy_flask_app_workers_user_name: ec2-user deploy_flask_app_number_of_workers: 2 deploy_flask_app_listening_port: 5000 deploy_flask_app_git_repository: https://github.com/abikouo/webapp_pyflask_demo.git @@ -60,6 +63,7 @@ deploy_flask_app_rds_master_username: ansible test_instance_class: db.r5.large # Global cluster parameters ================================ test_global_cluster_name: "{{ resource_prefix }}-global-cluster" +aurora_listening_port: 3306 # Primary cluster parameters ================================ test_primary_cluster_name: "{{ resource_prefix }}-primary-cluster" @@ -68,5 +72,5 @@ test_primary_cluster_instance_name: "{{ resource_prefix }}-primary-instance" # Replica cluster parameters ================================ test_replica_cluster_name: "{{ resource_prefix }}-replica-cluster" -test_replica_cluster_region: eu-north-1 +test_replica_cluster_region: us-east-1 test_replica_cluster_instance_name: "{{ resource_prefix }}-replica-instance" diff --git a/playbooks/webapp/webapp_ha_aurora.yaml b/playbooks/webapp/webapp_ha_aurora.yaml index 8aa19a1c..6a5e16b8 100644 --- a/playbooks/webapp/webapp_ha_aurora.yaml +++ b/playbooks/webapp/webapp_ha_aurora.yaml @@ -6,104 +6,102 @@ vars_files: - vars/main.yaml tasks: - - name: Fail when 'resource_prefix' is not defined - ansible.builtin.fail: - msg: resource prefix should be defined as resource_prefix - when: resource_prefix is not defined - - - name: Fail when 'test_replica_cluster_region' is not defined - ansible.builtin.fail: - msg: destination region should be defined as test_replica_cluster_region - when: test_replica_cluster_region is not defined - - - name: Create resources + - name: Create resources and Deploy App when: operation == "create" - ansible.builtin.include_tasks: tasks/create_aurora_setup.yaml - vars: - region: "{{ item }}" - with_items: - - "{{ test_primary_cluster_region }}" - - "{{ test_replica_cluster_region }}" + block: + - name: Create resources in primary region + ansible.builtin.include_tasks: tasks/create_aurora_setup.yaml + vars: + region: "{{ test_primary_cluster_region }}" + + - name: Create resources in replica region + ansible.builtin.include_tasks: tasks/create_aurora_setup.yaml + vars: + region: "{{ test_replica_cluster_region }}" + + - name: Create Aurora db cluster + ansible.builtin.import_tasks: tasks/create_aurora_db_cluster.yaml + vars: + rds_engine: "aurora-postgresql" + + # ================= Deploy App in the primary region ================= + - name: Get VPC info from primary region + amazon.aws.ec2_vpc_net_info: + filters: + "tag:Name": "{{ vpc_name }}" + region: "{{ test_primary_cluster_region }}" + register: primary_vpc + + - name: Get primary private subnet for workers + amazon.aws.ec2_vpc_subnet_info: + filters: + vpc-id: "{{ primary_vpc.vpcs[0].id }}" + region: "{{ test_primary_cluster_region }}" + register: primary_private_subnet + + - name: Get VM info in the primary region + amazon.aws.ec2_instance_info: + filters: + "tag:Name": "{{ deploy_flask_app_bastion_host_name }}" + instance-state-name: [ "running"] + region: "{{ test_primary_cluster_region }}" + register: primary_vm_result + + - name: Deploy app in primary region + ansible.builtin.include_role: + name: cloud.aws_ops.deploy_flask_app + vars: + deploy_flask_app_private_subnet_id: "{{ primary_private_subnet.subnets[0].id }}" + deploy_flask_app_vpc_id: "{{ primary_vpc.vpcs[0].id }}" + deploy_flask_app_vm_info: "{{ primary_vm_result }}" + deploy_flask_app_rds_info: "{{ primary_instance_info_result }}" + deploy_flask_app_region: "{{ test_primary_cluster_region }}" + + # ================= Deploy App in the replica region ================= + + - name: Get VPC info from replica region + amazon.aws.ec2_vpc_net_info: + filters: + "tag:Name": "{{ vpc_name }}" + region: "{{ test_replica_cluster_region }}" + register: replica_vpc + + - name: Get VM info in the replica region + amazon.aws.ec2_instance_info: + filters: + "tag:Name": "{{ deploy_flask_app_bastion_host_name }}" + instance-state-name: [ "running"] + region: "{{ test_replica_cluster_region }}" + register: replica_vm_result + + - name: Get replica private subnet for workers + amazon.aws.ec2_vpc_subnet_info: + filters: + vpc-id: "{{ replica_vpc.vpcs[0].id }}" + region: "{{ test_replica_cluster_region }}" + register: replica_private_subnet + + - debug: + msg: "Replica : {{ test_replica_cluster_region }}" + + - name: Deploy app in replica region + ansible.builtin.include_role: + name: cloud.aws_ops.deploy_flask_app + vars: + deploy_flask_app_private_subnet_id: "{{ replica_private_subnet.subnets[0].id }}" + deploy_flask_app_vpc_id: "{{ replica_vpc.vpcs[0].id }}" + deploy_flask_app_vm_info: "{{ replica_vm_result }}" + deploy_flask_app_rds_info: "{{ replica_instance_info_result }}" + deploy_flask_app_region: "{{ test_replica_cluster_region }}" - - name: Create Aurora db cluster - when: operation == "create" - ansible.builtin.import_tasks: tasks/create_aurora_db_cluster.yaml - - # ================= Deploy App in the primary region ================= - - name: Get VPC info from primary region - amazon.aws.ec2_vpc_net_info: - filters: - "tag:Name": "{{ vpc_name }}" - region: "{{ test_primary_cluster_region }}" - register: primary_vpc - - - name: Get primary private subnet for workers - amazon.aws.ec2_vpc_subnet_info: - filters: - vpc-id: "{{ primary_vpc.vpcs[0].id }}" - region: "{{ test_primary_cluster_region }}" - register: primary_private_subnet - - - name: Get VM info in the primary region - amazon.aws.ec2_instance_info: - filters: - "tag:Name": "{{ deploy_flask_app_bastion_host_name }}" - region: "{{ test_primary_cluster_region }}" - register: primary_vm_result - - - debug: - msg: "{{ primary_vm_result.instances }}" - - - name: Deploy app in primary region - when: operation == "create" - ansible.builtin.import_role: - name: cloud.aws_ops.deploy_flask_app - vars: - deploy_flask_app_private_subnet_id: "{{ primary_private_subnet.subnets[0].id }}" - deploy_flask_app_vpc_id: "{{ primary_vpc.vpcs[0].id }}" - deploy_flask_app_vm_info: "{{ primary_vm_result }}" - deploy_flask_app_rds_info: "{{ primary_instance_info_result }}" - deploy_flask_app_region: "{{ test_primary_cluster_region }}" - - # ================= Deploy App in the replica region ================= - - - name: Get VPC info from replica region - amazon.aws.ec2_vpc_net_info: - filters: - "tag:Name": "{{ vpc_name }}" - region: "{{ test_replica_cluster_region }}" - register: replica_vpc - - - name: Get VM info in the replica region - amazon.aws.ec2_instance_info: - filters: - "tag:Name": "{{ deploy_flask_app_bastion_host_name }}" - region: "{{ test_replica_cluster_region }}" - register: replica_vm_result - - - name: Get replica private subnet for workers - amazon.aws.ec2_vpc_subnet_info: - filters: - vpc-id: "{{ replica_vpc.vpcs[0].id }}" - region: "{{ test_replica_cluster_region }}" - register: replica_private_subnet - - - name: Deploy app in replica region - when: operation == "create" - ansible.builtin.import_role: - name: cloud.aws_ops.deploy_flask_app - vars: - deploy_flask_app_private_subnet_id: "{{ replica_private_subnet.subnets[0].id }}" - deploy_flask_app_vpc_id: "{{ replica_vpc.vpcs[0].id }}" - deploy_flask_app_vm_info: "{{ replica_vm_result }}" - deploy_flask_app_rds_info: "{{ replica_instance_info_result }}" - deploy_flask_app_region: "{{ test_replica_cluster_region }}" # ================================================================================ - name: Delete instance from source region when: operation == "delete" ansible.builtin.import_tasks: tasks/delete_aurora_db_cluster.yaml + vars: + rds_engine: "aurora-postgresql" - name: Delete instance from source region when: operation == "delete" diff --git a/roles/deploy_flask_app/files/run_app.yaml b/roles/deploy_flask_app/files/run_app.yaml index 080d3d26..e043660e 100644 --- a/roles/deploy_flask_app/files/run_app.yaml +++ b/roles/deploy_flask_app/files/run_app.yaml @@ -20,10 +20,16 @@ - regex: ^(# *)?ClientAliveCountMax line: ClientAliveCountMax 3 + # - name: Enable EPEL + # ansible.builtin.package: + # name: + # - epel-release + - name: Install Podman ansible.builtin.yum: name: - podman + update_cache: True state: present - name: Pull image from private registry diff --git a/roles/deploy_flask_app/meta/main.yaml b/roles/deploy_flask_app/meta/main.yaml index e8b3ab42..3bf1568b 100644 --- a/roles/deploy_flask_app/meta/main.yaml +++ b/roles/deploy_flask_app/meta/main.yaml @@ -1,3 +1,4 @@ --- dependencies: - role: cloud.aws_ops.aws_setup_credentials +allow_duplicates: true diff --git a/roles/deploy_flask_app/tasks/bastion_setup.yaml b/roles/deploy_flask_app/tasks/bastion_setup.yaml index f13e550c..2bb04d33 100644 --- a/roles/deploy_flask_app/tasks/bastion_setup.yaml +++ b/roles/deploy_flask_app/tasks/bastion_setup.yaml @@ -20,6 +20,12 @@ line: ClientAliveCountMax 3 become: true +# - name: Enable EPEL +# ansible.builtin.package: +# name: +# - epel-release +# become: true + - name: Install required packages ansible.builtin.yum: name: "{{ deploy_flask_app_bastion_host_required_packages }}" diff --git a/roles/deploy_flask_app/tasks/main.yaml b/roles/deploy_flask_app/tasks/main.yaml index 34df2040..4d722167 100644 --- a/roles/deploy_flask_app/tasks/main.yaml +++ b/roles/deploy_flask_app/tasks/main.yaml @@ -1,8 +1,13 @@ --- +- debug: + msg: "Rgion {{ deploy_flask_app_region }}" - name: Deploy flask app. module_defaults: group/aws: "{{ aws_setup_credentials__output }}" block: + - debug: + msg: "Rgion {{ deploy_flask_app_region }}" + - name: Create new host in inventory for use in later plays. ansible.builtin.include_tasks: setup.yaml diff --git a/roles/deploy_flask_app/templates/vars.yaml.j2 b/roles/deploy_flask_app/templates/vars.yaml.j2 index 9f61ae53..bc6a211d 100644 --- a/roles/deploy_flask_app/templates/vars.yaml.j2 +++ b/roles/deploy_flask_app/templates/vars.yaml.j2 @@ -3,6 +3,7 @@ registry_host_port: "{{ deploy_flask_app_setup.add_host.host_vars.host_config.pr registry_login: user: "{{ deploy_flask_app_local_registry_user }}" password: "{{ deploy_flask_app_local_registry_pwd }}" +rds_listening_port: "{{ rds_listening_port }}" application_dir: "{{ deploy_flask_app_config.app_dir }}" application_env: "{{ deploy_flask_app_config.env }}" application_db: