Terraform 模板文件功能 - ssh 配置或 ansible 主机文件创建
Terraform templatefile function - ssh config or ansible hosts files creation
是否可以使用 terraform 模板文件功能创建 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
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 主机文件和 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
[index]是模板文件中解决方案的关键:
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
是否可以使用 terraform 模板文件功能创建 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
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 主机文件和 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
[index]是模板文件中解决方案的关键:
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