我想按值填充模板,但我的内容与模板相同。
文件中的结果应该是这样的hosts.yaml
。模板尚未完成,将进行编辑。
rnd_hetzner:
hosts:
rnd.test:
ansible_port: 22
ansible_host: 1.1.2.3
模板:
# Ansible Inventory
---
all:
hosts:
{% for instance, details := instances_ips.value %}
{{ instance }}:
ansible_host: {{ details.external_ip }}
{% endfor %}
children:
all_servers:
hosts:
{% for instance, _ := instances_ips.value %}
{{ instance }}
{% endfor %}
地形
locals {
instances_ips = {
"value" : {
"app-server" : {
"external_ip" : "1.27.1.1"
},
"db-server" : {
"external_ip" : "1.24.9.1"
}
}
}
}
resource "local_file" "ansible_inventory" {
content = templatefile("${path.module}/templates/ansible_inventory.tpl",
{
instances_ips = jsonencode(local.instances_ips)
}
)
filename = "${path.module}/templates/hosts.yaml"
}