commit d0494f5078a3b460c3ee594d18c856a990d3d331 Author: Jan-Ole Hübner Date: Sun Jul 9 16:17:11 2023 +0200 added ability to supply additional data in the record name by using / e.g. priority for MX records: "@/50":server removed .tfstate from repo diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b12885a --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +venv/ +.idea/ +*.tfstate ++.tfstate.backup +.terraform.lock.hcl +.terraform +auth.yaml diff --git a/auth_sample.yaml b/auth_sample.yaml new file mode 100644 index 0000000..0f62506 --- /dev/null +++ b/auth_sample.yaml @@ -0,0 +1,3 @@ +auth: + api_token: !add + email: !add \ No newline at end of file diff --git a/inputs.tf b/inputs.tf new file mode 100644 index 0000000..e69de29 diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..eba9aee --- /dev/null +++ b/main.tf @@ -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 +} + + + + + + + + + + diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..9848c28 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,6 @@ +output "zones" { + value = data.cloudflare_zone.zone +} +output "records" { + value = cloudflare_record.myrecord +} diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..e69de29 diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..f828980 --- /dev/null +++ b/versions.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + cloudflare = { + source = "cloudflare/cloudflare" + version = "~> 4" + } + } +} \ No newline at end of file diff --git a/zones.yaml b/zones.yaml new file mode 100644 index 0000000..2f21532 --- /dev/null +++ b/zones.yaml @@ -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 \ No newline at end of file