Lab 12 - Final Lab¶
Mandatory¶
Get an overview¶
Checkout the README of this repo and try to figure out, what’s needed to run this application.
Frontend part¶
Reuse your
flask_approleEnsure, the role has a dependency on your
python-piproleInstall all pip packages, that are listed in the README. (Use the Pip Module)
Install all Red Hat specified packages, that are listed in the README. Hint:
when ConditionInstall all Debian specified packages, that are listed in the README. Hint:
when ConditionCreate a task to clone the git repository
- repo: https://github.com/pstauffer/flask-mysql-app.git
- dest:
/opt/flask_app - version: the defined
flask_app_versionvariable
Create a “version” template file
- path:
/tmp/app-version.txt - content: the defined
flask_app_versionvariable
- path:
Define a database configuration template file
- path:
/etc/dbhost.cfg - content:
MYSQL_DATABASE_HOST = '{{ groups['db'][0] }}'
- path:
Add a default variable
flask_gunicorn_commandwith the valuegunicorn --pid /tmp/gunicorn.pid --chdir /opt/flask_app --bind 0.0.0.0:8000 --daemon wsgi:appUse the default variable
flask_gunicorn_commandto start gunicorn via thecommandmodule.
Hint
If something went wrong, try to kill the gunicorn process by hand -> ansible -m shell -a "pgrep gunicorn|xargs kill" web -i prod
Backend part¶
Create a role
flask_dbfor the backend part and try to fulfill the requirementsinstall the
mysql-serverpackageconfigure the bind-address in the mysql configuration file
/etc/mysql/my.cnf. You can use the module Lineinfile to only replace thebind-addressdefinition. Or just manage the whole file with thetemplatemodule.define a handler to restart mysql after configuration change.
download the database dump with the get_url Module
- url: https://raw.githubusercontent.com/pstauffer/flask-mysql-app/master/students.sql
- dest:
/tmp/students.sql
load the mysql dump with the
shellcommandmysql --user=root --password='' < /tmp/students.sql. Also add the optionschanged_when: falseandignore_errors: true.ensure the mysql service is enabled and started
Playbooks and tests¶
Create a playbook
play-db.ymlfor the database part- include the defined
flask_dbrole - run only on
dbhostgroup
- include the defined
Create a playbook
play-web.ymlfor the web part- include the defined
flask_approle - run only on
webhostgroup
- include the defined
Create a master playbook
deploy.ymland include both playbooks (1. db, 2. web)Run your playbook
deploy.ymlfor both inventories (testandprod)Test the application (app_version and database entries)
Hint
You should see the version v1.1 and other images on the web2 url
Optional¶
- Run the gunicorn command only, if the pid file
/tmp/gunicorn.piddoes not exists. - Use tags
- Try to split the roles, to make parts more reusable (like gunicorn, git, flask)
Commit your changes¶
- add all files to your git repo and commit it