Terraform modules
main.tf |
Generated files |
---|---|
resource "tls_private_key" "host" { algorithm = "ED25519" } resource "hcloud_ssh_key" "loginUser" { name = "devops" public_key = file("~/.ssh/id_ed25519.pub") } resource "hcloud_server" "server" { name = "www" ... } resource "local_file" "ssh_script" { content = templatefile("tpl/ssh.sh", { ip = hcloud_server.helloServer.ipv4_address devopsUsername = hcloud_ssh_key.loginUser.name }) filename = "bin/ssh" depends_on = [local_file.known_hosts] } |
|
main.tf |
Using a module |
---|---|
resource "local_file" "known_hosts" { ... } resource "local_file" "ssh_script" { ... } resource "local_file" "scp_script" { ... } |
module "localfiles" { source = "../modules/localfiles" ipv4 = hcloud_server.webServer.ipv4_address dnsZone = var.dnsZone hostNames = ["www", "cloud"] loginUser = hcloud_ssh_key.loginUser.name hostKey = tls_private_key.host.public_key_openssh } |
variable "ipv4" { description = "The server's IPV4 address e.g. '141.62.1.5'" type = string } variable "hostNames" { description = "Set of unique local host names e.g. [\"www\", \"cloud\"] " type = list } ... resource "local_file" "known_hosts" {...
Switching between parent and child module context by ${path.module}:
resource "local_file" "ssh_script" { content = templatefile("${path.module}/tpl/ssh.sh", { serverFqdn = "${var.ostNames}.${var.dnsZone}" devopsUsername = var.loginUser }) filename = "bin/ssh" file_permission = "755" depends_on = [local_file.known_hosts] }
No. 23
A module for local file generation
Q: |
Complete the idea of Figure 1061, “Local file generation by module ” defining
a module for generating |