Terraform has some good ways to do interpolation that can be simple. However, I think as I’ve tried to make Terraform do more of what Ansible does, which might be outside its scope, I run into issues.
One issue I had was creating a list of all the servers in the cluster that I wanted to define static routes for. To do this I used Terraform’s formatlist built in function. The issue I had was that I couldn’t get a good index on it. I searched and found this issue. Seems I’m not the only one with an issue! So that is comforting. So I fought with it for a few minutes then finally went to bed exhausted. This morning I woke up renewed and thought of a great plan! I love how your mind works while you sleep.
I defined my compute node with a metadata to keep the count!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
resource "openstack_compute_instance_v2" "kube-worker" { name = "${var.worker_name}${format(var.count_format, count.index+1) }" count = "${var.worker_count}" image_name = "${var.kube_image}" flavor_name = "${var.kube_flavor}" metadata { kube_role = "worker" worker_number = "${count.index}" } network { name = "${var.network}" } key_pair = "${var.key_pair}" security_groups = ["${var.security_group}"] } |
Notice in the metadata section above where I give each node a worker_number, which just corresponds to the count.
Later, where I’m going through and creating a template with a list of all the servers I used this bad boy variable to give me the iteration number:
1 2 3 4 5 |
static_routes = "${join("\n", formatlist("up route add -net %s.%s.0/24 gw %s dev %s", var.cluster_nets_prefix, openstack_compute_instance_v2.kube-worker.*.metadata.worker_number, openstack_compute_instance_v2.kube-worker.*.access_ip_v4, var.if_dev))}" |
This works great and gives me the count that I need!