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

hw№8 Ansible-1 #8

Merged
merged 13 commits into from
Feb 19, 2024
Merged
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
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,29 @@ external_ip_address_db = "51.250.89.190"
создал инфраструктуру для окружений stage и prod
проверил оба окружения
добавил описание backend для stage и prod

HW_8. Ansible-1
создал окружение и установил ansible с помощью pip
поднял инфраструктуру контура stage
external_ip_address_app = "158.160.55.243"
external_ip_address_db = "62.84.127.171"
создал inventory, проверил ддоступность хостов для ansible
проверил рабооту инвентори в разных вариациях, создал inventory.yml
проверил inventory.yml, проверил модули command, shell, service, git
Создан и протестирован плейбук clone.yml
успешный пинг ansible all -m ping -i inventory
проверка динамического инвентори: ansible all -m ping -i inventory.sh
158.160.55.243 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
62.84.127.171 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
7 changes: 7 additions & 0 deletions ansible/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
v_ansible/
v1_ansible/
*.old
../terraform/
./terraform/
terraform/
*.tf
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.json
remote_user = ubuntu
private_key_file = ~/.ssh/ubuntu
host_key_checking = False
retry_files_enabled = False
7 changes: 7 additions & 0 deletions ansible/clone.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- name: Clone
hosts: app
tasks:
- name: Clone repo
git:
repo: https://github.com/express42/reddit.git
dest: /home/ubuntu/reddit
5 changes: 5 additions & 0 deletions ansible/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[app]
appserver ansible_host=158.160.55.243

[db]
dbserver ansible_host=62.84.127.171
12 changes: 12 additions & 0 deletions ansible/inventory.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"app": {
"hosts": {
"158.160.55.243": null
}
},
"db": {
"hosts": {
"62.84.127.171": null
}
}
}
28 changes: 28 additions & 0 deletions ansible/inventory.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
#yc compute instances list | tail -n +4 | head -n -2 | awk -F '|' '{print "["$3"]\n" $3 "-server" "%ansible_host="$6}' | tr -d '[:blank:]' | sed 's/%/ /g' | sed 's/reddit-//g' > inventory
#ansible-inventory -i inventory --list > inventory.json

if [[ $1 == "--list" ]]; then

app=158.160.55.243
db=62.84.127.171

cat <<EOT
{
"_meta": {
"hostvars": {}
},
"app": {
"hosts": ["${app}"]
},
"db": {
"hosts": ["${db}"]
}
}
EOT

elif [[ $1 == "--host" ]]; then
echo '{"_meta": {"hostvars": {}}}' | jq -M
else
echo '{}'
fi
16 changes: 16 additions & 0 deletions ansible/inventory.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
app:
hosts:
appserver:
ansible_host: 158.160.55.243
vars:
ansible_user: "ubuntu"
ansible_ssh_private_key_file: "~/.ssh/ubuntu"
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
db:
hosts:
dbserver:
ansible_host: 62.84.127.171
vars:
ansible_user: "ubuntu"
ansible_ssh_private_key_file: "~/.ssh/ubuntu"
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
3 changes: 2 additions & 1 deletion ansible/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ansible>=2.4
ansible==2.10.7
#python=3.5.2
9 changes: 0 additions & 9 deletions terraform/.gitignore

This file was deleted.

10 changes: 0 additions & 10 deletions terraform/.terraform.lock.hcl

This file was deleted.

12 changes: 0 additions & 12 deletions terraform/files/deploy.sh

This file was deleted.

13 changes: 0 additions & 13 deletions terraform/files/puma.service

This file was deleted.

39 changes: 0 additions & 39 deletions terraform/main.tf

This file was deleted.

5 changes: 3 additions & 2 deletions terraform/modules/app/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ resource "yandex_compute_instance" "app" {
tags = "reddit-app"
}
resources {
cores = 2
memory = 2
core_fraction = 20
cores = 2
memory = 2
}
boot_disk {
initialize_params {
Expand Down
5 changes: 3 additions & 2 deletions terraform/modules/db/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ resource "yandex_compute_instance" "db" {
}

resources {
cores = 2
memory = 2
core_fraction = 20
cores = 2
memory = 2
}

boot_disk {
Expand Down
6 changes: 0 additions & 6 deletions terraform/outputs.tf

This file was deleted.

10 changes: 0 additions & 10 deletions terraform/prod/.terraform.lock.hcl

This file was deleted.

19 changes: 0 additions & 19 deletions terraform/prod/backend.tf

This file was deleted.

3 changes: 3 additions & 0 deletions terraform/prod/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ terraform {

provider "yandex" {
service_account_key_file = var.service_account_key_file
token = var.yc_token
access_key = var.access_key
secret_key = var.secret_key
cloud_id = var.cloud_id
folder_id = var.folder_id
zone = var.zone
Expand Down
6 changes: 0 additions & 6 deletions terraform/prod/outputs.tf

This file was deleted.

3 changes: 3 additions & 0 deletions terraform/prod/terraform.tfvars.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
yc_token = "y0_AgAAAABnLd1xAATuwQAAAADx1Dxxxxxxxxxxxxxxxxx"
cloud_id = "b1gulp4hfrunfqk3lttb"
folder_id = "b1gsnn20hr06bqfra90t"
zone = "ru-central1-a"
Expand All @@ -9,3 +10,5 @@ subnet_id = "e9b4tphlh2sjr6g0j2cq"
app_disk_image = "fd8atnk3t60s5fmiaqil"
db_disk_image = "fd8u6gsncuov2n36bcrt"
environment_space = "prod"
access_key = "YCAJEHdVd2Qxxxxxxxxxxxxx"
secret_key = "YCNOs05Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
4 changes: 4 additions & 0 deletions terraform/prod/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ variable "access_key" {
variable secret_key {
description = "secret for s3 bucket"
}
variable yc_token {
type = string
description = "yc_token"
}
10 changes: 0 additions & 10 deletions terraform/stage/.terraform.lock.hcl

This file was deleted.

19 changes: 0 additions & 19 deletions terraform/stage/backend.tf

This file was deleted.

1 change: 1 addition & 0 deletions terraform/stage/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ terraform {

provider "yandex" {
service_account_key_file = var.service_account_key_file
token = var.yc_token
access_key = var.access_key
secret_key = var.secret_key
cloud_id = var.cloud_id
Expand Down
6 changes: 0 additions & 6 deletions terraform/stage/outputs.tf

This file was deleted.

3 changes: 3 additions & 0 deletions terraform/stage/terraform.tfvars.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
yc_token = "y0_AgAAAABnLd1xAATuwQAAAADx1Dxxxxxxxxxxxxxxxxx"
cloud_id = "b1gulp4hfrunfqk3lttb"
folder_id = "b1gsnn20hr06bqfra90t"
zone = "ru-central1-a"
Expand All @@ -9,3 +10,5 @@ subnet_id = "e9b4tphlh2sjr6g0j2cq"
app_disk_image = "fd8atnk3t60s5fmiaqil"
db_disk_image = "fd8u6gsncuov2n36bcrt"
environment_space = "stage"
access_key = "YCAJEHdVd2Qxxxxxxxxxxxxx"
secret_key = "YCNOs05Dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
4 changes: 4 additions & 0 deletions terraform/stage/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ variable "access_key" {
variable secret_key {
description = "secret for s3 bucket"
}
variable yc_token {
type = string
description = "yc_token"
}
Loading
Loading