Skip to content

Commit

Permalink
ansible-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Baykanurov committed Oct 20, 2023
1 parent e1d17fc commit 903df41
Show file tree
Hide file tree
Showing 20 changed files with 326 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ packer/variables.json
*.tfstate.backup
*.tfvars
.terraform/
*.retry
2 changes: 1 addition & 1 deletion ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[defaults]
inventory = ./inventory.py
inventory = ./inventory.yml
remote_user = ubuntu
private_key_file = /home/baykanurov/.ssh/id_ed25519
host_key_checking = False
Expand Down
26 changes: 26 additions & 0 deletions ansible/app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
- name: Configure App
hosts: app
become: true
vars:
db_host: 51.250.0.108
tasks:
- name: Add unit file for Puma
copy:
src: files/puma.service
dest: /etc/systemd/system/puma.service
notify: reload puma

- name: Add config for DB connection
template:
src: templates/db_config.j2
dest: /home/ubuntu/db_config
owner: ubuntu
group: ubuntu

- name: enable puma
systemd: name=puma enabled=yes

handlers:
- name: reload puma
systemd: name=puma state=restarted
17 changes: 17 additions & 0 deletions ansible/db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: Configure MongoDB
hosts: db
become: true
vars:
mongo_bind_ip: 0.0.0.0
tasks:
- name: Change mongo config file
template:
src: templates/mongod.conf.j2
dest: /etc/mongod.conf
mode: 0644
notify: restart mongod

handlers:
- name: restart mongod
service: name=mongod state=restarted
19 changes: 19 additions & 0 deletions ansible/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- name: Deploy App
hosts: app
tasks:
- name: Fetch the latest version of application code
git:
repo: 'https://github.com/express42/reddit.git'
dest: /home/ubuntu/reddit
version: monolith
notify: restart puma

- name: bundle install
bundler:
state: present
chdir: /home/ubuntu/reddit

handlers:
- name: restart puma
become: true
systemd: name=puma state=restarted
14 changes: 14 additions & 0 deletions ansible/files/puma.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=Puma HTTP Server
After=network.target

[Service]
Type=simple
EnvironmentFile=/home/ubuntu/db_config
User=ubuntu
WorkingDirectory=/home/ubuntu/reddit
ExecStart=/bin/bash -lc 'puma'
Restart=always

[Install]
WantedBy=multi-user.target
Empty file modified ansible/inventory.py
100755 → 100644
Empty file.
4 changes: 2 additions & 2 deletions ansible/inventory.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
app:
hosts:
appserver:
ansible_host: 84.201.128.233
ansible_host: 84.252.130.145

db:
hosts:
dbserver:
ansible_host: 62.84.116.43
ansible_host: 51.250.0.108
11 changes: 11 additions & 0 deletions ansible/packer_app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Install Ruby && Bundler
hosts: all
become: true
tasks:
- name: Install ruby and rubygems and required packages
apt: "name={{ item }} state=present"
with_items:
- ruby-full
- ruby-bundler
- build-essential
24 changes: 24 additions & 0 deletions ansible/packer_db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
- name: Install MongoDB 3.2
hosts: all
become: true
tasks:
- name: Add APT key
apt_key:
id: EA312927
keyserver: keyserver.ubuntu.com

- name: Add APT repository
apt_repository:
repo: deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse
state: present

- name: Install mongodb package
apt:
name: mongodb-org
state: present

- name: Configure service supervisor
systemd:
name: mongod
enabled: yes
68 changes: 68 additions & 0 deletions ansible/reddit_app_multiple_plays.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
- name: Configure MongoDB
hosts: db
tags: db-tag
become: true
vars:
mongo_bind_ip: 0.0.0.0
tasks:
- name: Change mongo config file
template:
src: templates/mongod.conf.j2
dest: /etc/mongod.conf
mode: 0644
notify: restart mongod

handlers:
- name: restart mongod
service: name=mongod state=restarted


- name: Configure App
hosts: app
tags: app-tag
become: true
vars:
db_host: 51.250.89.176
tasks:
- name: Add unit file for Puma
copy:
src: files/puma.service
dest: /etc/systemd/system/puma.service
notify: reload puma

- name: Add config for DB connection
template:
src: templates/db_config.j2
dest: /home/ubuntu/db_config
owner: ubuntu
group: ubuntu

- name: enable puma
systemd: name=puma enabled=yes

handlers:
- name: reload puma
systemd: name=puma state=reloaded


- name: Deploy App
hosts: app
tags: deploy-tag
tasks:
- name: Fetch the latest version of application code
git:
repo: 'https://github.com/express42/reddit.git'
dest: /home/ubuntu/reddit
version: monolith
notify: restart puma

- name: bundle install
bundler:
state: present
chdir: /home/ubuntu/reddit

