From e2423e7e92af063b4309fdc6bffaeb7c5e5adac4 Mon Sep 17 00:00:00 2001 From: Von Random Date: Sun, 19 Jan 2025 23:57:00 +0200 Subject: [PATCH] rearrange things, automate ansible inventory --- .gitignore | 1 + README.md | 6 ----- ansible/ansible.cfg | 6 +++++ ansible/inventory.yml | 2 ++ nginx.conf.j2 => ansible/nginx.conf.j2 | 0 nginx_setup.yml => ansible/nginx_setup.yml | 0 mkinv | 11 --------- output.tf | 9 -------- main.tf => terraform/main.tf | 27 ++++++++++++++++------ terraform/output.tf | 3 +++ users.yml => terraform/users.yml | 0 11 files changed, 32 insertions(+), 33 deletions(-) create mode 100644 .gitignore create mode 100644 ansible/ansible.cfg create mode 100644 ansible/inventory.yml rename nginx.conf.j2 => ansible/nginx.conf.j2 (100%) rename nginx_setup.yml => ansible/nginx_setup.yml (100%) delete mode 100755 mkinv delete mode 100644 output.tf rename main.tf => terraform/main.tf (72%) create mode 100644 terraform/output.tf rename users.yml => terraform/users.yml (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b6124d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +ansible/id_ed25519 diff --git a/README.md b/README.md index 2a172d6..e8e6c68 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,5 @@ # yatf -## nginx setup on vms: -```bash -./mkinv 127.0.0.1 127.0.0.2 -ansible-playbook nginx_setup.yml -i ./inventory.ini -``` - ## doc links https://terraform-provider.yandexcloud.net/ https://yandex.cloud/ru/docs/tutorials/infrastructure-management/terraform-quickstart diff --git a/ansible/ansible.cfg b/ansible/ansible.cfg new file mode 100644 index 0000000..51eb0db --- /dev/null +++ b/ansible/ansible.cfg @@ -0,0 +1,6 @@ +[defaults] +inventory = inventory.yml + +remote_user = andrei +private_key_file = id_ed25519_yatf +host_key_checking = False diff --git a/ansible/inventory.yml b/ansible/inventory.yml new file mode 100644 index 0000000..eb5c8df --- /dev/null +++ b/ansible/inventory.yml @@ -0,0 +1,2 @@ +plugin: cloud.terraform.terraform_provider +project_path: ../terraform diff --git a/nginx.conf.j2 b/ansible/nginx.conf.j2 similarity index 100% rename from nginx.conf.j2 rename to ansible/nginx.conf.j2 diff --git a/nginx_setup.yml b/ansible/nginx_setup.yml similarity index 100% rename from nginx_setup.yml rename to ansible/nginx_setup.yml diff --git a/mkinv b/mkinv deleted file mode 100755 index 0f37862..0000000 --- a/mkinv +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash -FILENAME="./inventory.ini" -A_USER="andrei" -A_KEY="$HOME/.ssh/id_ed25519_yatf" - -counter=0 -echo -n > "$FILENAME" -for i; do - echo "vm$counter ansible_host=$i ansible_user=$A_USER ansible_ssh_private_key_file=$A_KEY" >> "$FILENAME" - ((counter++)) -done diff --git a/output.tf b/output.tf deleted file mode 100644 index 7183801..0000000 --- a/output.tf +++ /dev/null @@ -1,9 +0,0 @@ -output "lb-ip" { - value = yandex_lb_network_load_balancer.balancer1.listener -} - -output "vm-ips" { - value = tomap({ - for name, vm in yandex_compute_instance.vm : name => vm.network_interface.0.nat_ip_address - }) -} diff --git a/main.tf b/terraform/main.tf similarity index 72% rename from main.tf rename to terraform/main.tf index a45ab13..35e4764 100644 --- a/main.tf +++ b/terraform/main.tf @@ -3,6 +3,9 @@ terraform { yandex = { source = "yandex-cloud/yandex" } + ansible = { + source = "ansible/ansible" + } } } @@ -12,6 +15,7 @@ provider "yandex" { resource "yandex_compute_instance" "vm" { count = 2 + name = "vm${count.index}" platform_id = "standard-v1" boot_disk { @@ -47,14 +51,13 @@ resource "yandex_vpc_subnet" "subnet1" { resource "yandex_lb_target_group" "group1" { name = "group1" - target { - subnet_id = yandex_vpc_subnet.subnet1.id - address = yandex_compute_instance.vm[0].network_interface.0.ip_address - } - target { - subnet_id = yandex_vpc_subnet.subnet1.id - address = yandex_compute_instance.vm[1].network_interface.0.ip_address + dynamic "target" { + for_each = yandex_compute_instance.vm + content { + subnet_id = yandex_vpc_subnet.subnet1.id + address = target.value.network_interface.0.ip_address + } } } @@ -80,3 +83,13 @@ resource "yandex_lb_network_load_balancer" "balancer1" { } } } + +resource "ansible_host" "vm" { + count = length(yandex_compute_instance.vm) + + name = "vm${count.index}" + groups = ["nginx"] + variables = { + ansible_host = yandex_compute_instance.vm[count.index].network_interface.0.nat_ip_address + } +} diff --git a/terraform/output.tf b/terraform/output.tf new file mode 100644 index 0000000..3c58075 --- /dev/null +++ b/terraform/output.tf @@ -0,0 +1,3 @@ +output "lb-ip" { + value = yandex_lb_network_load_balancer.balancer1.listener +} diff --git a/users.yml b/terraform/users.yml similarity index 100% rename from users.yml rename to terraform/users.yml