Skip to content

Commit

Permalink
Add module_defaults for aws_ vars to support migration to another region
Browse files Browse the repository at this point in the history
  • Loading branch information
GomathiselviS committed Sep 11, 2023
1 parent f050cf5 commit 7ff0f79
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
35 changes: 11 additions & 24 deletions playbooks/webapp/migrate_webapp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 }}"
Expand All @@ -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 }}"
10 changes: 7 additions & 3 deletions playbooks/webapp/tasks/create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions playbooks/webapp/vars/main.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions roles/deploy_flask_app/tasks/bastion_setup.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7ff0f79

Please sign in to comment.