Skip to content

Commit

Permalink
ansible-2
Browse files Browse the repository at this point in the history
  • Loading branch information
c3ntik committed Oct 18, 2023
1 parent 53347bf commit 8b397b7
Show file tree
Hide file tree
Showing 25 changed files with 345 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ 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.json
inventory = ./inventory
remote_user = ubuntu
private_key_file = ~/.ssh/otus.pub
host_key_checking = False
Expand Down
32 changes: 32 additions & 0 deletions ansible/app.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
- name: Configure App
hosts: app
become: true
vars:
db_host: 172.16.72.4
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: Install git
apt:
name: git
state: present
update_cache: yes

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

handlers:
- name: reload puma
systemd: name=puma state=restarted
2 changes: 1 addition & 1 deletion ansible/clone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
- name: Clone repo
git:
repo: https://github.com/express42/reddit.git
dest: /home/ubuntu/red2
dest: /home/ubuntu/reddit
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
18 changes: 18 additions & 0 deletions ansible/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
- name: Deploy
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
4 changes: 2 additions & 2 deletions ansible/inventory
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[app]
appserver ansible_host=158.160.107.84
appserver ansible_host=158.160.50.91
[db]
dbserver ansible_host=158.160.50.213
dbserver ansible_host=158.160.34.79
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: 158.160.107.84
ansible_host: 158.160.58.127

db:
hosts:
dbserver:
ansible_host: 158.160.50.213
ansible_host: 158.160.112.75
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
28 changes: 28 additions & 0 deletions ansible/packer_db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
- name: Install MongoDB 3.2
hosts: db
become: true
tasks:
# Добавим ключ репозитория для последующей работы с ним
- name: Add APT key
apt_key:
id: EA312927
keyserver: keyserver.ubuntu.com

# Подключаем репозиторий с пакетами mongodb
- 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
70 changes: 70 additions & 0 deletions ansible/reddit_app_multiple_plays.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
- 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: 172.16.72.4
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: Install git
apt:
name: git
state: present
update_cache: yes

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

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

- name: Deploy
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
52 changes: 52 additions & 0 deletions ansible/reddit_app_one_play.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
- name: Configure hosts & deploy application
hosts: all
vars:
mongo_bind_ip: 0.0.0.0 # <-- Переменная задается в блоке vars
db_host: 172.16.72.32
tasks:
- name: Change mongo config file
become: true # <-- Выполнить задание от root
template:
src: templates/mongod.conf.j2 # <-- Путь до локального файла-шаблона
dest: /etc/mongod.conf # <-- Путь на удаленном хосте
mode: 0644
tags: db-tag
- 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
#become: true
#become_user: "ubuntu"
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 # <-- В какой директории выполнить команду bundle
tags: deploy-tag

handlers: # <-- Добавим блок 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 }}
18 changes: 18 additions & 0 deletions ansible/templates/mongod.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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:
# default - один из фильтров Jinja2, он задает значение по умолчанию,
# если переменная слева не определена
port: {{ mongo_port | default('27017') }}
bindIp: {{ mongo_bind_ip }} # <-- Подстановка значения переменной
43 changes: 21 additions & 22 deletions packer/app.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
{
"builders": [
"builders": [
{
"type": "yandex",
"service_account_key_file": "/home/c3nt/otus/devops/c3ntik_infra/packer/key.json",
"folder_id": "b1gi8rea67r233k0htdp",
"subnet_id": "e9bliv6sqtuf7647rnrv",
"zone": "{{user `zone`}}",
"source_image_family": "ubuntu-1604-lts",
"use_ipv4_nat": "true",
"image_name": "reddit-app-{{timestamp}}",
"image_family": "reddit-app",
"disk_size_gb": "10",
"ssh_username": "ubuntu",
"platform_id": "standard-v1"
"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-base-{{timestamp}}",
"image_family": "reddit-app-base",
"ssh_username": "ubuntu",
"platform_id": "standard-v1",
"use_ipv4_nat": "true",
"disk_name": "reddit-app",
"zone": "ru-central1-a"
}
],
"provisioners": [
{
"type": "shell",
"script": "scripts/install_ruby.sh",
"execute_command": "sudo {{.Path}}"
}
]
}
],
"provisioners": [
{
"type": "ansible",
"user": "ubuntu",
"playbook_file": "../ansible/packer_app.yml"
}
]
}
Loading

0 comments on commit 8b397b7

Please sign in to comment.