--- - name: Deploy Flask Application on Amazon Linux hosts: all become: yes vars: app_directory: "/opt/image_processor" requirements: - Flask==2.0.1 - flask-cors - boto3 - opencv-python-headless - numpy - gunicorn tasks: - name: Update all system packages yum: name: "*" state: latest update_cache: yes - name: Install essential packages yum: name: - gcc - gcc-c++ - git state: present - name: enable Nginx using amazon-linux-extras command: amazon-linux-extras enable nginx1.12 - name: Install nginx yum: name: nginx state: present - name: Upgrade pip command: pip3 install --upgrade pip - name: Install virtualenv using pip command: pip3 install virtualenv - name: Create application directory file: path: "{{ app_directory }}" state: directory mode: '0755' - name: Remove existing virtual environment file: path: "{{ app_directory }}/venv" state: absent - name: Create a virtual environment using virtualenv command: virtualenv -p python {{ app_directory }}/venv - name: Install Python packages in the virtual environment pip: name: "{{ item }}" virtualenv: "{{ app_directory }}/venv" loop: "{{ requirements }}" - name: Copy application files to the server copy: src: "{{ item }}" dest: "{{ app_directory }}" mode: '0644' with_fileglob: - "../../CloudRender/backend/*.py" - name: Setup Gunicorn systemd service template: src: gunicorn.service.j2 dest: /etc/systemd/system/gunicorn.service notify: - Reload systemd - Restart Gunicorn - name: Setup Nginx configuration template: src: nginx.conf.j2 dest: /etc/nginx/conf.d/my_flask_app.conf notify: - Restart nginx - name: Ensure nginx is running and enabled systemd: name: nginx state: started enabled: true handlers: - name: Restart nginx systemd: name: nginx state: restarted - name: Reload systemd systemd: daemon_reload: yes - name: Restart Gunicorn systemd: name: gunicorn state: restarted