Skip to content

Commit

Permalink
update ansible playbook
Browse files Browse the repository at this point in the history
  • Loading branch information
yuriipavlov committed Mar 6, 2024
1 parent 956e3e1 commit b56daef
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ terraform:
terraform -chdir=iac/terraform $(PARAMS)

ansible:
ansible-playbook -i iac/ansible/inventory.ini iac/ansible/playbook-debian.yml $(PARAMS)
ansible-playbook -i iac/ansible/inventory.ini iac/ansible/playbook.yml $(PARAMS)

# docker build|docker push|docker clean
docker:
Expand Down
35 changes: 35 additions & 0 deletions iac/ansible/partials/common-tasks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Prepare common tasks

- name: Update apt cache
apt:
update_cache: yes
cache_valid_time: 3600

- name: Install required system packages
apt:
name: "{{ packages }}"
vars:
packages:
- ca-certificates
- curl
- gnupg
- make
- rsync

- name: Ensure ansible_user owns the deploy folder
file:
path: /srv
owner: "{{ ansible_user }}"
state: directory
recurse: yes

- name: Update hostname
hostname:
name: "{{ inventory_hostname }}"

- name: Update PS1 to show full hostname
replace:
path: /home/{{ ansible_user }}/.bashrc
regexp: '\\u@\\h'
replace: '\\u@\\H'
backup: yes
46 changes: 46 additions & 0 deletions iac/ansible/partials/install-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Install Docker and Docker Compose

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

- name: Set up the stable repository
apt_repository:
repo: "deb [arch=arm64] https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable"
state: present

- name: Install Docker Engine
apt:
name: docker-ce
state: latest

- name: Add current user to the docker group
user:
name: "{{ ansible_user }}"
groups: docker
append: yes

- name: Ensure Docker service is running
service:
name: docker
state: started
enabled: yes

- name: Verify Docker installation
command: docker --version
register: docker_version
changed_when: false

- name: Output Docker version
debug:
var: docker_version.stdout

- name: Verify Docker Compose plugin installation
command: docker compose version
register: compose_version
changed_when: false

- name: Output Docker Compose plugin version
debug:
var: compose_version.stdout
37 changes: 37 additions & 0 deletions iac/ansible/partials/setup-swap.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Setup swap

- name: Check if swap is already enabled
shell: swapon --show | grep -q "^"
register: swap_exists
changed_when: false
ignore_errors: true

- name: Create swap file
command: fallocate -l 1G /swapfile
when: swap_exists.rc != 0
become: yes

- name: Set swap file permissions
file:
path: /swapfile
mode: '0600'
when: swap_exists.rc != 0
become: yes

- name: Set up swap space
command: mkswap /swapfile
when: swap_exists.rc != 0
become: yes

- name: Enable swap
command: swapon /swapfile
when: swap_exists.rc != 0
become: yes

- name: Add swap to fstab
blockinfile:
path: /etc/fstab
block: "/swapfile none swap sw 0 0"
marker: "# {mark} ANSIBLE MANAGED BLOCK"
when: swap_exists.rc != 0
become: yes
5 changes: 3 additions & 2 deletions iac/ansible/playbook-alpine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
state: present
vars:
packages:
- make
- rsync
- bash
- docker
- docker-cli-compose
- make
- rsync

- name: Add docker service to start at boot
command: rc-update add docker default
Expand Down
2 changes: 1 addition & 1 deletion iac/ansible/playbook-debian.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
- ca-certificates
- curl
- gnupg
- rsync
- make
- rsync

- name: Add Docker’s official GPG key
apt_key:
Expand Down
8 changes: 8 additions & 0 deletions iac/ansible/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- name: Install required packages and settings on Debian/Ubuntu Server
hosts:
- development2
become: yes
tasks:
- import_tasks: ./partials/common-tasks.yml
- import_tasks: ./partials/install-docker.yml
- import_tasks: ./partials/setup-swap.yml
8 changes: 4 additions & 4 deletions iac/terraform/instances.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ resource "aws_instance" "develop-server" {
key_name = aws_key_pair.deploy.key_name

tags = {
Name = "starter-kit.io DEV"
Name = "DEV starter-kit.io"
Environment = "Development"
}

root_block_device {
volume_type = "gp2" # General Purpose SSD
volume_size = 8 # Size in GB
volume_size = 20 # Size in GB
}
}

Expand All @@ -24,13 +24,13 @@ resource "aws_instance" "develop2-server" {
key_name = aws_key_pair.deploy.key_name

tags = {
Name = "starter-kit.io DEV2"
Name = "DEV2 starter-kit.io"
Environment = "Development"
}

root_block_device {
volume_type = "gp2" # General Purpose SSD
volume_size = 8 # Size in GB
volume_size = 20 # Size in GB
}
}

Expand Down

0 comments on commit b56daef

Please sign in to comment.