From 7ff0f79b573e83650451b819a854128f5b4cea21 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Mon, 11 Sep 2023 16:16:03 -0400 Subject: [PATCH] Add module_defaults for aws_ vars to support migration to another region --- playbooks/webapp/migrate_webapp.yaml | 35 ++++++------------- playbooks/webapp/tasks/create.yaml | 10 ++++-- playbooks/webapp/vars/main.yaml | 1 + .../deploy_flask_app/tasks/bastion_setup.yaml | 6 ++++ .../test_deploy_flask_app/tasks/create.yaml | 3 +- 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/playbooks/webapp/migrate_webapp.yaml b/playbooks/webapp/migrate_webapp.yaml index 527bdf8b..016bc232 100644 --- a/playbooks/webapp/migrate_webapp.yaml +++ b/playbooks/webapp/migrate_webapp.yaml @@ -6,12 +6,6 @@ vars_files: - vars/main.yaml - 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) }}" - tasks: - name: Fail when 'resource_prefix' is not defined ansible.builtin.fail: @@ -60,6 +54,17 @@ rds_snapshot_arn: "{{ result.db_snapshot_arn }}" region: "{{ dest_region }}" + - name: Deploy app + when: operation == "create" + ansible.builtin.import_role: + name: cloud.aws_ops.deploy_flask_app + vars: + deploy_flask_app_private_subnet_id: "{{ private_subnet.subnet.id }}" + deploy_flask_app_vpc_id: "{{ vpc.vpc.id }}" + deploy_flask_app_vm_info: "{{ vm_result }}" + deploy_flask_app_rds_info: "{{ rds_result }}" + deploy_flask_app_region: "{{ dest_region }}" + - name: Delete RDS snapshots from different regions amazon.aws.rds_instance_snapshot: region: "{{ item }}" @@ -73,21 +78,3 @@ - name: Delete instance from source region ansible.builtin.import_tasks: tasks/delete.yaml when: delete_source | default(false) | bool - -- name: Deploy resource from Bastion - hosts: bastion - gather_facts: false - - vars_files: - - vars/main.yaml - - tasks: - - name: Deploy app - ansible.builtin.import_role: - name: cloud.aws_ops.deploy_flask_app - vars: - deploy_flask_app_private_subnet_id: "{{ private_subnet.subnet.id }}" - deploy_flask_app_vpc_id: "{{ vpc.vpc.id }}" - deploy_flask_app_vm_info: "{{ vm_result }}" - deploy_flask_app_rds_info: "{{ rds_result }}" - deploy_flask_app_region: "{{ dest_region }}" diff --git a/playbooks/webapp/tasks/create.yaml b/playbooks/webapp/tasks/create.yaml index 33e3ee28..0a24ae62 100644 --- a/playbooks/webapp/tasks/create.yaml +++ b/playbooks/webapp/tasks/create.yaml @@ -4,7 +4,12 @@ region: "{{ region | default(aws_region) }}" - name: Create 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: Get image ID to create an instance amazon.aws.ec2_ami_info: @@ -17,8 +22,7 @@ - name: List availability zones from aws region amazon.aws.aws_az_info: - filters: - region-name: "{{ region }}" + region: "{{ region }}" register: zones - name: Set region_av_zones' variable diff --git a/playbooks/webapp/vars/main.yaml b/playbooks/webapp/vars/main.yaml index 57920fd2..8a2ff513 100644 --- a/playbooks/webapp/vars/main.yaml +++ b/playbooks/webapp/vars/main.yaml @@ -1,6 +1,7 @@ --- # Variables for create.yaml aws_region: us-east-1 +dest_region: us-east-2 resource_prefix: "ansible-demo-test" vpc_name: "{{ resource_prefix }}-vpc" vpc_cidr: 10.1.0.0/16 diff --git a/roles/deploy_flask_app/tasks/bastion_setup.yaml b/roles/deploy_flask_app/tasks/bastion_setup.yaml index 0ceda6d2..f13e550c 100644 --- a/roles/deploy_flask_app/tasks/bastion_setup.yaml +++ b/roles/deploy_flask_app/tasks/bastion_setup.yaml @@ -1,6 +1,12 @@ --- - name: Deploy resource from Bastion delegate_to: bastion + 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: "{{ deploy_flask_app_region | default(aws_region) }}" block: - name: Update ssh_config ansible.builtin.lineinfile: diff --git a/tests/integration/targets/test_deploy_flask_app/tasks/create.yaml b/tests/integration/targets/test_deploy_flask_app/tasks/create.yaml index 298ed7d1..b03c53e1 100644 --- a/tests/integration/targets/test_deploy_flask_app/tasks/create.yaml +++ b/tests/integration/targets/test_deploy_flask_app/tasks/create.yaml @@ -16,8 +16,7 @@ - name: List availability zones from aws region amazon.aws.aws_az_info: - filters: - region-name: "{{ region }}" + region: "{{ region }}" register: zones - name: Set region_av_zones' variable