handlers:
- name: restart puma
become: true
systemd: name=puma state=restarted
56 changes: 56 additions & 0 deletions ansible/reddit_app_one_play.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
- name: Configure hosts & deploy application
hosts: all
vars:
mongo_bind_ip: 0.0.0.0
db_host: 51.250.69.71
tasks:
- name: Change mongo config file
become: true
template:
src: templates/mongod.conf.j2
dest: /etc/mongod.conf
mode: 0644
tags: db-tag
notify: restart mongod

- name: Add unit file for Puma
become: true
copy:
src: files/puma.service
dest: /etc/systemd/system/puma.service
tags: app-tag
notify: reload puma

- name: Add config for DB connection
template:
src: templates/db_config.j2
dest: /home/ubuntu/db_config
tags: app-tag

- name: enable puma
become: true
systemd: name=puma enabled=yes
tags: app-tag

- name: Fetch the latest version of application code
git:
repo: 'https://github.com/express42/reddit.git'
dest: /home/ubuntu/reddit
version: monolith
tags: deploy-tag
notify: reload puma

- name: Bundle install
bundler:
state: present
chdir: /home/ubuntu/reddit
tags: deploy-tag

handlers:
- name: restart mongod
become: true
service: name=mongod state=restarted

- name: reload puma
become: true
systemd: name=puma state=restarted
4 changes: 4 additions & 0 deletions ansible/site.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- import_playbook: db.yml
- import_playbook: app.yml
- import_playbook: deploy.yml
1 change: 1 addition & 0 deletions ansible/templates/db_config.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL={{ db_host }}
16 changes: 16 additions & 0 deletions ansible/templates/mongod.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true

# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log

# network interfaces
net:
port: {{ mongo_port | default('27017') }}
bindIp: {{ mongo_bind_ip }}
40 changes: 20 additions & 20 deletions packer/app.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"builders": [
{
"type": "yandex",
"service_account_key_file": "{{user `service_account_key_file`}}",
"folder_id": "{{user `folder_id`}}",
"source_image_family": "{{user `source_image_family`}}",
"image_name": "reddit-base-{{timestamp}}",
"image_family": "reddit-base",
"ssh_username": "{{user `ssh_username`}}",
"platform_id": "standard-v1",
"use_ipv4_nat": "true"
}
],
"provisioners": [
{
"type": "shell",
"script": "scripts/install_ruby.sh",
"execute_command": "sudo {{.Path}}"
}
]
"builders": [
{
"type": "yandex",
"service_account_key_file": "{{user `service_account_key_file`}}",
"folder_id": "{{user `folder_id`}}",
"source_image_family": "{{user `source_image_family`}}",
"image_name": "reddit-app-{{timestamp}}",
"image_family": "reddit-base",
"ssh_username": "{{user `ssh_username`}}",
"platform_id": "standard-v1",
"use_ipv4_nat": "true"
}
],
"provisioners": [
{
"type": "ansible",
"user": "{{user `ssh_username`}}",
"playbook_file": "ansible/packer_app.yml"
}
]
}
40 changes: 20 additions & 20 deletions packer/db.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"builders": [
{
"type": "yandex",
"service_account_key_file": "{{user `service_account_key_file`}}",
"folder_id": "{{user `folder_id`}}",
"source_image_family": "{{user `source_image_family`}}",
"image_name": "reddit-base-{{timestamp}}",
"image_family": "reddit-base",
"ssh_username": "{{user `ssh_username`}}",
"platform_id": "standard-v1",
"use_ipv4_nat": "true"
}
],
"provisioners": [
{
"type": "shell",
"script": "scripts/install_mongodb.sh",
"execute_command": "sudo {{.Path}}"
}
]
"builders": [
{
"type": "yandex",
"service_account_key_file": "{{user `service_account_key_file`}}",
"folder_id": "{{user `folder_id`}}",
"source_image_family": "{{user `source_image_family`}}",
"image_name": "reddit-db-{{timestamp}}",
"image_family": "reddit-base",
"ssh_username": "{{user `ssh_username`}}",
"platform_id": "standard-v1",
"use_ipv4_nat": "true"
}
],
"provisioners": [
{
"type": "ansible",
"user": "{{user `ssh_username`}}",
"playbook_file": "ansible/packer_db.yml"
}
]
}
16 changes: 8 additions & 8 deletions terraform/modules/app/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ resource "yandex_compute_instance" "app" {
private_key = file("${var.private_key_path}")
}

provisioner "file" {
content = templatefile("${path.module}/files/puma.service", { mongod_ip = var.mongod_ip })
destination = "/tmp/puma.service"
}

provisioner "remote-exec" {
script = "${path.module}/files/deploy.sh"
}
# provisioner "file" {
# content = templatefile("${path.module}/files/puma.service", { mongod_ip = var.mongod_ip })
# destination = "/tmp/puma.service"
# }
#
# provisioner "remote-exec" {
# script = "${path.module}/files/deploy.sh"
# }

}
Loading

0 comments on commit 903df41

Please sign in to comment.