Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ansible 1 #7

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
packer/variables.json
terraform.tfvars
"~"
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,17 @@ packer build ubuntu16.json
## Дополнительное задание:
1. Создан шаблон `packer` `immutable.json`, который используется для создания `bake-образа`.
2. Создан скрипт `create-reddit-vm.sh`, который создаем VM на основе ранее созданного `bake-образа`.


# HW №9

- Созданы сеть и подсеть `yandex_vpc_network & yandex_vpc_subnet`.
- Изучены неявная и явная зависимости `depends_on`.
- Созданые зависимые VM `app & db`.
- Конфигурации ВМ вынесены в отдельные каталоги и инициализированы как модули `terraform get`. Осознание работы метода переиспользования модулей и принципа `DRY`.
- Из модулей созданы два окружения: `stage and prod`.

## Дополнительное задание:

- *Создан и инициализирован S3 Object Storage `terraform init -backend-config="access_key=<your access key>" -backend-config="secret_key=<your secret key>"`
- *Во время теста были корректно созданы `prod.tfstate и stage.tfstate`, а так же не было обнаружено блокировок при создании двух проектов одновременно.
6 changes: 6 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[defaults]
inventory = ./inventory
remote_user = ubuntu
private_key_file = ~/.ssh/appuser
host_key_checking = False
retry_files_enabled = False
8 changes: 8 additions & 0 deletions ansible/clone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Clone
hosts: app
tasks:
- name: Clone repo
git:
repo: https://github.com/express42/reddit.git
dest: /home/ubuntu/reddit
4 changes: 4 additions & 0 deletions ansible/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[app]
appserver ansible_host=178.154.201.209
[db]
dbserver ansible_host=158.160.123.163
16 changes: 16 additions & 0 deletions ansible/inventory.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"app": {
"hosts": {
"appserver": {
"ansible_host": 178.154.201.209
}
}
},
"db": {
"hosts": {
"dbserver": {
"ansible_host": 158.160.123.163
}
}
}
}
9 changes: 9 additions & 0 deletions ansible/inventory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
app:
hosts:
appserver:
ansible_host: 178.154.201.209

db:
hosts:
dbserver:
ansible_host: 158.160.123.163
1 change: 1 addition & 0 deletions ansible/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ansible>=2.4
49 changes: 49 additions & 0 deletions ansible/script_inv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/python3

import yandex_cloud_client as yc
import json
from sys import argv

options = ['--list', '--host']

folder_id = 'b1g9rbhfq6ba8dfpgv37'

with open('/home/akuravskij/otus/secrets/key.json', 'r') as infile:
credentials = json.load(infile)

client = yc.ComputeClient(service_account_key=credentials)

clients = client.instances_in_folder(folder_id)


app_name = inst[0].name
db_name = inst[1].name


# получаем IP хостов
app_ip = inst[0].network_interfaces[0].primary_v4_address.one_to_one_nat.address
db_ip = inst[1].network_interfaces[0].primary_v4_address.one_to_one_nat.address


data = \
{
"app": {
"hosts": {
"appserver": {
"ansible_host": app_ip
}
}
},
"db": {
"hosts": {
"dbserver": {
"ansible_host": db_ip
}
}
}
}

