├── README.md ├── ansible.cfg ├── app.py ├── inventory ├── playbook.yml └── upgrade_zlib.yml /README.md: -------------------------------------------------------------------------------- 1 | # Simple Web Application 2 | Automates deployment of simple web application 3 | 4 | ## 1. Clone repository 5 | ``` 6 | $ git clone https://github.com/amrutja26/simple_web_application.git 7 | $ cd simple_web_application 8 | ``` 9 | 10 | ## 2. Upgrade zlib version to be compatible with mysql-5.6.42 11 | mysql community release requires zlib 1.2.11+ now as per https://dev.mysql.com/doc/relnotes/mysql/5.6/en/news-5-6-42.html 12 | ``` 13 | $ ansible-playbook upgrade_zlib.yml -i inventory 14 | ``` 15 | ## 3. Install web application requirements and deploy the application 16 | ``` 17 | $ ansible-playbook playbook.yml -i inventory 18 | ``` 19 | 20 | **Note: Please adjust the inventory file setting as per your environment before running ansible-playbook** 21 | -------------------------------------------------------------------------------- /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | host_key_checking = False 3 | 4 | [privilege_escalation] 5 | become=True 6 | become_method=sudo 7 | become_user=root 8 | become_ask_pass=True 9 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import os 2 | from flask import Flask 3 | from flaskext.mysql import MySQL # For newer versions of flask-mysql 4 | # from flask.ext.mysql import MySQL # For older versions of flask-mysql 5 | app = Flask(__name__) 6 | 7 | mysql = MySQL() 8 | 9 | mysql_database_host = 'MYSQL_DATABASE_HOST' in os.environ and os.environ['MYSQL_DATABASE_HOST'] or 'localhost' 10 | 11 | # MySQL configurations 12 | app.config['MYSQL_DATABASE_USER'] = 'db_user' 13 | app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd' 14 | app.config['MYSQL_DATABASE_DB'] = 'employee_db' 15 | app.config['MYSQL_DATABASE_HOST'] = mysql_database_host 16 | mysql.init_app(app) 17 | 18 | conn = mysql.connect() 19 | 20 | cursor = conn.cursor() 21 | 22 | @app.route("/") 23 | def main(): 24 | return "Welcome!" 25 | 26 | @app.route('/how are you') 27 | def hello(): 28 | return 'I am good, how about you?' 29 | 30 | @app.route('/read from database') 31 | def read(): 32 | cursor.execute("SELECT * FROM employees") 33 | row = cursor.fetchone() 34 | result = [] 35 | while row is not None: 36 | result.append(row[0]) 37 | row = cursor.fetchone() 38 | 39 | return ",".join(result) 40 | 41 | if __name__ == "__main__": 42 | app.run() 43 | -------------------------------------------------------------------------------- /inventory: -------------------------------------------------------------------------------- 1 | db_and_web_server1 ansible_host=192.168.57.5 ansible_user=osboxes ansible_ssh_pass=osboxes.org 2 | db_and_web_server1 ansible_host=192.168.57.6 ansible_user=osboxes ansible_ssh_pass=osboxes.org 3 | -------------------------------------------------------------------------------- /playbook.yml: -------------------------------------------------------------------------------- 1 | # This playbook requires privilege escalation to be set. Please 2 | # refer the ansible.cfg in current working directory for privilege_escalation 3 | # configuration. 4 | 5 | - name: Deploy a web application 6 | hosts: db_and_web_server1,db_and_web_server2 7 | tasks: 8 | - name: Install all required dependencies 9 | yum: 10 | name: "{{ item }}" 11 | state: present 12 | with_items: 13 | - "epel-release" 14 | - "python" 15 | - "python-devel" 16 | 17 | - name: Install MySQL community repository 18 | yum: 19 | name: "http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm" 20 | state: present 21 | 22 | - name: Install MySQL database 23 | yum: 24 | name: "{{ item }}" 25 | state: present 26 | with_items: 27 | - "mysql-server" 28 | - "mysql-devel" 29 | 30 | - name: create MySQL configuration file 31 | copy: 32 | content: | 33 | [client] 34 | user=root 35 | password="@targ8et" 36 | dest: "/etc/.my.cnf" 37 | 38 | - name: Start MySQL Service 39 | service: 40 | name: mysqld 41 | state: started 42 | enabled: yes 43 | 44 | - name: Install mysql-python package 45 | yum: 46 | name: MySQL-python 47 | state: present 48 | 49 | - name: Install pip 50 | yum: 51 | name: "python-pip" 52 | state: present 53 | 54 | - name: Install required Python libraries 55 | pip: 56 | name: "{{ item }}" 57 | state: present 58 | with_items: 59 | - "flask" 60 | - "flask-mysql" 61 | 62 | - name: Create Application Database 63 | mysql_db: 64 | name: employee_db 65 | state: present 66 | 67 | - name: Create Database user 68 | mysql_user: 69 | name: db_user 70 | password: Passw0rd 71 | priv: '*.*:ALL' 72 | state: present 73 | 74 | - name: Copy source code 75 | copy: 76 | src: app.py 77 | dest: /opt/app.py 78 | 79 | - name: Start web server 80 | shell: FLASK_APP=/opt/app.py nohup flask run --host=0.0.0.0 & 81 | -------------------------------------------------------------------------------- /upgrade_zlib.yml: -------------------------------------------------------------------------------- 1 | # Upgrade zlib versions 2 | # https://github.com/ansible/ansible/issues/47927#issuecomment-438351341 3 | - name: Deploy a web application 4 | hosts: db_and_web_server1,db_and_web_server2 5 | become: yes 6 | become_user: root 7 | tasks: 8 | - name: install the 'Development tools' package group 9 | yum: 10 | name: "@Development tools" 11 | state: present 12 | 13 | - name: Upgrade zlib to latest version 14 | shell: "{{ item }}" 15 | args: 16 | chdir: /tmp 17 | with_items: 18 | - "wget https://zlib.net/zlib-1.2.11.tar.gz" 19 | - "tar -xzf zlib-1.2.11.tar.gz" 20 | - "/tmp/zlib-1.2.11/configure --prefix=/usr" 21 | - "/usr/bin/make" 22 | - "/usr/bin/make install" 23 | --------------------------------------------------------------------------------