#terraform #terraform-template-file
#terraform #terraform-template-file
Вопрос:
Возможно ли создать файлы ssh config или ansible hosts с помощью функции terraform templatefile? Использование Terraform 0.12.
Определение Terraform:
resource "local_file" "ansible_hosts_file" {
content = templatefile("templates/ansible_hosts.tpl", {
jumphost_fip = openstack_networking_floatingip_v2.dth_fip.address,
node_name = module.app-cluster.app_cluster_compute.*.name,
node_ip = module.app-cluster.app_cluster_compute.*.access_ip_v4
})
filename = "../config/inventory/hosts"
}
resource "local_file" "ssh_config_file" {
content = templatefile("templates/ssh_config.tpl", {
jumphost_fip = openstack_networking_floatingip_v2.dth_fip.address,
node_name = module.app-cluster.app_cluster_compute.*.name,
node_ip = module.app-cluster.app_cluster_compute.*.access_ip_v4
})
filename = "../config/inventory/ssh_config"
}
Моя проблема в том, что я понятия не имею, как должны выглядеть файлы шаблонов, особенно интерполяция или директивы..
ansible_hosts.tpl
jumphost ansible_host=${jumphost_fip}
[app_cluster]
%{ for node in node_name ~} # ???????????? These 3 lines don't work of course
${node} ansible_host=${node_ip} # ???????????? so could this be somehow specified
%{ endfor ~} # ???????????? that node and node_ip are filled in correctly?
[app_cluster:vars]
ansible_ssh_common_args=' -o ProxyCommand="ssh -W %h:%p -q jumphost"'
[all:vars]
ansible_user=ubuntu
ssh_config.tpl
Host *
StrictHostKeyChecking no
Host jumphost
Hostname ${jumphost_ip}
User ubuntu
%{ for node in node_name ~} # ???????????? Similar issue here
Host ${node} # ????????????
Hostname ${node_ip} # ????????????
User ubuntu
ProxyCommand ssh -A jumphost -W %h:%p
%{ endfor ~}
Файл ansible hosts и конфигурация ssh должны выглядеть следующим образом:
ansible_hosts
jumphost ansible_host=10.200.100.100
[app_cluster]
app-node01 ansible_host=192.168.0.3
app-node02 ansible_host=192.168.0.5
[app_cluster:vars]
ansible_ssh_common_args=' -o ProxyCommand="ssh -W %h:%p -q jumphost"'
[all:vars]
ansible_user=ubuntu
ssh_config
Host *
StrictHostKeyChecking no
Host jumphost
Hostname 10.200.100.100
User ubuntu
Host app-node01
Hostname 192.168.0.3
User ubuntu
ProxyCommand ssh -A jumphost -W %h:%p
Host app-node02
Hostname 192.168.0.5
User ubuntu
ProxyCommand ssh -A jumphost -W %h:%p
Ответ №1:
[индекс] является ключом к решению в файлах шаблонов:
ansible_hosts.tpl
%{ for index, node in node_name ~}
${node} ansible_host=${node_ip[index]}
%{ endfor ~}
ssh_config.tpl
%{ for index, node in node_name ~}
Host ${node}
Hostname ${node_ip[index]}
User ubuntu
ProxyCommand ssh -A jumphost -W %h:%p
%{ endfor ~}
Источник — https://www.linkbynet.com/produce-an-ansible-inventory-with-terraform