Skip to content

Commit 868f1d4

Browse files
Merge pull request #85 from GomathiselviS/webapp_role
Add role deploy_flask_app
2 parents d7f1a95 + 7532ad9 commit 868f1d4

25 files changed

+1191
-363
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Name | Description
2828
[cloud.aws_ops.manage_vpc_peering](https://github.com/ansible-collections/cloud.aws_ops/blob/main/roles/manage_vpc_peering/README.md)|A role to create, delete and accept existing VPC peering connections.
2929
[cloud.aws_ops.moving_objects_between_buckets](https://github.com/ansible-collections/cloud.aws_ops/blob/main/roles/moving_objects_between_buckets/README.md)|A role to move objects from one bucket to another bucket.
3030
[cloud.aws_ops.awsconfig_apigateway_with_lambda_integration](https://github.com/ansible-collections/cloud.aws_ops/blob/main/roles/awsconfig_apigateway_with_lambda_integration/README.md)|A role to create/delete an API gateway with lambda function integration.
31+
[cloud.aws_ops.manage_transit_gateway](https://github.com/ansible-collections/cloud.aws_ops/blob/main/roles/manage_transit_gateway/README.md)|A role to create/delete transit_gateway with vpc and vpn attachments.
32+
[cloud.aws_ops.deploy_flask_app](https://github.com/ansible-collections/cloud.aws_ops/blob/main/roles/deploy_flask_app/README.md)|A role to deploy a flask web application on AWS.
3133

3234
### Playbooks
3335
Name | Description
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
breaking_changes:
3+
- "Rename the vars in webapp playbook with role name prefix (https://github.com/redhat-cop/cloud.aws_ops/pull/85).
4+
'sshkey_pair_name' changed to 'deploy_flask_app_sshkey_pair_name'
5+
'bastion_host_name' changed to 'deploy_flask_app_bastion_host_name'
6+
'bastion_host_username' changed to 'deploy_flask_app_bastion_host_username'
7+
'bastion_host_required_packages' changed to 'deploy_flask_app_bastion_host_required_packages'
8+
'app_listening_port' changed to 'deploy_flask_app_listening_port'
9+
'rds_master_user' changed to 'deploy_flask_app_rds_master_username'
10+
'rds_master_password' changed to 'deploy_flask_app_rds_master_password'
11+
'app_git_repository' changed to 'deploy_flask_app_git_repository'
12+
'number_of_workers' changed to 'deploy_flask_app_number_of_workers'
13+
'workers_instance_type' changed to 'deploy_flask_app_workers_instance_type'
14+
'local_registry_user' changed to 'deploy_flask_app_local_registry_user'
15+
'local_registry_pwd' changed to 'deploy_flask_app_local_registry_pwd'
16+
'local_registry_port' changed to 'deploy_flask_app_local_registry_port'
17+
'app_config' changed to 'deploy_flask_app_config'
18+
'app_force_init' changed to 'deploy_flask_app_force_init'
19+
"
20+
minor_changes:
21+
- "Convert deploy_flask_app playbook to role (https://github.com/redhat-cop/cloud.aws_ops/pull/85)."

playbooks/webapp/README.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ To delete the webapp:
9797
### EC2 instance
9898

9999
* **image_filter** (str): Name of AWS AMI to use. Default: `Fedora-Cloud-Base-35-*`
100-
* **sshkey_pair_name** (str): Name for the EC2 key pair. Default: `"{{ resource_prefix }}-key"`
101-
* **bastion_host_name** (str): Name for the EC2 instance. Default: `"{{ resource_prefix }}-bastion"`
100+
* **deploy_flask_app_sshkey_pair_name** (str): Name for the EC2 key pair. Default: `"{{ resource_prefix }}-key"`
101+
* **deploy_flask_app_bastion_host_name** (str): Name for the EC2 instance. Default: `"{{ resource_prefix }}-bastion"`
102102
* **bastion_host_type** (str): Instance type for the EC2 instance. Default: `t2.xlarge`
103-
* **bastion_host_username** (str): Username for the bastion host SSH user. Default: `fedora`
104-
* **bastion_host_required_packages** (list, elements str): Packages to be installed on the bastion host. Default:
103+
* **deploy_flask_app_bastion_host_username** (str): Username for the bastion host SSH user. Default: `fedora`
104+
* **deploy_flask_app_bastion_host_required_packages** (list, elements str): Packages to be installed on the bastion host. Default:
105105
```yaml
106106
- python3
107107
- python-virtualenv
@@ -125,7 +125,7 @@ To delete the webapp:
125125
* **rds_subnet_group_name** (str): Subnet group name for the RDS instance. Default: `"{{ resource_prefix }}-rds-sg"`
126126
* **rds_secgroup_name** (str): Security group name for the RDS instance. Default: `"{{ resource_prefix }}-rds-sec"`
127127
* **public_secgroup_name** (str): Security group name for the bastion host. Default: `"{{ resource_prefix }}-sg"`
128-
* **app_listening_port** (int): Connection listening port for the app on the bastion host. Default: `5000`
128+
* **deploy_flask_app_listening_port** (int): Connection listening port for the app on the bastion host. Default: `5000`
129129
* **rds_listening_port** (int): Connection listening port for the RDS instance. Default: `5432`
130130

131131
### RDS instance
@@ -137,25 +137,25 @@ To delete the webapp:
137137
* **rds_instance_name** (str): Name for the database. Default: `mysampledb123`
138138
* **rds_engine** (str): Engine to use for the database. Default: `postgres`
139139
* **rds_engine_version** (str): Version number of the database engine to use. Default: `"14.8"`
140-
* **rds_master_user** (str): Name of the master user for the database instance. Default: `ansible`
141-
* **rds_master_password** (str): Password for the master database user. Default: `L#5cH2mgy_`
140+
* **deploy_flask_app_rds_master_username** (str): Name of the master user for the database instance. Default: `ansible`
141+
* **deploy_flask_app_rds_master_password** (str): Password for the master database user. Default: `L#5cH2mgy_`
142142

143143
### Webapp
144144

145-
* **app_git_repository** (str): Git repository for the webapp. Default: `https://github.com/abikouo/webapp_pyflask_demo.git`
146-
* **number_of_workers** (int): Number of worker instances to create. Default: `2`
147-
* **workers_instance_type** (str): EC2 instance type for workers. Default: `t2.large`
148-
* **local_registry_user** (str): Username for local Podman registry. Default: `ansible`
149-
* **local_registry_pwd** (str): Password for local Podman registry. Default: `testing123`
150-
* **local_registry_port** (int): Port for the local Podman registery. Default: `"{{ app_listening_port }}"`
151-
* **app_config** (dict, elements dict): Configuration values for the webapp, passed as corresponding env variables FLASK_APP, FLASK_ENV, ADMIN_USER, and ADMIN_PASSWORD when the app is deployed. Default:
145+
* **deploy_flask_app_git_repository** (str): Git repository for the webapp. Default: `https://github.com/abikouo/webapp_pyflask_demo.git`
146+
* **deploy_flask_app_number_of_workers** (int): Number of worker instances to create. Default: `2`
147+
* **deploy_flask_app_workers_instance_type** (str): EC2 instance type for workers. Default: `t2.xlarge`
148+
* **deploy_flask_app_local_registry_user** (str): Username for local Podman registry. Default: `ansible`
149+
* **deploy_flask_app_local_registry_pwd** (str): Password for local Podman registry. Default: `testing123`
150+
* **deploy_flask_app_local_registry_port** (int): Port for the local Podman registery. Default: `"{{ app_listening_port }}"`
151+
* **deploy_flask_app_config** (dict, elements dict): Configuration values for the webapp, passed as corresponding env variables FLASK_APP, FLASK_ENV, ADMIN_USER, and ADMIN_PASSWORD when the app is deployed. Default:
152152
```yaml
153153
app_dir: /app/pyapp
154154
env: development
155155
admin_user: admin
156156
admin_password: admin
157157
```
158-
* **app_force_init** (bool): Whether to drop existing tables and create new ones when deploying the webapp database. Default: `false`
158+
* **deploy_flask_app_force_init** (bool): Whether to drop existing tables and create new ones when deploying the webapp database. Default: `false`
159159

160160
## Example Usage
161161

playbooks/webapp/migrate_webapp.yaml

+11-19
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
vars_files:
77
- vars/main.yaml
88

9-
module_defaults:
10-
group/aws:
11-
aws_access_key: "{{ aws_access_key | default(omit) }}"
12-
aws_secret_key: "{{ aws_secret_key | default(omit) }}"
13-
security_token: "{{ security_token | default(omit) }}"
14-
159
tasks:
1610
- name: Fail when 'resource_prefix' is not defined
1711
ansible.builtin.fail:
@@ -60,6 +54,17 @@
6054
rds_snapshot_arn: "{{ result.db_snapshot_arn }}"
6155
region: "{{ dest_region }}"
6256

57+
- name: Deploy app
58+
when: operation == "create"
59+
ansible.builtin.import_role:
60+
name: cloud.aws_ops.deploy_flask_app
61+
vars:
62+
deploy_flask_app_private_subnet_id: "{{ private_subnet.subnet.id }}"
63+
deploy_flask_app_vpc_id: "{{ vpc.vpc.id }}"
64+
deploy_flask_app_vm_info: "{{ vm_result }}"
65+
deploy_flask_app_rds_info: "{{ rds_result }}"
66+
deploy_flask_app_region: "{{ dest_region }}"
67+
6368
- name: Delete RDS snapshots from different regions
6469
amazon.aws.rds_instance_snapshot:
6570
region: "{{ item }}"
@@ -73,16 +78,3 @@
7378
- name: Delete instance from source region
7479
ansible.builtin.import_tasks: tasks/delete.yaml
7580
when: delete_source | default(false) | bool
76-
77-
- name: Deploy resource from Bastion
78-
hosts: bastion
79-
gather_facts: false
80-
81-
vars_files:
82-
- vars/main.yaml
83-
84-
tasks:
85-
- name: Deploy app
86-
ansible.builtin.import_tasks: tasks/deploy_app.yaml
87-
vars:
88-
region: "{{ dest_region }}"

playbooks/webapp/tasks/create.yaml

+14-37
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
aws_secret_key: "{{ aws_secret_key | default(omit) }}"
1111
security_token: "{{ security_token | default(omit) }}"
1212
region: "{{ region }}"
13-
1413
block:
1514
- name: Get image ID to create an instance
1615
amazon.aws.ec2_ami_info:
@@ -23,8 +22,6 @@
2322

2423
- name: List availability zones from aws region
2524
amazon.aws.aws_az_info:
26-
filters:
27-
region-name: "{{ region }}"
2825
register: zones
2926

3027
- name: Set region_av_zones' variable
@@ -127,8 +124,8 @@
127124
to_port: 22
128125
- cidr_ip: 0.0.0.0/0
129126
proto: tcp
130-
from_port: "{{ app_listening_port }}"
131-
to_port: "{{ app_listening_port }}"
127+
from_port: "{{ deploy_flask_app_listening_port }}"
128+
to_port: "{{ deploy_flask_app_listening_port }}"
132129
rules_egress:
133130
- cidr_ip: 0.0.0.0/0
134131
proto: -1
@@ -153,10 +150,10 @@
153150
- name: Get RDS instance info
154151
amazon.aws.rds_instance_info:
155152
db_instance_identifier: "{{ rds_identifier }}"
156-
register: rds_info
153+
register: rds_result
157154

158155
- name: Create RDS instance
159-
when: rds_info.instances | length == 0
156+
when: rds_result.instances | length == 0
160157
block:
161158
- name: Create RDS instance (PostGreSQL Database)
162159
amazon.aws.rds_instance:
@@ -169,8 +166,8 @@
169166
db_name: "{{ rds_instance_name }}"
170167
engine: "{{ rds_engine }}"
171168
engine_version: "{{ rds_engine_version }}"
172-
master_user_password: "{{ rds_master_password }}"
173-
master_username: "{{ rds_master_user }}"
169+
master_user_password: "{{ deploy_flask_app_rds_master_password }}"
170+
master_username: "{{ deploy_flask_app_rds_master_username }}"
174171
monitoring_interval: 0
175172
storage_type: standard
176173
skip_final_snapshot: true
@@ -189,8 +186,8 @@
189186
db_instance_identifier: "{{ rds_identifier }}"
190187
engine: "{{ rds_engine }}"
191188
engine_version: "{{ rds_engine_version }}"
192-
master_user_password: "{{ rds_master_password }}"
193-
master_username: "{{ rds_master_user }}"
189+
master_user_password: "{{ deploy_flask_app_rds_master_password }}"
190+
master_username: "{{ deploy_flask_app_rds_master_username }}"
194191
monitoring_interval: 0
195192
storage_type: standard
196193
skip_final_snapshot: true
@@ -204,15 +201,15 @@
204201
- name: Get RDS instance info
205202
amazon.aws.rds_instance_info:
206203
db_instance_identifier: "{{ rds_identifier }}"
207-
register: rds_info
204+
register: rds_result
208205

209206
- name: Set 'sshkey_file' variable
210207
ansible.builtin.set_fact:
211-
sshkey_file: ~/private-key-{{ sshkey_pair_name }}-{{ region | default(aws_region) }}
208+
sshkey_file: ~/private-key-{{ deploy_flask_app_sshkey_pair_name }}-{{ region | default(aws_region) }}
212209

213210
- name: Create key pair to connect to the VM
214211
amazon.aws.ec2_key:
215-
name: "{{ sshkey_pair_name }}"
212+
name: "{{ deploy_flask_app_sshkey_pair_name }}"
216213
register: rsa_key
217214

218215
- name: Save private key into file
@@ -224,10 +221,10 @@
224221

225222
- name: Create a virtual machine
226223
amazon.aws.ec2_instance:
227-
name: "{{ bastion_host_name }}"
224+
name: "{{ deploy_flask_app_bastion_host_name }}"
228225
instance_type: "{{ bastion_host_type }}"
229226
image_id: "{{ images.images.0.image_id }}"
230-
key_name: "{{ sshkey_pair_name }}"
227+
key_name: "{{ deploy_flask_app_sshkey_pair_name }}"
231228
subnet_id: "{{ subnet.subnet.id }}"
232229
network:
233230
assign_public_ip: true
@@ -237,24 +234,4 @@
237234
- "{{ secgroup.group_id }}"
238235
wait: true
239236
state: started
240-
register: result
241-
242-
- name: Add host to inventory
243-
ansible.builtin.add_host:
244-
hostname: bastion
245-
ansible_ssh_user: "{{ bastion_host_username }}"
246-
ansible_host: "{{ result.instances.0.public_ip_address }}"
247-
ansible_ssh_common_args: -o "UserKnownHostsFile=/dev/null" -o StrictHostKeyChecking=no -i {{ sshkey_file }}
248-
ansible_host_name: "{{ result.instances.0.public_dns_name | split('.') | first }}"
249-
host_config:
250-
public_subnet_id: "{{ subnet.subnet.id }}"
251-
private_subnet_id: "{{ private_subnet.subnet.id }}"
252-
image_id: "{{ images.images.0.image_id }}"
253-
group_id: "{{ secgroup.group_id }}"
254-
private_ip: "{{ result.instances.0.private_ip_address }}"
255-
vpc_id: "{{ vpc.vpc.id }}"
256-
rds_info:
257-
host: "{{ rds_info.instances.0.endpoint.address }}"
258-
name: "{{ rds_instance_name }}"
259-
master_user_password: "{{ rds_master_password }}"
260-
master_username: "{{ rds_master_user }}"
237+
register: vm_result

playbooks/webapp/tasks/delete.yaml

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
---
22
- name: Delete resources playbook
3-
module_defaults:
4-
group/aws:
5-
aws_access_key: "{{ aws_access_key | default(omit) }}"
6-
aws_secret_key: "{{ aws_secret_key | default(omit) }}"
7-
security_token: "{{ security_token | default(omit) }}"
8-
region: "{{ region | default(aws_region) }}"
93

104
block:
5+
- name: Set 'region' variable
6+
ansible.builtin.set_fact:
7+
region: "{{ region | default(aws_region) }}"
8+
119
- name: Get vpc information
1210
amazon.aws.ec2_vpc_net_info:
1311
filters:
@@ -25,7 +23,7 @@
2523
amazon.aws.ec2_instance_info:
2624
filters:
2725
instance-type: "{{ bastion_host_type }}"
28-
key-name: "{{ sshkey_pair_name }}"
26+
key-name: "{{ deploy_flask_app_sshkey_pair_name }}"
2927
vpc-id: "{{ vpc_id }}"
3028
register: bastion
3129

@@ -76,7 +74,7 @@
7674

7775
- name: Delete key pair to connect to the bastion VM
7876
amazon.aws.ec2_key:
79-
name: "{{ sshkey_pair_name }}"
77+
name: "{{ deploy_flask_app_sshkey_pair_name }}"
8078
state: absent
8179

8280
- name: Delete RDS subnet group

0 commit comments

Comments
 (0)