From 8b6309c288aeb30760d80953ab5c75ce71a54786 Mon Sep 17 00:00:00 2001 From: GomathiselviS Date: Fri, 8 Sep 2023 17:09:32 -0400 Subject: [PATCH] Rename playbook and role vars --- .../fragments/add_deploy_flask_app_role.yaml | 2 +- playbooks/webapp/README.md | 32 +++++----- playbooks/webapp/migrate_webapp.yaml | 6 +- playbooks/webapp/tasks/create.yaml | 20 +++--- playbooks/webapp/tasks/delete.yaml | 4 +- playbooks/webapp/vars/main.yaml | 62 +++++++++---------- playbooks/webapp/webapp.yaml | 10 +-- roles/deploy_flask_app/README.md | 53 +++++++++------- .../deploy_flask_app/meta/argument_specs.yaml | 50 +++++++++------ .../deploy_flask_app/tasks/bastion_setup.yaml | 4 +- roles/deploy_flask_app/tasks/deploy_app.yaml | 39 ++++++------ roles/deploy_flask_app/tasks/setup.yaml | 34 +++++----- roles/deploy_flask_app/templates/inventory.j2 | 2 +- roles/deploy_flask_app/templates/vars.yaml.j2 | 14 ++--- .../test_deploy_flask_app/tasks/create.yaml | 23 ++++--- .../test_deploy_flask_app/tasks/delete.yaml | 4 +- .../test_deploy_flask_app/tasks/main.yaml | 10 +-- .../test_deploy_flask_app/vars/main.yaml | 41 ++++++------ 18 files changed, 213 insertions(+), 197 deletions(-) diff --git a/changelogs/fragments/add_deploy_flask_app_role.yaml b/changelogs/fragments/add_deploy_flask_app_role.yaml index 770b1ce5..15c2d4f6 100644 --- a/changelogs/fragments/add_deploy_flask_app_role.yaml +++ b/changelogs/fragments/add_deploy_flask_app_role.yaml @@ -1,5 +1,5 @@ --- breaking_changes: -- "Rename webapp playbook variable rds_master_user to rds_master_username (https://github.com/redhat-cop/cloud.aws_ops/pull/85)." +- "Rename the vars in webapp playbook with role name prefix (https://github.com/redhat-cop/cloud.aws_ops/pull/85)." minor_changes: - "Convert deploy_flask_app playbook to role (https://github.com/redhat-cop/cloud.aws_ops/pull/85)." diff --git a/playbooks/webapp/README.md b/playbooks/webapp/README.md index 547b09ee..ed71fc44 100644 --- a/playbooks/webapp/README.md +++ b/playbooks/webapp/README.md @@ -97,11 +97,11 @@ To delete the webapp: ### EC2 instance * **image_filter** (str): Name of AWS AMI to use. Default: `Fedora-Cloud-Base-35-*` -* **sshkey_pair_name** (str): Name for the EC2 key pair. Default: `"{{ resource_prefix }}-key"` -* **bastion_host_name** (str): Name for the EC2 instance. Default: `"{{ resource_prefix }}-bastion"` -* **bastion_host_type** (str): Instance type for the EC2 instance. Default: `t3.micro` -* **bastion_host_username** (str): Username for the bastion host SSH user. Default: `fedora` -* **bastion_host_required_packages** (list, elements str): Packages to be installed on the bastion host. Default: +* **deploy_sshkey_pair_name** (str): Name for the EC2 key pair. Default: `"{{ resource_prefix }}-key"` +* **deploy_bastion_host_name** (str): Name for the EC2 instance. Default: `"{{ resource_prefix }}-bastion"` +* **bastion_host_type** (str): Instance type for the EC2 instance. Default: `t2.xlarge` +* **deploy_flask_app_bastion_host_username** (str): Username for the bastion host SSH user. Default: `fedora` +* **deploy_flask_app_bastion_host_required_packages** (list, elements str): Packages to be installed on the bastion host. Default: ```yaml - python3 - python-virtualenv @@ -125,7 +125,7 @@ To delete the webapp: * **rds_subnet_group_name** (str): Subnet group name for the RDS instance. Default: `"{{ resource_prefix }}-rds-sg"` * **rds_secgroup_name** (str): Security group name for the RDS instance. Default: `"{{ resource_prefix }}-rds-sec"` * **public_secgroup_name** (str): Security group name for the bastion host. Default: `"{{ resource_prefix }}-sg"` -* **app_listening_port** (int): Connection listening port for the app on the bastion host. Default: `5000` +* **deploy_flask_app_listening_port** (int): Connection listening port for the app on the bastion host. Default: `5000` * **rds_listening_port** (int): Connection listening port for the RDS instance. Default: `5432` ### RDS instance @@ -137,25 +137,25 @@ To delete the webapp: * **rds_instance_name** (str): Name for the database. Default: `mysampledb123` * **rds_engine** (str): Engine to use for the database. Default: `postgres` * **rds_engine_version** (str): Version number of the database engine to use. Default: `"14.8"` -* **rds_master_username** (str): Name of the master user for the database instance. Default: `ansible` -* **rds_master_password** (str): Password for the master database user. Default: `L#5cH2mgy_` +* **deploy_flask_app_rds_master_username** (str): Name of the master user for the database instance. Default: `ansible` +* **deploy_flask_app_rds_master_password** (str): Password for the master database user. Default: `L#5cH2mgy_` ### Webapp -* **app_git_repository** (str): Git repository for the webapp. Default: `https://github.com/abikouo/webapp_pyflask_demo.git` -* **number_of_workers** (int): Number of worker instances to create. Default: `2` -* **workers_instance_type** (str): EC2 instance type for workers. Default: `t3.micro` -* **local_registry_user** (str): Username for local Podman registry. Default: `ansible` -* **local_registry_pwd** (str): Password for local Podman registry. Default: `testing123` -* **local_registry_port** (int): Port for the local Podman registery. Default: `"{{ app_listening_port }}"` -* **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: +* **deploy_flask_app_git_repository** (str): Git repository for the webapp. Default: `https://github.com/abikouo/webapp_pyflask_demo.git` +* **deploy_flask_app_number_of_workers** (int): Number of worker instances to create. Default: `2` +* **deploy_flask_app_workers_instance_type** (str): EC2 instance type for workers. Default: `t2.xlarge` +* **deploy_flask_app_local_registry_user** (str): Username for local Podman registry. Default: `ansible` +* **deploy_flask_app_local_registry_pwd** (str): Password for local Podman registry. Default: `testing123` +* **deploy_flask_app_local_registry_port** (int): Port for the local Podman registery. Default: `"{{ app_listening_port }}"` +* **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: ```yaml app_dir: /app/pyapp env: development admin_user: admin admin_password: admin ``` -* **app_force_init** (bool): Whether to drop existing tables and create new ones when deploying the webapp database. Default: `false` +* **deploy_flask_app_force_init** (bool): Whether to drop existing tables and create new ones when deploying the webapp database. Default: `false` ## Example Usage diff --git a/playbooks/webapp/migrate_webapp.yaml b/playbooks/webapp/migrate_webapp.yaml index 2679013e..527bdf8b 100644 --- a/playbooks/webapp/migrate_webapp.yaml +++ b/playbooks/webapp/migrate_webapp.yaml @@ -86,4 +86,8 @@ ansible.builtin.import_role: name: cloud.aws_ops.deploy_flask_app vars: - region: "{{ dest_region }}" + 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 7db9e2e3..33e3ee28 100644 --- a/playbooks/webapp/tasks/create.yaml +++ b/playbooks/webapp/tasks/create.yaml @@ -121,8 +121,8 @@ to_port: 22 - cidr_ip: 0.0.0.0/0 proto: tcp - from_port: "{{ app_listening_port }}" - to_port: "{{ app_listening_port }}" + from_port: "{{ deploy_flask_app_listening_port }}" + to_port: "{{ deploy_flask_app_listening_port }}" rules_egress: - cidr_ip: 0.0.0.0/0 proto: -1 @@ -163,8 +163,8 @@ db_name: "{{ rds_instance_name }}" engine: "{{ rds_engine }}" engine_version: "{{ rds_engine_version }}" - master_user_password: "{{ rds_master_password }}" - master_username: "{{ rds_master_username }}" + master_user_password: "{{ deploy_flask_app_rds_master_password }}" + master_username: "{{ deploy_flask_app_rds_master_username }}" monitoring_interval: 0 storage_type: standard skip_final_snapshot: true @@ -183,8 +183,8 @@ db_instance_identifier: "{{ rds_identifier }}" engine: "{{ rds_engine }}" engine_version: "{{ rds_engine_version }}" - master_user_password: "{{ rds_master_password }}" - master_username: "{{ rds_master_username }}" + master_user_password: "{{ deploy_flask_app_rds_master_password }}" + master_username: "{{ deploy_flask_app_rds_master_username }}" monitoring_interval: 0 storage_type: standard skip_final_snapshot: true @@ -202,11 +202,11 @@ - name: Set 'sshkey_file' variable ansible.builtin.set_fact: - sshkey_file: ~/private-key-{{ sshkey_pair_name }}-{{ region | default(aws_region) }} + sshkey_file: ~/private-key-{{ deploy_flask_app_sshkey_pair_name }}-{{ region | default(aws_region) }} - name: Create key pair to connect to the VM amazon.aws.ec2_key: - name: "{{ sshkey_pair_name }}" + name: "{{ deploy_flask_app_sshkey_pair_name }}" register: rsa_key - name: Save private key into file @@ -218,10 +218,10 @@ - name: Create a virtual machine amazon.aws.ec2_instance: - name: "{{ bastion_host_name }}" + name: "{{ deploy_flask_app_bastion_host_name }}" instance_type: "{{ bastion_host_type }}" image_id: "{{ images.images.0.image_id }}" - key_name: "{{ sshkey_pair_name }}" + key_name: "{{ deploy_flask_app_sshkey_pair_name }}" subnet_id: "{{ subnet.subnet.id }}" network: assign_public_ip: true diff --git a/playbooks/webapp/tasks/delete.yaml b/playbooks/webapp/tasks/delete.yaml index 1e4787d1..57a07e0d 100644 --- a/playbooks/webapp/tasks/delete.yaml +++ b/playbooks/webapp/tasks/delete.yaml @@ -23,7 +23,7 @@ amazon.aws.ec2_instance_info: filters: instance-type: "{{ bastion_host_type }}" - key-name: "{{ sshkey_pair_name }}" + key-name: "{{ deploy_flask_app_sshkey_pair_name }}" vpc-id: "{{ vpc_id }}" register: bastion @@ -74,7 +74,7 @@ - name: Delete key pair to connect to the bastion VM amazon.aws.ec2_key: - name: "{{ sshkey_pair_name }}" + name: "{{ deploy_flask_app_sshkey_pair_name }}" state: absent - name: Delete RDS subnet group diff --git a/playbooks/webapp/vars/main.yaml b/playbooks/webapp/vars/main.yaml index e99372e9..57920fd2 100644 --- a/playbooks/webapp/vars/main.yaml +++ b/playbooks/webapp/vars/main.yaml @@ -1,4 +1,5 @@ --- +# Variables for create.yaml aws_region: us-east-1 resource_prefix: "ansible-demo-test" vpc_name: "{{ resource_prefix }}-vpc" @@ -9,18 +10,27 @@ subnet_cidr: - 10.1.2.0/24 resource_tags: prefix: "{{ resource_prefix }}" +operation: create -rds_subnet_group_name: "{{ resource_prefix }}-rds-sg" +image_filter: Fedora-Cloud-Base-35-* public_secgroup_name: "{{ resource_prefix }}-sg" +rds_subnet_group_name: "{{ resource_prefix }}-rds-sg" rds_secgroup_name: "{{ resource_prefix }}-rds-sec" rds_identifier: "{{ resource_prefix }}-rds-01" -sshkey_pair_name: "{{ resource_prefix }}-key" - - -bastion_host_name: "{{ resource_prefix }}-bastion" -bastion_host_username: fedora +rds_allocated_storage_gb: 20 +rds_instance_class: db.m6g.large +rds_instance_name: mysampledb123 +rds_engine: postgres +rds_engine_version: "14.8" bastion_host_type: t2.xlarge -bastion_host_required_packages: +bastion_host_venv_path: ~/env +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_required_packages: - python3 - python-virtualenv - sshpass @@ -28,33 +38,19 @@ bastion_host_required_packages: - podman - httpd-tools - ansible -bastion_host_venv_path: ~/env - -workers_instance_type: t3.micro -workers_user_name: fedora -number_of_workers: 2 - -app_listening_port: 5000 -app_git_repository: https://github.com/abikouo/webapp_pyflask_demo.git -app_config: +deploy_flask_app_workers_instance_type: t3.micro +deploy_flask_app_workers_user_name: fedora +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 +deploy_flask_app_config: env: development admin_user: admin admin_password: admin app_dir: /app/pyapp -app_force_init: false -rds_listening_port: 5432 -local_registry_user: ansible -local_registry_pwd: testing123 -local_registry_port: "{{ app_listening_port }}" - -rds_allocated_storage_gb: 20 -rds_instance_class: db.m6g.large -rds_instance_name: mysampledb123 -rds_engine: postgres -rds_engine_version: "14.8" -rds_master_password: L#5cH2mgy_ -rds_master_username: ansible - -image_filter: Fedora-Cloud-Base-35-* - -operation: create +deploy_flask_app_force_init: false +deploy_flask_app_local_registry_user: ansible +deploy_flask_app_local_registry_pwd: testing123 +deploy_flask_app_local_registry_port: "{{ deploy_flask_app_listening_port }}" +deploy_flask_app_rds_master_password: L#5cH2mgy_ +deploy_flask_app_rds_master_username: ansible diff --git a/playbooks/webapp/webapp.yaml b/playbooks/webapp/webapp.yaml index ebc8c261..58973e44 100644 --- a/playbooks/webapp/webapp.yaml +++ b/playbooks/webapp/webapp.yaml @@ -15,7 +15,7 @@ - name: Run operation create/delete ansible.builtin.import_tasks: tasks/{{ operation }}.yaml -- name: Deploy resource from Bastion +- name: Deploy Flask App hosts: localhost gather_facts: false @@ -28,7 +28,7 @@ ansible.builtin.include_role: name: cloud.aws_ops.deploy_flask_app vars: - private_subnet_id: "{{ private_subnet.subnet.id }}" - vpc_id: "{{ vpc.vpc.id }}" - vm_info: "{{ vm_result }}" - rds_info: "{{ rds_result }}" + 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 }}" diff --git a/roles/deploy_flask_app/README.md b/roles/deploy_flask_app/README.md index bd235cc3..690013b9 100644 --- a/roles/deploy_flask_app/README.md +++ b/roles/deploy_flask_app/README.md @@ -23,30 +23,33 @@ Role Variables ## variables to create new hosts and groups in inventory of in memory playbook. -* **region** (str): Region where the app is to be deployed. -* **bastion_host_username** (str): Username for the bastion host SSH user. -* **private_subnet_id** (str): Private subnet id of the bastion host -* **vpc_id** (str): vpc id for the host. -* **rds_info** (dict): A dict of information for the backend RDS. This dict has the output of amazon.aws.rds_instance_info mode. -* **rds_master_username** (str): Username for the RDS instance. -* **rds_master_password** (str): password for the RDS instance. -* **vm_info** (dict): A dict of information for the vm to use. This dict has the output of amazon.aws.ec2_instance_info module. +* **deploy_flask_app_region** (str): Region where the app is to be deployed. +* **deploy_flask_app_bastion_host_username** (str): Username for the bastion host SSH user. +* **deploy_flask_app_private_subnet_id** (str): Private subnet id of the bastion host +* **deploy_flask_app_vpc_id** (str): vpc id for the host. +* **deploy_flask_app_rds_info** (dict): A dict of information for the backend RDS. This dict has the output of amazon.aws.rds_instance_info mode. +* **deploy_flask_app_rds_master_username** (str): Username for the RDS instance. +* **deploy_flask_app_rds_master_password** (str): password for the RDS instance. +* **deploy_flask_app_vm_info** (dict): A dict of information for the vm to use. This dict has the output of amazon.aws.ec2_instance_info module. +* **deploy_flask_app_sshkey_pair_name** (str): Name for the EC2 key pair. ## variables needed for the deployment # Bastion host -* **bastion_host_name** (str): Name for the EC2 instance. -* **bastion_host_required_packages** (list): Packages to be installed on the bastion host. -* **number_of_workers** (int): Number of instances to create. -* **workers_instance_type** (str): RC2 instance type for workers. +* **deploy_flask_app_bastion_host_name** (str): Name for the EC2 instance. +* **deploy_flask_app_bastion_host_required_packages** (list): Packages to be installed on the bastion host. +* **deploy_flask_app_number_of_workers** (int): Number of instances to create. +* **deploy_flask_app_workers_instance_type** (str): RC2 instance type for workers. +* **deploy_flask_app_workers_user_name** (str): Username for the workers. # App -* **app_git_repository** (str): Git repository to be cloned for the webapp. -* **app_listening_port** (int): Load balancer port. -* **app_force_init** (bool): A boolean value True to force init the app and False to not force init. -* **local_registry_user** (str): Registry user name. -* **local_registry_pwd** (str): Registry password. -* **app_config** (dict): A dict of config parameterys for the app. +* **deploy_flask_app_git_repository** (str): Git repository to be cloned for the webapp. +* **deploy_flask_app_listening_port** (int): Load balancer port. +* **deploy_flask_app_force_init** (bool): A boolean value True to force init the app and False to not force init. +* **deploy_flask_app_local_registry_port** (int): Port for the local Podman registry. +* **deploy_flask_app_local_registry_user** (str): Registry user name. +* **deploy_flask_app_local_registry_pwd** (str): Registry password. +* **deploy_flask_app_config** (dict): A dict of config parameterys for the app. **env** (str): Flask env. **admin_user** (str): App config's admin username. **admin_password** (str): App config's admin password. @@ -66,11 +69,11 @@ This role takes care of adding the bastion host to the inventory and deploying t * A Bastion host. * SSH key pair to connect to the host. -The setup of these resources needed for the app deployment can be done using a playbook similar to this [playbook]((https://github.com/ansible-collections/cloud.aws_ops/playbooks/webapp/tasks/create.yaml). +The setup of these resources needed for the app deployment can be done using a playbook similar to this [playbook](https://github.com/ansible-collections/cloud.aws_ops/playbooks/webapp/tasks/create.yaml). **Deploy a simple flask app** -- name: Deploy resource from Bastion +- name: Import Deploy Flask App Role hosts: localhost gather_facts: false @@ -79,7 +82,15 @@ The setup of these resources needed for the app deployment can be done using a p tasks: - name: Deploy app - ansible.builtin.import_tasks: deploy_app.yaml + ansible.builtin.import_role: deploy_flask_app.yaml + +- name: Use Deploy Flask App Role + hosts: localhost + roles: + - role: cloud.aws_ops.deploy_flask_app + vars: + - [vars.yaml](https://github.com/ansible-collections/cloud.aws_ops/playbooks/webapp/vars/main.yaml) + License ------- diff --git a/roles/deploy_flask_app/meta/argument_specs.yaml b/roles/deploy_flask_app/meta/argument_specs.yaml index 3b0b95e5..defd2df8 100644 --- a/roles/deploy_flask_app/meta/argument_specs.yaml +++ b/roles/deploy_flask_app/meta/argument_specs.yaml @@ -3,75 +3,87 @@ argument_specs: main: short_description: Deploy flask app in AWS. options: - region: + deploy_flask_app_region: description: (Optional) Region where the app has to be deployed. type: str - bastion_host_username: + required: False + deploy_flask_app_bastion_host_username: description: Username for the bastion host SSH user. type: str required: True - bastion_host_name: + deploy_flask_app_bastion_host_name: description: Name for the EC2 instance. type: str required: True - bastion_host_required_packages: + deploy_flask_app_bastion_host_required_packages: description: Packages to be installed on the bastion host. type: list elements: str required: True - private_subnet_id: + deploy_flask_app_private_subnet_id: description: Private subnet id of the bastion host. type: str required: True - vpc_id: + deploy_flask_app_vpc_id: description: vpc id for the host. type: str required: True - rds_info: + deploy_flask_app_sshkey_pair_name: + description: Name for the EC2 key pair. + type: str + required: True + deploy_flask_app_rds_info: description: A dict of information for the backend RDS. This dict has the output of amazon.aws.rds_instance_info module. type: dict required: True - rds_master_username: + deploy_flask_app_rds_master_username: description: Master username of the RDS backend. type: str required: True - rds_master_password: + deploy_flask_app_rds_master_password: description: Master password of the RDS backend. type: str required: True - vm_info: + deploy_flask_app_vm_info: description: A dict of information for the vm to use. This dict has the output of amazon.aws.ec2_instance_info module. type: dict required: True - number_of_workers: + deploy_flask_app_number_of_workers: description: Number of instances to create. type: int - workers_instance_type: + deploy_flask_app_workers_instance_type: description: RC2 instance type for workers. type: str required: True - choices: ["t2.nano", "t2.micro", "t3.nano", "t3.micro", "m1.large"] - app_git_repository: + deploy_flask_app_workers_user_name: + description: Username for the workers. + type: str + required: True + deploy_flask_app_git_repository: description: Git repository to be cloned for the webapp. type: str required: True - app_listening_port: + deploy_flask_app_listening_port: description: Load balancer port. type: int required: True - app_force_init: + deploy_flask_app_force_init: description: A boolean value True to force init the app and False to not force init. type: bool required: True - local_registry_user: + deploy_flask_app_local_registry_user: description: Registry user name. type: str required: True - local_registry_pwd: + deploy_flask_app_local_registry_pwd: description: Registry password. type: str required: True - app_config: + deploy_flask_app_local_registry_port: + description: Registry port. + type: int + required: True + deploy_flask_app_config: description: A dict of config parameterys for the app. type: dict required: True diff --git a/roles/deploy_flask_app/tasks/bastion_setup.yaml b/roles/deploy_flask_app/tasks/bastion_setup.yaml index dd9bf264..0ceda6d2 100644 --- a/roles/deploy_flask_app/tasks/bastion_setup.yaml +++ b/roles/deploy_flask_app/tasks/bastion_setup.yaml @@ -16,13 +16,13 @@ - name: Install required packages ansible.builtin.yum: - name: "{{ bastion_host_required_packages }}" + name: "{{ deploy_flask_app_bastion_host_required_packages }}" state: present become: true - name: Generate ssh key for existing user ansible.builtin.user: - name: "{{ bastion_host_username }}" + name: "{{ deploy_flask_app_bastion_host_username }}" state: present generate_ssh_key: true diff --git a/roles/deploy_flask_app/tasks/deploy_app.yaml b/roles/deploy_flask_app/tasks/deploy_app.yaml index 4c2087aa..e8691be6 100644 --- a/roles/deploy_flask_app/tasks/deploy_app.yaml +++ b/roles/deploy_flask_app/tasks/deploy_app.yaml @@ -5,7 +5,7 @@ aws_access_key: "{{ aws_access_key | default(omit) }}" aws_secret_key: "{{ aws_secret_key | default(omit) }}" security_token: "{{ security_token | default(omit) }}" - region: "{{ region | default(aws_region) }}" + region: "{{ deploy_flask_app_region | default(aws_region) }}" block: - name: Set variables @@ -27,12 +27,12 @@ - name: Compute number of instances to create/delete ansible.builtin.set_fact: - deploy_flask_app_expected_instances: "{{ number_of_workers | int - deploy_flask_app_vms.instances | length }}" + deploy_flask_app_expected_instances: "{{ deploy_flask_app_number_of_workers | int - deploy_flask_app_vms.instances | length }}" - name: Create list of targets hosts amazon.aws.ec2_instance: name: "{{ deploy_flask_app_instance_name }}" - instance_type: "{{ workers_instance_type }}" + instance_type: "{{ deploy_flask_app_workers_instance_type }}" image_id: "{{ deploy_flask_app_setup.add_host.host_vars.host_config.image_id }}" key_name: "{{ deploy_flask_app_localhost_key_pair }}" subnet_id: "{{ deploy_flask_app_setup.add_host.host_vars.host_config.private_subnet_id }}" @@ -64,7 +64,6 @@ deploy_flask_app_instances_list: "{{ deploy_flask_app_instances_list + [item.instance_id + ':' + item.private_ip_address] }}" with_items: "{{ deploy_flask_app_vms.instances }}" - - name: Set variables ansible.builtin.set_fact: deploy_flask_app_workers_instances: "{{ deploy_flask_app_vms.instances }}" @@ -89,7 +88,7 @@ block: - name: Clone git repository for web application ansible.builtin.git: - repo: "{{ app_git_repository }}" + repo: "{{ deploy_flask_app_git_repository }}" dest: ~/webapp - name: Build webapp container image @@ -129,15 +128,15 @@ ansible.builtin.shell: cmd: > htpasswd -bBc /opt/registry/auth/htpasswd - {{ local_registry_user }} - {{ local_registry_pwd }} + {{ deploy_flask_app_local_registry_user }} + {{ deploy_flask_app_local_registry_pwd }} changed_when: false - name: Start the registry ansible.builtin.shell: cmd: > podman run --name registry500x - -p {{ app_listening_port }}:5000 + -p {{ deploy_flask_app_listening_port }}:5000 -v /opt/registry/data:/var/lib/registry:z -v /opt/registry/auth:/auth:z -e "REGISTRY_AUTH=htpasswd" @@ -151,26 +150,26 @@ - name: Push image into private registry ansible.builtin.shell: cmd: > - podman login 127.0.0.1:{{ app_listening_port }} -u '{{ local_registry_user }}' -p '{{ local_registry_pwd }}' --tls-verify=false && - podman tag webapp 127.0.0.1:{{ app_listening_port }}/ansible-webapp && - podman push 127.0.0.1:{{ app_listening_port }}/ansible-webapp --tls-verify=false + podman login 127.0.0.1:{{ deploy_flask_app_listening_port }} -u '{{ deploy_flask_app_local_registry_user }}' -p '{{ deploy_flask_app_local_registry_pwd }}' --tls-verify=false && + podman tag webapp 127.0.0.1:{{ deploy_flask_app_listening_port }}/ansible-webapp && + podman push 127.0.0.1:{{ deploy_flask_app_listening_port }}/ansible-webapp --tls-verify=false changed_when: false - name: Initialize database tables ansible.builtin.shell: cmd: > podman run --rm - -e FLASK_APP="{{ app_config.app_dir }}" - -e FLASK_ENV="{{ app_config.env }}" + -e FLASK_APP="{{ deploy_flask_app_config.app_dir }}" + -e FLASK_ENV="{{ deploy_flask_app_config.env }}" -e DATABASE_HOST="{{ deploy_flask_app_setup.add_host.host_vars.host_config.rds_info.host }}" -e DATABASE_INSTANCE="{{ deploy_flask_app_setup.add_host.host_vars.host_config.rds_info.name }}" -e DATABASE_USER="{{ deploy_flask_app_setup.add_host.host_vars.host_config.rds_info.master_username }}" -e DATABASE_PASSWORD="{{ deploy_flask_app_setup.add_host.host_vars.host_config.rds_info.master_user_password }}" - -e ADMIN_USER="{{ app_config.admin_user }}" - -e ADMIN_PASSWORD="{{ app_config.admin_password }}" + -e ADMIN_USER="{{ deploy_flask_app_config.admin_user }}" + -e ADMIN_PASSWORD="{{ deploy_flask_app_config.admin_password }}" -e WORKER_HOSTNAME="{{ inventory_hostname }}" -e WORKERS_HOSTS="bastion" - webapp flask {{ app_force_init | bool | ternary('force-init-db', 'init-db') }} + webapp flask {{ deploy_flask_app_force_init | bool | ternary('force-init-db', 'init-db') }} run_once: true changed_when: false delegate_to: bastion @@ -187,7 +186,7 @@ cmd: > ansible-playbook playbook.yaml -i inventory.ini -vvv -e '@vars.yaml' - -e registry_host_port='{{ deploy_flask_app_setup.add_host.host_vars.host_config.private_ip }}:{{ app_listening_port }}' + -e registry_host_port='{{ deploy_flask_app_setup.add_host.host_vars.host_config.private_ip }}:{{ deploy_flask_app_listening_port }}' args: chdir: ~/ changed_when: false @@ -199,12 +198,12 @@ aws_access_key: "{{ aws_access_key | default(omit) }}" aws_secret_key: "{{ aws_secret_key | default(omit) }}" security_token: "{{ security_token | default(omit) }}" - region: "{{ region | default(aws_region) }}" + region: "{{ deploy_flask_app_region | default(aws_region) }}" amazon.aws.elb_classic_lb: state: present name: "{{ deploy_flask_app_setup.add_host.host_vars.ansible_host_name }}-lb" listeners: - - load_balancer_port: "{{ app_listening_port }}" + - load_balancer_port: "{{ deploy_flask_app_listening_port }}" instance_port: 5000 protocol: HTTP instance_protocol: HTTP @@ -223,4 +222,4 @@ - name: Debug application url ansible.builtin.debug: - msg: "Application url: {{ deploy_flask_app_lb_result.elb.dns_name }}:{{ app_listening_port }}" + msg: "Application url: {{ deploy_flask_app_lb_result.elb.dns_name }}:{{ deploy_flask_app_listening_port }}" diff --git a/roles/deploy_flask_app/tasks/setup.yaml b/roles/deploy_flask_app/tasks/setup.yaml index acf4f46f..e8da0fc5 100644 --- a/roles/deploy_flask_app/tasks/setup.yaml +++ b/roles/deploy_flask_app/tasks/setup.yaml @@ -1,32 +1,32 @@ --- -- name: Set 'region' variable +- name: Set 'deploy_flask_app_region' variable ansible.builtin.set_fact: - deploy_flask_app_region: "{{ region | default(aws_region) }}" + deploy_flask_app_region: "{{ deploy_flask_app_region | default(aws_region) }}" - name: Create resources playbook block: - name: Set 'sshkey_file' variable ansible.builtin.set_fact: - deploy_flask_app_sshkey_file: ~/private-key-{{ sshkey_pair_name }}-{{ deploy_flask_app_region | default(aws_region) }} + deploy_flask_app_sshkey_file: ~/private-key-{{ deploy_flask_app_sshkey_pair_name }}-{{ deploy_flask_app_region | default(aws_region) }} - name: Add host to inventory ansible.builtin.add_host: - hostname: bastion - ansible_ssh_user: "{{ bastion_host_username }}" - ansible_host: "{{ vm_info.instances.0.public_ip_address }}" + hostname: "bastion" + ansible_ssh_user: "{{ deploy_flask_app_bastion_host_username }}" + ansible_host: "{{ deploy_flask_app_vm_info.instances.0.public_ip_address }}" ansible_ssh_common_args: -o "UserKnownHostsFile=/dev/null" -o StrictHostKeyChecking=no -i {{ deploy_flask_app_sshkey_file }} ansible_python_interpreter: auto - ansible_host_name: "{{ vm_info.instances.0.public_dns_name | split('.') | first }}" + ansible_host_name: "{{ deploy_flask_app_vm_info.instances.0.public_dns_name | split('.') | first }}" host_config: - public_subnet_id: "{{ vm_info.instances.0.subnet_id }}" - private_subnet_id: "{{ private_subnet_id }}" - image_id: "{{ vm_info.instances.0.image_id }}" - group_id: "{{ vm_info.instances.0.security_groups[0].group_id }}" - private_ip: "{{ vm_info.instances.0.private_ip_address }}" - vpc_id: "{{ vpc_id }}" + public_subnet_id: "{{ deploy_flask_app_vm_info.instances.0.subnet_id }}" + private_subnet_id: "{{ deploy_flask_app_private_subnet_id }}" + image_id: "{{ deploy_flask_app_vm_info.instances.0.image_id }}" + group_id: "{{ deploy_flask_app_vm_info.instances.0.security_groups[0].group_id }}" + private_ip: "{{ deploy_flask_app_vm_info.instances.0.private_ip_address }}" + vpc_id: "{{ deploy_flask_app_vpc_id }}" rds_info: - host: "{{ rds_info.instances.0.endpoint.address }}" - name: "{{ rds_info.instances.0.dbname | default('mysampledb123') }}" - master_user_password: "{{ rds_master_password | default('L#5cH2mgy_') }}" - master_username: "{{ rds_master_username | default('ansible') }}" + host: "{{ deploy_flask_app_rds_info.instances.0.endpoint.address }}" + name: "{{ deploy_flask_app_rds_info.instances.0.dbname | default('mysampledb123') }}" + master_user_password: "{{ deploy_flask_app_rds_master_password | default('L#5cH2mgy_') }}" + master_username: "{{ deploy_flask_app_rds_master_username | default('ansible') }}" register: deploy_flask_app_setup diff --git a/roles/deploy_flask_app/templates/inventory.j2 b/roles/deploy_flask_app/templates/inventory.j2 index a01f8a43..d5fb7eba 100644 --- a/roles/deploy_flask_app/templates/inventory.j2 +++ b/roles/deploy_flask_app/templates/inventory.j2 @@ -1,4 +1,4 @@ [all] {% for item in deploy_flask_app_workers_instances %} -{{ item.instance_id }} workers_hosts="{{ deploy_flask_app_workers_join }}" ansible_ssh_user="{{ workers_user_name }}" ansible_ssh_common_args='-o "UserKnownHostsFile=/dev/null" -o StrictHostKeyChecking=no' ansible_host="{{ item.private_ip_address }}" +{{ item.instance_id }} workers_hosts="{{ deploy_flask_app_workers_join }}" ansible_ssh_user="{{ deploy_flask_app_workers_user_name }}" ansible_ssh_common_args='-o "UserKnownHostsFile=/dev/null" -o StrictHostKeyChecking=no' ansible_host="{{ item.private_ip_address }}" {% endfor %} diff --git a/roles/deploy_flask_app/templates/vars.yaml.j2 b/roles/deploy_flask_app/templates/vars.yaml.j2 index dfe9c55d..9f61ae53 100644 --- a/roles/deploy_flask_app/templates/vars.yaml.j2 +++ b/roles/deploy_flask_app/templates/vars.yaml.j2 @@ -1,14 +1,14 @@ --- -registry_host_port: "{{ deploy_flask_app_setup.add_host.host_vars.host_config.private_ip }}:{{ local_registry_port }}" +registry_host_port: "{{ deploy_flask_app_setup.add_host.host_vars.host_config.private_ip }}:{{ deploy_flask_app_local_registry_port }}" registry_login: - user: "{{ local_registry_user }}" - password: "{{ local_registry_pwd }}" -application_dir: "{{ app_config.app_dir }}" -application_env: "{{ app_config.env }}" + user: "{{ deploy_flask_app_local_registry_user }}" + password: "{{ deploy_flask_app_local_registry_pwd }}" +application_dir: "{{ deploy_flask_app_config.app_dir }}" +application_env: "{{ deploy_flask_app_config.env }}" application_db: host: "{{ deploy_flask_app_setup.add_host.host_vars.host_config.rds_info.host }}" instance: "{{ deploy_flask_app_setup.add_host.host_vars.host_config.rds_info.name }}" dbuser_name: "{{ deploy_flask_app_setup.add_host.host_vars.host_config.rds_info.master_username }}" dbuser_password: "{{ deploy_flask_app_setup.add_host.host_vars.host_config.rds_info.master_user_password }}" - admin_user: "{{ app_config.admin_user }}" - admin_password: "{{ app_config.admin_password }}" + admin_user: "{{ deploy_flask_app_config.admin_user }}" + admin_password: "{{ deploy_flask_app_config.admin_password }}" 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 9cf17e3e..298ed7d1 100644 --- a/tests/integration/targets/test_deploy_flask_app/tasks/create.yaml +++ b/tests/integration/targets/test_deploy_flask_app/tasks/create.yaml @@ -1,10 +1,9 @@ --- - name: Set 'region' variable ansible.builtin.set_fact: - region: "{{ region | default(aws_region) }}" + region: "{{ deploy_flask_app_region | default(aws_region) }}" - name: Create resources playbook - block: - name: Get image ID to create an instance amazon.aws.ec2_ami_info: @@ -121,8 +120,8 @@ to_port: 22 - cidr_ip: 0.0.0.0/0 proto: tcp - from_port: "{{ app_listening_port }}" - to_port: "{{ app_listening_port }}" + from_port: "{{ deploy_flask_app_listening_port }}" + to_port: "{{ deploy_flask_app_listening_port }}" rules_egress: - cidr_ip: 0.0.0.0/0 proto: -1 @@ -163,8 +162,8 @@ db_name: "{{ rds_instance_name }}" engine: "{{ rds_engine }}" engine_version: "{{ rds_engine_version }}" - master_user_password: "{{ rds_master_password }}" - master_username: "{{ rds_master_username }}" + master_user_password: "{{ deploy_flask_app_rds_master_password }}" + master_username: "{{ deploy_flask_app_rds_master_username }}" monitoring_interval: 0 storage_type: standard skip_final_snapshot: true @@ -183,8 +182,8 @@ db_instance_identifier: "{{ rds_identifier }}" engine: "{{ rds_engine }}" engine_version: "{{ rds_engine_version }}" - master_user_password: "{{ rds_master_password }}" - master_username: "{{ rds_master_user }}" + master_user_password: "{{ deploy_flask_app_rds_master_password }}" + master_username: "{{ deploy_flask_app_rds_master_user }}" monitoring_interval: 0 storage_type: standard skip_final_snapshot: true @@ -202,11 +201,11 @@ - name: Set 'sshkey_file' variable ansible.builtin.set_fact: - sshkey_file: ~/private-key-{{ sshkey_pair_name }}-{{ region | default(aws_region) }} + sshkey_file: ~/private-key-{{ deploy_flask_app_sshkey_pair_name }}-{{ region | default(aws_region) }} - name: Create key pair to connect to the VM amazon.aws.ec2_key: - name: "{{ sshkey_pair_name }}" + name: "{{ deploy_flask_app_sshkey_pair_name }}" register: rsa_key - name: Save private key into file @@ -218,10 +217,10 @@ - name: Create a virtual machine amazon.aws.ec2_instance: - name: "{{ bastion_host_name }}" + name: "{{ deploy_flask_app_bastion_host_name }}" instance_type: "{{ bastion_host_type }}" image_id: "{{ images.images.0.image_id }}" - key_name: "{{ sshkey_pair_name }}" + key_name: "{{ deploy_flask_app_sshkey_pair_name }}" subnet_id: "{{ subnet.subnet.id }}" network: assign_public_ip: true diff --git a/tests/integration/targets/test_deploy_flask_app/tasks/delete.yaml b/tests/integration/targets/test_deploy_flask_app/tasks/delete.yaml index 38baebf8..ae621d91 100644 --- a/tests/integration/targets/test_deploy_flask_app/tasks/delete.yaml +++ b/tests/integration/targets/test_deploy_flask_app/tasks/delete.yaml @@ -19,7 +19,7 @@ amazon.aws.ec2_instance_info: filters: instance-type: "{{ bastion_host_type }}" - key-name: "{{ sshkey_pair_name }}" + key-name: "{{ deploy_flask_app_sshkey_pair_name }}" vpc-id: "{{ vpc_id }}" register: bastion @@ -70,7 +70,7 @@ - name: Delete key pair to connect to the bastion VM amazon.aws.ec2_key: - name: "{{ sshkey_pair_name }}" + name: "{{ deploy_flask_app_sshkey_pair_name }}" state: absent - name: Delete RDS subnet group diff --git a/tests/integration/targets/test_deploy_flask_app/tasks/main.yaml b/tests/integration/targets/test_deploy_flask_app/tasks/main.yaml index 28d0f44b..0b013d3d 100644 --- a/tests/integration/targets/test_deploy_flask_app/tasks/main.yaml +++ b/tests/integration/targets/test_deploy_flask_app/tasks/main.yaml @@ -20,14 +20,14 @@ ansible.builtin.include_role: name: cloud.aws_ops.deploy_flask_app vars: - private_subnet_id: "{{ private_subnet.subnet.id }}" - vpc_id: "{{ vpc.vpc.id }}" - vm_info: "{{ vm_result }}" - rds_info: "{{ rds_result }}" + 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 }}" - name: Check that a page returns successfully ansible.builtin.uri: - url: "http://{{ deploy_flask_app_lb_result.elb.dns_name }}:{{ app_listening_port }}" + url: "http://{{ deploy_flask_app_lb_result.elb.dns_name }}:{{ deploy_flask_app_listening_port }}" register: deploy_flask_app_check until: "deploy_flask_app_check.status == 200" retries: 5 diff --git a/tests/integration/targets/test_deploy_flask_app/vars/main.yaml b/tests/integration/targets/test_deploy_flask_app/vars/main.yaml index a9355bfb..638b72bb 100644 --- a/tests/integration/targets/test_deploy_flask_app/vars/main.yaml +++ b/tests/integration/targets/test_deploy_flask_app/vars/main.yaml @@ -1,7 +1,6 @@ --- # vars for create and delete # ================================== -resource_prefix: "ansible-demo-test" vpc_name: "{{ resource_prefix }}-vpc" vpc_cidr: 10.1.0.0/16 subnet_cidr: @@ -15,27 +14,22 @@ rds_subnet_group_name: "{{ resource_prefix }}-rds-sg" public_secgroup_name: "{{ resource_prefix }}-sg" rds_secgroup_name: "{{ resource_prefix }}-rds-sec" rds_identifier: "{{ resource_prefix }}-rds-01" -sshkey_pair_name: "{{ resource_prefix }}-key" - rds_listening_port: 5432 rds_allocated_storage_gb: 20 rds_instance_class: db.m6g.large rds_instance_name: mysampledb123 rds_engine: postgres rds_engine_version: "14.8" - bastion_host_type: t3.micro bastion_host_venv_path: ~/env - -workers_user_name: fedora image_filter: Fedora-Cloud-Base-35-* # vars for the deploy_flask_app role and create task # ================================================= -region: "us-east-1" -bastion_host_name: "{{ resource_prefix }}-bastion" -bastion_host_username: fedora -bastion_host_required_packages: + +deploy_flask_app_bastion_host_name: "{{ resource_prefix }}-bastion" +deploy_flask_app_bastion_host_username: fedora +deploy_flask_app_bastion_host_required_packages: - python3 - python-virtualenv - sshpass @@ -43,21 +37,22 @@ bastion_host_required_packages: - podman - httpd-tools - ansible - -workers_instance_type: t3.micro -number_of_workers: 2 - -app_listening_port: 5000 -app_git_repository: https://github.com/abikouo/webapp_pyflask_demo.git -app_config: +deploy_flask_app_sshkey_pair_name: "{{ resource_prefix }}-key" +deploy_flask_app_workers_user_name: fedora +deploy_flask_app_workers_instance_type: t3.micro +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 +deploy_flask_app_config: env: development admin_user: admin admin_password: admin app_dir: /app/pyapp -app_force_init: false -local_registry_user: ansible -local_registry_pwd: testing123 -local_registry_port: "{{ app_listening_port }}" +deploy_flask_app_force_init: false +deploy_flask_app_local_registry_user: ansible +deploy_flask_app_local_registry_pwd: testing123 +deploy_flask_app_local_registry_port: "{{ deploy_flask_app_listening_port }}" -rds_master_password: L#5cH2mgy_ -rds_master_username: ansible +deploy_flask_app_rds_master_password: L#5cH2mgy_ +deploy_flask_app_rds_master_username: ansible