Terraform modules
# main.tf
resource "hcloud_server" "myServer" { ... }
resource "local_file" "hostdata" {
content = templatefile("Tpl/hostdata.json", {
ip4 = hcloud_server.helloServer.ipv4_address
ip6 = hcloud_server.helloServer.ipv6_address
location = hcloud_server.helloServer.location
})
filename = "Gen/hostdata.json"
}{ "network": {
"ipv4": "${ip4}","ipv6": "${ip6}"},
"location": "${location}"} |
{
"network": {
"ipv4": "157.180.78.16",
"ipv6": "2a01:4f9:c013:be69::1"
},
"location": "hel1"
} |
├── CreateHostByModule │ ├── main.tf │ ├── variables.tf │ └── Gen | └── hostdata.json ◀━━━┓ └── Modules ┃ └── HostMetaData ┃ ├── main.tf ┃ ├── outputs.tf ┃ ├── providers.tf ┃ ├── Tpl ┃ │ └── hostdata.json ━┛ └── variables.tf
# variable.tf
variable "hcloud_token" { sensitive = true }# main.tf
module "createHostAmongMetaData" {
source = "../Modules/HostMetaData"
name = "myServer"
hcloud_token = var.hcloud_token
}# variable.tf variable "name" { type = string nullable = false } variable "hcloud_token" { type = string nullable = false } |
# main.tf
resource "hcloud_server" "newServer" {
name = var.name
image = "debian-13"
server_type = "cx23" }
resource "local_file" "hostdata" {
content = templatefile("${path.module}/Tpl/hostdata.json", {
ip4 = hcloud_server.newServer.ipv4_address
ip6 = hcloud_server.newServer.ipv6_address
location = hcloud_server.newServer.location
})
filename = "Gen/${var.name}.json" } |
{
"network": {
"ipv4": "${ip4}",
"ipv6": "${ip6}"
},
"location": "${location}"
} |
{
"network": {
"ipv4": "157.180.78.16",
"ipv6": "2a01:4f9:c013:be69::1"
},
"location": "hel1"
} |
resource "local_file" "hostdata" { content = templatefile( "${path.module}/Tpl/hostdata.json",{ ip4 = hcloud_server.newServer.ipv4_address ip6 = hcloud_server.newServer.ipv6_address location = hcloud_server.newServer.location }) filename = "Gen/${var.name}.json" } |
├── CreateHostByModule │ ├── main.tf │ ├── variables.tf │ └── Gen | └── hostdata.json ◀━━━┓ └── Modules ┃ └── HostMetaData ┃ ├── main.tf ┃ ├── outputs.tf ┃ ├── providers.tf ┃ ├── Tpl ┃ │ └── hostdata.json ━┛ └── variables.tf |
No. 17
Generating host meta data
|
Q: |
Implement the module based host creation including meta data generation as being outlined in Figure 1064, “Parent module vs. sub module context ”. |
No. 18
A module for ssh host key handling
|
Q: |
Create a SshKnownHosts module for generating
...
resource "hcloud_server" "helloServer" {
name = "hello"
...
}
module "createSshKnownHosts" {
source = "../Modules/SshKnownHosts"
loginUserName = hcloud_ssh_key.loginUser.name
serverNameOrIp = hcloud_server.helloServer.ipv4_address
serverHostPublicKey = tls_private_key.host.public_key_openssh
}Your overall project layout may look like: . ├── KnownHostsByModule │ ├── bin │ │ ├── scp ━━┓ │ │ └── ssh━┓ ┃ depend │ ├── gen ▼ ▼ │ │ └── known_hosts │ ├── main.tf │ ├── network.tf │ ├── outputs.tf │ ├── providers.tf │ ├── tpl │ │ └── userData.yml │ └── variables.tf └── Modules └── SshKnownHosts ├── main.tf ├── tpl │ ├── scp.sh │ └── ssh.sh └── variables.tf Red colour indicates generated resources: As in Solving the |
