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]
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

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 = {
source = "yandex-cloud/yandex"
}
ansible = {
source = "ansible/ansible"
}
}
}
@ -36,7 +33,7 @@ resource "yandex_compute_instance" "vm" {
memory = 2
}
metadata = { user-data = "${file("users.yml")}" }
metadata = { ssh-keys = "ubuntu:${file("~/.ssh/id_ed25519.pub")}" }
}
resource "yandex_vpc_network" "network1" {
@ -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
}
}