From 75aeaf38a907d650984f06d4ddc7f6b5bce2d38c Mon Sep 17 00:00:00 2001 From: Von Random <von@mechanus.net> Date: Sun, 16 Feb 2025 22:13:44 +0200 Subject: [PATCH] use local_file for ansible inventory --- .gitignore | 1 - ansible/ansible.cfg | 6 +++--- ansible/inventory.yml | 2 -- terraform/ansible.tf | 10 ++++++++++ terraform/main.tf | 35 +++++++++++------------------------ 5 files changed, 24 insertions(+), 30 deletions(-) delete mode 100644 .gitignore delete mode 100644 ansible/inventory.yml create mode 100644 terraform/ansible.tf diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 2b6124d..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -ansible/id_ed25519 diff --git a/ansible/ansible.cfg b/ansible/ansible.cfg index 55afa3a..662d3a5 100644 --- a/ansible/ansible.cfg +++ b/ansible/ansible.cfg @@ -1,7 +1,7 @@ [defaults] -inventory = inventory.yml +inventory = inventory.ini interpreter_python = /usr/bin/python3 -remote_user = andrei -private_key_file = ~/.ssh/id_ed25519_yatf +remote_user = ubuntu +private_key_file = ~/.ssh/id_ed25519 host_key_checking = False diff --git a/ansible/inventory.yml b/ansible/inventory.yml deleted file mode 100644 index eb5c8df..0000000 --- a/ansible/inventory.yml +++ /dev/null @@ -1,2 +0,0 @@ -plugin: cloud.terraform.terraform_provider -project_path: ../terraform diff --git a/terraform/ansible.tf b/terraform/ansible.tf new file mode 100644 index 0000000..ae0b561 --- /dev/null +++ b/terraform/ansible.tf @@ -0,0 +1,10 @@ +resource "local_file" "ansible_inventory" { + file_permission = "0644" + filename = "${path.module}/../ansible/inventory.ini" + content = <<EOT +[all] +%{for host in yandex_compute_instance.vm~} +${trimspace("${host.name} ansible_host=${host.network_interface.0.nat_ip_address}")} +%{endfor~} +EOT +} diff --git a/terraform/main.tf b/terraform/main.tf index 35e4764..ab191e2 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -3,9 +3,6 @@ terraform { yandex = { source = "yandex-cloud/yandex" } - ansible = { - source = "ansible/ansible" - } } } @@ -16,27 +13,27 @@ provider "yandex" { resource "yandex_compute_instance" "vm" { count = 2 - name = "vm${count.index}" + name = "vm${count.index}" platform_id = "standard-v1" boot_disk { initialize_params { image_id = "fd87j6d92jlrbjqbl32q" # ubuntu 22.04 - size = 8 + size = 8 } } network_interface { subnet_id = yandex_vpc_subnet.subnet1.id - nat = true + nat = true } resources { core_fraction = 5 - cores = 2 - memory = 2 + cores = 2 + memory = 2 } - metadata = { user-data = "${file("users.yml")}" } + metadata = { ssh-keys = "ubuntu:${file("~/.ssh/id_ed25519.pub")}" } } resource "yandex_vpc_network" "network1" { @@ -44,9 +41,9 @@ resource "yandex_vpc_network" "network1" { } resource "yandex_vpc_subnet" "subnet1" { - name = "subnet1" - v4_cidr_blocks = [ "172.24.8.0/24" ] - network_id = yandex_vpc_network.network1.id + name = "subnet1" + v4_cidr_blocks = ["172.24.8.0/24"] + network_id = yandex_vpc_network.network1.id } resource "yandex_lb_target_group" "group1" { @@ -56,13 +53,13 @@ resource "yandex_lb_target_group" "group1" { for_each = yandex_compute_instance.vm content { subnet_id = yandex_vpc_subnet.subnet1.id - address = target.value.network_interface.0.ip_address + address = target.value.network_interface.0.ip_address } } } resource "yandex_lb_network_load_balancer" "balancer1" { - name = "balancer1" + name = "balancer1" deletion_protection = "false" listener { name = "my-lb1" @@ -83,13 +80,3 @@ 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 - } -}