added ability to supply additional data in the record name by using /

e.g. priority for MX records:
"@/50":server

removed .tfstate from repo
This commit is contained in:
Jan-Ole Hübner 2023-07-09 16:17:11 +02:00
commit d0494f5078
8 changed files with 144 additions and 0 deletions

7
.gitignore vendored Normal file
View file

@ -0,0 +1,7 @@
venv/
.idea/
*.tfstate
+.tfstate.backup
.terraform.lock.hcl
.terraform
auth.yaml

3
auth_sample.yaml Normal file
View file

@ -0,0 +1,3 @@
auth:
api_token: !add
email: !add

0
inputs.tf Normal file
View file

49
main.tf Normal file
View file

@ -0,0 +1,49 @@
provider "cloudflare" {
api_token = local.cloudflare_api.auth.api_token
}
locals {
cloudflare_api = yamldecode(file("${path.module}/auth.yaml"))
zones = yamldecode(file("${path.module}/zones.yaml"))
zone_data = flatten([
for zone_name, records in local.zones : [
for record_type, record_values in records : [
for record_name, record_value in record_values : {
zone_name = zone_name
record_type = upper(record_type)
record_value = record_value
extra_data = strcontains(record_name, "/") ? split( "/",record_name)[1] : ""
record_name = strcontains(record_name, "/") ? split( "/",record_name)[0] : record_name
}
]
]
])
}
data "cloudflare_zone" "zone" {
for_each = local.zones
name = each.key
}
resource "cloudflare_record" "myrecord" {
for_each = { for record in local.zone_data : "${record.record_type}${record.extra_data}-${record.record_name}.${record.zone_name}" => record }
zone_id = data.cloudflare_zone.zone[each.value.zone_name].id
name = "${each.value.record_name}.${each.value.zone_name}" == "@.${each.value.zone_name}" ? "${each.value.zone_name}" : "${each.value.record_name}.${each.value.zone_name}"
type = each.value.record_type
value = each.value.record_value
ttl = 300
priority = each.value.record_type == "MX" ? tonumber(each.value.extra_data) : 0
}

6
outputs.tf Normal file
View file

@ -0,0 +1,6 @@
output "zones" {
value = data.cloudflare_zone.zone
}
output "records" {
value = cloudflare_record.myrecord
}

0
variables.tf Normal file
View file

8
versions.tf Normal file
View file

@ -0,0 +1,8 @@
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4"
}
}
}

71
zones.yaml Normal file
View file

@ -0,0 +1,71 @@
jan-ole.cloud:
a:
"*": 138.201.67.182
"@": 138.201.67.182
www: 138.201.67.182
txt:
"*": v=spf1 a mx include:ole.click -all
"www": v=spf1 a mx include:ole.click -all
jan-ole.link:
a:
"@": 46.38.249.190
www: 46.38.249.190
webmail: 46.38.249.190
mail: 46.38.249.160
aaaa:
"@": 2a03:4000:61:3f85::18:3398
"www": 2a03:4000:61:3f85::18:3398
cname:
autoconfig: autoconfig.netcup.net
key1_domainkey: key1._domainkey.webhosting.systems
key2_domainkey: key2._domainkey.webhosting.systems
txt:
"@": v=spf1 mx a include:_spf.webhosting.systems ~all
mx:
"@/10": mail.jan-ole.link
"@/50": mxf9a0.netcup.net
ole.click:
a:
"@": 46.38.249.190
www: 46.38.249.190
webmail: 46.38.249.190
mail: 46.38.249.160
share: 46.38.249.190
aaaa:
"@": 2a03:4000:61:3f85::18:3398
"www": 2a03:4000:61:3f85::18:3398
cname:
autoconfig: autoconfig.netcup.net
key1_domainkey: key1._domainkey.webhosting.systems
key2_domainkey: key2._domainkey.webhosting.systems
txt:
"@": v=spf1 mx a include:_spf.webhosting.systems ~all
mx:
"@/10": mail.ole.click
"@/50": mxf9a0.netcup.net
krewella.rocks:
a:
"@": 46.38.249.190
www: 46.38.249.190
webmail: 46.38.249.190
mail: 46.38.249.160
aaaa:
"@": 2a03:4000:61:3f85::18:3398
"www": 2a03:4000:61:3f85::18:3398
cname:
autoconfig: autoconfig.netcup.net
key1_domainkey: key1._domainkey.webhosting.systems
key2_domainkey: key2._domainkey.webhosting.systems
txt:
"@": v=spf1 mx a include:_spf.webhosting.systems ~all
mx:
"@/10": mail.krewella.rocks
"@/50": mxf9a0.netcup.net
jan-ole.tech:
a:
"@": 192.168.84.1
www: 192.168.84.1