a = json.dumps(data)
f = open('inventory.json', 'w')
f.write(json.dumps(data, indent=4))
f.close()
36 changes: 36 additions & 0 deletions packer/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"builders": [
{
"type": "yandex",
"service_account_key_file": "key.json",
"folder_id": "b1g9rbhfq6ba8dfpgv37",
"subnet_id": "e9boli9hpqei2gmc9927",
"zone": "ru-central1-a",
"use_ipv4_nat": true,
"source_image_family": "ubuntu-1604-lts",
"image_name": "app-{{timestamp}}",
"image_family": "reddit-base",
"ssh_username": "ubuntu",
"ssh_keypair_name": "appuser",
"ssh_private_key_file": "/home/akuravskij/.ssh/appuser",
"platform_id": "standard-v1"
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"echo Waiting for apt-get to finish...",
"a=1; while [ -n \"$(pgrep apt-get)\" ]; do echo $a; sleep 1s; a=$(expr $a + 1); done",
"echo Done."
]
},
{
"type": "shell",
"script": "scripts/install_ruby.sh",
"execute_command": "sudo {{.Path}}"
}

]

}
36 changes: 36 additions & 0 deletions packer/db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"builders": [
{
"type": "yandex",
"service_account_key_file": "key.json",
"folder_id": "b1g9rbhfq6ba8dfpgv37",
"subnet_id": "e9boli9hpqei2gmc9927",
"zone": "ru-central1-a",
"use_ipv4_nat": true,
"source_image_family": "ubuntu-1604-lts",
"image_name": "db-{{timestamp}}",
"image_family": "reddit-base",
"ssh_username": "ubuntu",
"ssh_keypair_name": "appuser",
"ssh_private_key_file": "/home/akuravskij/.ssh/appuser",
"platform_id": "standard-v1"
}
],
"provisioners": [
{
"type": "shell",
"inline": [
"echo Waiting for apt-get to finish...",
"a=1; while [ -n \"$(pgrep apt-get)\" ]; do echo $a; sleep 1s; a=$(expr $a + 1); done",
"echo Done."
]
},
{
"type": "shell",
"script": "scripts/install_mongodb.sh",
"execute_command": "sudo {{.Path}}"
}

]

}
12 changes: 6 additions & 6 deletions packer/key.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "********************",
"service_account_id": "********************",
"created_at": "2023-10-29T00:09:57.955124849Z",
"id": "ajeei6l16cc4havceuv9",
"service_account_id": "ajepveubr1hs27mdara3",
"created_at": "2023-10-30T12:38:46.252077555Z",
"key_algorithm": "RSA_2048",
"public_key": "-----BEGIN PUBLIC KEY-----\n********************\n-----END PUBLIC KEY-----\n",
"private_key": "PLEASE DO NOT REMOVE THIS LINE! Yandex.Cloud SA Key ID \n********************\n-----END PRIVATE KEY-----\n"
}
"public_key": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApuUN79DrpcrZ8Sdi4iE6\nfio3eRgfB5/Jyqf0PGKK9YGuTn948F3H2dD26KccfjlViM5hzy/P1BPbSAwpt58o\nXyoEjdQAE0hVtO4tn65NMQ7P7hl4T/jFbjXVO7QBZIc+ler60eubxvvHuM4mnbQ/\nFuei11mpj55Jnk9JXrQJ1ZvO5gwQFymhO0JdwWh1a6wLdgvc6dOzPUKVcZoWuVbQ\nm+gIcaXaehaZrmR7EqC2PFaQk2QSju+1Rw6C/ss78YujhQE5C8aAe3SauEl6cyxD\ndzVzYk5vD5RRkTO/OnfZ7LNAiNpr5rnTlw/BQHvgz8XQlHtMfkqKwABu3WaFmhOf\nmwIDAQAB\n-----END PUBLIC KEY-----\n",
"private_key": "PLEASE DO NOT REMOVE THIS LINE! Yandex.Cloud SA Key ID \u003cajeei6l16cc4havceuv9\u003e\n-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCm5Q3v0Oulytnx\nJ2LiITp+Kjd5GB8Hn8nKp/Q8Yor1ga5Of3jwXcfZ0Pbopxx+OVWIzmHPL8/UE9tI\nDCm3nyhfKgSN1AATSFW07i2frk0xDs/uGXhP+MVuNdU7tAFkhz6V6vrR65vG+8e4\nziadtD8W56LXWamPnkmeT0letAnVm87mDBAXKaE7Ql3BaHVrrAt2C9zp07M9QpVx\nmha5VtCb6Ahxpdp6FpmuZHsSoLY8VpCTZBKO77VHDoL+yzvxi6OFATkLxoB7dJq4\nSXpzLEN3NXNiTm8PlFGRM786d9nss0CI2mvmudOXD8FAe+DPxdCUe0x+SorAAG7d\nZoWaE5+bAgMB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AoGAOJQSSDUjYnS9KNDWgMJ4\nqbku/JBLLtHgtDwxmyoShzbAqfJI5HJTxswdDi+ilegmq+flNy7IwhxOZ6/kExvP\nIv/3c2DwwXbC0ETwkj2BXqvIZD0xp2ryYHDdESABP91Gx1SZF7kyLjAXGvojeC6k\nTDgL4KUoNU//WzNWwIx2ZK0=\n-----END PRIVATE KEY-----\n"
}
2 changes: 1 addition & 1 deletion packer/scripts/install_mongodb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ wget -qO - https://www.mongodb.org/static/pgp/server-3.2.asc | sudo apt-key add
echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list

apt-get update
apt-get install -y mongodb-org
apt-get install -y mongodb-org git

systemctl start mongod
systemctl enable mongod
2 changes: 1 addition & 1 deletion packer/scripts/install_ruby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
apt-get update
apt-get install -y ruby-full ruby-bundler build-essential
apt-get install -y ruby-full ruby-bundler build-essential git
1 change: 1 addition & 0 deletions terraform/.terraform/modules/modules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Modules":[{"Key":"db","Source":"./modules/db","Dir":"modules/db"},{"Key":"","Source":"","Dir":"."},{"Key":"app","Source":"./modules/app","Dir":"modules/app"}]}
Loading
Loading