1
0
Fork 0

use local_file for ansible inventory

This commit is contained in:
Von Random 2025-02-16 22:13:44 +02:00
parent 1920af5bc4
commit 75aeaf38a9
5 changed files with 24 additions and 30 deletions

1
.gitignore vendored
View file

@ -1 +0,0 @@
ansible/id_ed25519

View file

@ -1,7 +1,7 @@
[defaults] [defaults]
inventory = inventory.yml inventory = inventory.ini
interpreter_python = /usr/bin/python3 interpreter_python = /usr/bin/python3
remote_user = andrei remote_user = ubuntu
private_key_file = ~/.ssh/id_ed25519_yatf private_key_file = ~/.ssh/id_ed25519
host_key_checking = False host_key_checking = False

View file

@ -1,2 +0,0 @@
plugin: cloud.terraform.terraform_provider
project_path: ../terraform

10
terraform/ansible.tf Normal file
View file

@ -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
}

View file

@ -3,9 +3,6 @@ terraform {
yandex = { yandex = {
source = "yandex-cloud/yandex" source = "yandex-cloud/yandex"
} }
ansible = {
source = "ansible/ansible"
}
} }
} }
@ -16,27 +13,27 @@ provider "yandex" {
resource "yandex_compute_instance" "vm" { resource "yandex_compute_instance" "vm" {
count = 2 count = 2
name = "vm${count.index}" name = "vm${count.index}"
platform_id = "standard-v1" platform_id = "standard-v1"
boot_disk { boot_disk {
initialize_params { initialize_params {
image_id = "fd87j6d92jlrbjqbl32q" # ubuntu 22.04 image_id = "fd87j6d92jlrbjqbl32q" # ubuntu 22.04
size = 8 size = 8
} }
} }
network_interface { network_interface {
subnet_id = yandex_vpc_subnet.subnet1.id subnet_id = yandex_vpc_subnet.subnet1.id
nat = true nat = true
} }
resources { resources {
core_fraction = 5 core_fraction = 5
cores = 2 cores = 2
memory = 2 memory = 2
} }
metadata = { user-data = "${file("users.yml")}" } metadata = { ssh-keys = "ubuntu:${file("~/.ssh/id_ed25519.pub")}" }
} }
resource "yandex_vpc_network" "network1" { resource "yandex_vpc_network" "network1" {
@ -44,9 +41,9 @@ resource "yandex_vpc_network" "network1" {
} }
resource "yandex_vpc_subnet" "subnet1" { resource "yandex_vpc_subnet" "subnet1" {
name = "subnet1" name = "subnet1"
v4_cidr_blocks = [ "172.24.8.0/24" ] v4_cidr_blocks = ["172.24.8.0/24"]
network_id = yandex_vpc_network.network1.id network_id = yandex_vpc_network.network1.id
} }
resource "yandex_lb_target_group" "group1" { resource "yandex_lb_target_group" "group1" {
@ -56,13 +53,13 @@ resource "yandex_lb_target_group" "group1" {
for_each = yandex_compute_instance.vm for_each = yandex_compute_instance.vm
content { content {
subnet_id = yandex_vpc_subnet.subnet1.id 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" { resource "yandex_lb_network_load_balancer" "balancer1" {
name = "balancer1" name = "balancer1"
deletion_protection = "false" deletion_protection = "false"
listener { listener {
name = "my-lb1" 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
}
}