{"id":3598,"date":"2016-11-04T11:15:53","date_gmt":"2016-11-04T17:15:53","guid":{"rendered":"http:\/\/benincosa.com\/?p=3598"},"modified":"2016-11-04T11:15:53","modified_gmt":"2016-11-04T17:15:53","slug":"getting-around-terraform-indexing","status":"publish","type":"post","link":"https:\/\/benincosa.com\/?p=3598","title":{"rendered":"Getting around Terraform Indexing"},"content":{"rendered":"<p>Terraform has some good ways to do interpolation that can be simple. \u00a0However, I think as I&#8217;ve tried to make Terraform do more of what Ansible does, which might be outside its scope, I run into issues.<\/p>\n<p>One issue I had was creating a list of all the servers in the cluster that I wanted to define static routes for. \u00a0To do this I used Terraform&#8217;s formatlist built in function. \u00a0The issue I had was that I couldn&#8217;t get a good index on it. \u00a0<a href=\"https:\/\/github.com\/hashicorp\/terraform\/issues\/2769\">I searched and found this issue<\/a>. \u00a0Seems I&#8217;m not the only one with an issue! \u00a0So that is comforting. \u00a0So I fought with it for a few minutes then finally went to bed exhausted. \u00a0This morning I woke up renewed and thought of a great plan! \u00a0I love how your mind works while you sleep.<\/p>\n<p>I defined my compute node with a metadata to keep the count!<\/p>\n<pre class=\"lang:sh decode:true \">resource \"openstack_compute_instance_v2\" \"kube-worker\" {\r\n  name = \"${var.worker_name}${format(var.count_format, count.index+1) }\"\r\n  count = \"${var.worker_count}\"\r\n  image_name = \"${var.kube_image}\"\r\n  flavor_name = \"${var.kube_flavor}\"\r\n  metadata {\r\n    kube_role = \"worker\"\r\n    worker_number = \"${count.index}\"\r\n  }\r\n  network {\r\n    name = \"${var.network}\"\r\n  }\r\n  key_pair = \"${var.key_pair}\"\r\n  security_groups = [\"${var.security_group}\"]\r\n}<\/pre>\n<p>Notice in the metadata section above where I give each node a worker_number, which just corresponds to the count.<\/p>\n<p>Later, where I&#8217;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:<\/p>\n<pre class=\"lang:sh decode:true \">static_routes = \"${join(\"\\n\", formatlist(\"up route add -net %s.%s.0\/24 gw %s dev %s\",                       \r\n                      var.cluster_nets_prefix,                       \r\n                      openstack_compute_instance_v2.kube-worker.*.metadata.worker_number,                      \r\n                      openstack_compute_instance_v2.kube-worker.*.access_ip_v4, \r\n                      var.if_dev))}\"<\/pre>\n<p>This works great and gives me the count that I need!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Terraform has some good ways to do interpolation that can be simple. \u00a0However, I think as I&#8217;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&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[797,1001,846],"tags":[849,847,848,823],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3598"}],"collection":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3598"}],"version-history":[{"count":1,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3598\/revisions"}],"predecessor-version":[{"id":3599,"href":"https:\/\/benincosa.com\/index.php?rest_route=\/wp\/v2\/posts\/3598\/revisions\/3599"}],"wp:attachment":[{"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benincosa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}