Terraform templatefile函数-ssh配置或ansible主机文件创建

Terraform templatefile函数-ssh配置或ansible主机文件创建,terraform,terraform-template-file,Terraform,Terraform Template File,是否可以使用terraform templatefile函数创建ssh配置或ansible主机文件? 使用Terraform 0.12 地形定义: resource "local_file" "ansible_hosts_file" { content = templatefile("templates/ansible_hosts.tpl", { jumphost_fip = openstack_networking_f

是否可以使用terraform templatefile函数创建ssh配置或ansible主机文件? 使用Terraform 0.12

地形定义:

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
%{ for index, node in node_name ~}
${node} ansible_host=${node_ip[index]}
%{ endfor ~}
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 ~}
%{ for index, node in node_name ~}
Host ${node}
    Hostname ${node_ip[index]}
    User ubuntu
    ProxyCommand ssh -A jumphost -W %h:%p
%{ endfor ~}
ansible主机文件和ssh配置应如下所示:

ansible_主机

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_配置

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

[index]是模板文件中解决方案的关键:

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
%{ for index, node in node_name ~}
${node} ansible_host=${node_ip[index]}
%{ endfor ~}
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 ~}
%{ for index, node in node_name ~}
Host ${node}
    Hostname ${node_ip[index]}
    User ubuntu
    ProxyCommand ssh -A jumphost -W %h:%p
%{ endfor ~}

源-

[index]是模板文件中解决方案的关键:

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
%{ for index, node in node_name ~}
${node} ansible_host=${node_ip[index]}
%{ endfor ~}
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 ~}
%{ for index, node in node_name ~}
Host ${node}
    Hostname ${node_ip[index]}
    User ubuntu
    ProxyCommand ssh -A jumphost -W %h:%p
%{ endfor ~}
来源-