Skip to content

Commit

Permalink
Add ansible deployment (#18)
Browse files Browse the repository at this point in the history
* Add ansible deployment to azure vm and make sure the github pipeline only triggers on success

* Update github pipeline to include ansible deployment
  • Loading branch information
Nagisa19 authored Jan 20, 2025
1 parent 3a692f1 commit d0b7646
Show file tree
Hide file tree
Showing 44 changed files with 626 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/push-to-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:

jobs:
build-and-push:
if: ${{ github.event.workflow_run.conclusion == 'success' }}

runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -45,3 +47,22 @@ jobs:
context: ./app/nginx
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/ml_ops_web_server:latest

# Step 2: Set up SSH Key for Ansible
- name: Configure SSH Key
run: |
echo "${{ secrets.ANSIBLE_SSH_KEY }}" > /tmp/id_rsa
chmod 600 /tmp/id_rsa
# Step 2: Set up Ansible
- name: Install Ansible
run: |
sudo apt-get update
sudo apt-get install -y ansible
# Step 3: Run Ansible Playbook
- name: Deploy Application with Ansible
run: |
ansible-playbook -i ansible/inventories/setup.yml ansible/playbooks/deploy-playbook.yml
env:
ANSIBLE_HOST_KEY_CHECKING: "false"
2 changes: 2 additions & 0 deletions .github/workflows/staging-to-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ on:

jobs:
e2e-tests:
if: ${{ github.event.workflow_run.conclusion == 'success' }}

runs-on: ubuntu-latest

steps:
Expand Down
7 changes: 7 additions & 0 deletions ansible/inventories/setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all:
vars:
ansible_user: azureuser
ansible_ssh_private_key_file: /home/clestrat/.ssh/deployementVm_key.pem
children:
prod:
hosts: ml-ops.francecentral.cloudapp.azure.com
35 changes: 35 additions & 0 deletions ansible/playbooks/deploy-playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
- name: Deploy Application
hosts: all
gather_facts: true
become: true

tasks:
- name: Test connection
ping:

- name: Check if Docker is installed
command: docker --version
register: docker_check
ignore_errors: true

- name: Include Install Docker role
include_role:
name: ansible/roles/docker
when: docker_check.failed

- name: Include Create Network role
include_role:
name: ansible/roles/network

- name: Include Launch Backend role
include_role:
name: ansible/roles/backend

- name: Include Launch Frontend role
include_role:
name: ansible/roles/frontend

- name: Include Launch Proxy role
include_role:
name: ansible/roles/proxy

38 changes: 38 additions & 0 deletions ansible/roles/backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Role Name
=========

A brief description of the role goes here.

Requirements
------------

Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.

Role Variables
--------------

A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.

Dependencies
------------

A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.

Example Playbook
----------------

Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:

- hosts: servers
roles:
- { role: username.rolename, x: 42 }

License
-------

BSD

Author Information
------------------

An optional section for the role authors to include contact information, or a website (HTML is not allowed).
3 changes: 3 additions & 0 deletions ansible/roles/backend/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#SPDX-License-Identifier: MIT-0
---
# defaults file for roles/app
3 changes: 3 additions & 0 deletions ansible/roles/backend/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#SPDX-License-Identifier: MIT-0
---
# handlers file for roles/app
35 changes: 35 additions & 0 deletions ansible/roles/backend/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#SPDX-License-Identifier: MIT-0
galaxy_info:
author: your name
description: your role description
company: your company (optional)

# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker

# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)

min_ansible_version: 2.1

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.

dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
11 changes: 11 additions & 0 deletions ansible/roles/backend/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#SPDX-License-Identifier: MIT-0
---
# tasks file for roles/app
- name: Launch Flask Backend Container
docker_container:
name: ml_ops_rest_server
image: clestrat/ml_ops_rest_server:latest
networks:
- name: srv_private
state: started
restart_policy: always
3 changes: 3 additions & 0 deletions ansible/roles/backend/tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#SPDX-License-Identifier: MIT-0
localhost

6 changes: 6 additions & 0 deletions ansible/roles/backend/tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#SPDX-License-Identifier: MIT-0
---
- hosts: localhost
remote_user: root
roles:
- roles/app
3 changes: 3 additions & 0 deletions ansible/roles/backend/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#SPDX-License-Identifier: MIT-0
---
# vars file for roles/app
38 changes: 38 additions & 0 deletions ansible/roles/docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Role Name
=========

A brief description of the role goes here.

Requirements
------------

Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.

Role Variables
--------------

A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.

Dependencies
------------

A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.

Example Playbook
----------------

Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:

- hosts: servers
roles:
- { role: username.rolename, x: 42 }

License
-------

BSD

Author Information
------------------

An optional section for the role authors to include contact information, or a website (HTML is not allowed).
3 changes: 3 additions & 0 deletions ansible/roles/docker/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#SPDX-License-Identifier: MIT-0
---
# defaults file for roles/docker
3 changes: 3 additions & 0 deletions ansible/roles/docker/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#SPDX-License-Identifier: MIT-0
---
# handlers file for roles/docker
35 changes: 35 additions & 0 deletions ansible/roles/docker/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#SPDX-License-Identifier: MIT-0
galaxy_info:
author: your name
description: your role description
company: your company (optional)

# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker

# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)

min_ansible_version: 2.1

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.

dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
58 changes: 58 additions & 0 deletions ansible/roles/docker/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#SPDX-License-Identifier: MIT-0
---
# Install prerequisites for Docker
- name: Install required packages
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg
- lsb-release
- python3-venv
state: latest
update_cache: yes

# Add Docker’s official GPG key
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/debian/gpg
state: present

# Set up the Docker stable repository
- name: Add Docker APT repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/debian {{ ansible_facts['distribution_release'] }} stable"
state: present
update_cache: yes

# Install Docker
- name: Install Docker
apt:
name: docker-ce
state: present

# Install Python3 and pip3
- name: Install Python3 and pip3
apt:
name:
- python3
- python3-pip
state: present

# Create a virtual environment for Python packages
- name: Create a virtual environment for Docker SDK
command: python3 -m venv /opt/docker_venv
args:
creates: /opt/docker_venv # Only runs if this directory doesn’t exist

# Install Docker SDK for Python in the virtual environment
- name: Install Docker SDK for Python in virtual environment
command: /opt/docker_venv/bin/pip install docker

# Ensure Docker is running
- name: Make sure Docker is running
service:
name: docker
state: started
tags: docker
3 changes: 3 additions & 0 deletions ansible/roles/docker/tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#SPDX-License-Identifier: MIT-0
localhost

6 changes: 6 additions & 0 deletions ansible/roles/docker/tests/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#SPDX-License-Identifier: MIT-0
---
- hosts: localhost
remote_user: root
roles:
- roles/docker
3 changes: 3 additions & 0 deletions ansible/roles/docker/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#SPDX-License-Identifier: MIT-0
---
# vars file for roles/docker
38 changes: 38 additions & 0 deletions ansible/roles/frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Role Name
=========

A brief description of the role goes here.

Requirements
------------

Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.

Role Variables
--------------

A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.

Dependencies
------------

A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.

Example Playbook
----------------

Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:

- hosts: servers
roles:
- { role: username.rolename, x: 42 }

License
-------

BSD

Author Information
------------------

An optional section for the role authors to include contact information, or a website (HTML is not allowed).
3 changes: 3 additions & 0 deletions ansible/roles/frontend/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#SPDX-License-Identifier: MIT-0
---
# defaults file for roles/frontend
3 changes: 3 additions & 0 deletions ansible/roles/frontend/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#SPDX-License-Identifier: MIT-0
---
# handlers file for roles/frontend
Loading

0 comments on commit d0b7646

Please sign in to comment.