Skip to content

Commit

Permalink
Added ansible playbook to deploy app
Browse files Browse the repository at this point in the history
  • Loading branch information
rishavnandi committed Mar 13, 2023
1 parent 70651c2 commit b3d0168
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[defaults]
inventory = inventory
host_key_checking = False
forks = 32

[ssh]
pipelining = true
31 changes: 31 additions & 0 deletions group_vars/all/vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
username: vagrant

docker_dir: /home/{{ username }}/docker

packages:
- unzip
- wget
- curl
- git
- htop
- build-essential
- nano
- python3
- python3-pip

docker_dependencies:
- apt-transport-https
- ca-certificates
- gnupg2
- curl
- software-properties-common
- python3-pip
- virtualenv
- python3-setuptools
- docker-compose

docker_packages:
- docker-ce
- docker-compose
- cgroupfs-mount
6 changes: 6 additions & 0 deletions inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[server]
192.168.60.4

[server:vars]
ansible_user = vagrant
ansible_ssh_private_key_file = ~/.vagrant.d/insecure_private_key
12 changes: 12 additions & 0 deletions run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Setup docker and deploy flask app
hosts: all
become: true

tasks:
- name: Perform essential tasks
ansible.builtin.import_tasks: tasks/essential.yml
- name: Setup docker
ansible.builtin.import_tasks: tasks/docker.yml
- name: Deploy the app
ansible.builtin.import_tasks: tasks/flask.yml
10 changes: 10 additions & 0 deletions tasks/app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Deploy the docker container
community.docker.docker_container:
name: flask-stock
image: rishavnandi/flask-stock:latest-slim
pull: true
state: started
restart_policy: unless-stopped
ports:
- "5000:5000"
65 changes: 65 additions & 0 deletions tasks/docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
- name: Set amd64 architecture
ansible.builtin.set_fact:
docker_arch: amd64
when: ansible_architecture == "x86_64"

- name: Set arm64 architecture
ansible.builtin.set_fact:
docker_arch: arm64
when: ansible_architecture == "aarch64" or ansible_architecture == "arm64"

- name: Install docker dependencies
ansible.builtin.apt:
name: "{{ docker_dependencies }}"
state: present

- name: Add docker gpg key
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Add docker repository
ansible.builtin.apt_repository:
repo: deb [arch={{ docker_arch }}] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
state: present

- name: Update apt and install docker
block:
- name: Attempt installation
ansible.builtin.apt:
update_cache: true
name: "{{ docker_packages }}"
state: present
rescue:
- name: Fix the dumb Ubuntu Jammy error
ansible.builtin.replace:
path: /etc/systemd/system/multi-user.target.wants/docker.service
regexp: "fd://"
replace: "unix://"
when: ansible_distribution == 'Ubuntu' and ansible_distribution_version is version('22.04', '>=')

- name: Ensure docker group exists
ansible.builtin.group:
name: docker
state: present

- name: Add user to docker group
ansible.builtin.user:
name: "{{ username }}"
groups: docker
append: true

- name: Enable docker service
ansible.builtin.service:
name: docker
enabled: true
state: started

- name: Create docker directory
ansible.builtin.file:
path: "{{ docker_dir }}"
state: directory
owner: "{{ username }}"
group: "{{ username }}"
mode: 0755
12 changes: 12 additions & 0 deletions tasks/essential.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Update the server
ansible.builtin.apt:
update_cache: yes
cache_valid_time: 3600
upgrade: yes

- name: Install packages
ansible.builtin.apt:
name: "{{ item }}"
state: present
loop: "{{ packages }}"

0 comments on commit b3d0168

Please sign in to comment.