title: Comparision of numeric values with two variables in Terraform templatefile template
body: <p>I'm trying to create a rendered file with Terraform's templatefile function.</p>
  <p>Here is my code:</p>
  <pre><code>variable &quot;prefix&quot; {
    default = &quot;k8s&quot;
  }
  
  variable &quot;masters_count&quot; {
    default = 2
  }
  
  resource &quot;google_compute_address&quot; &quot;k8s-static-ips&quot; {
    name = &quot;${var.prefix}-${count.index}&quot;
    count = var.num_k8s_nodes
  }
  
  locals {
    ips = {
      for k in google_compute_address.k8s-static-ips : k.name =&gt; k.address
    }
  }
  
  resource &quot;local_file&quot; &quot;AnsibleInventory&quot; {
  
    content = templatefile(&quot;inventory.ini.tmpl&quot;,
    {
      private = local.ips
      prefix = var.prefix
      masters_count = var.masters_count
    }
    )
    filename = &quot;inventory.ini&quot;
  }
  </code></pre>
  <pre><code>[all]
  %{ for index, ip in private ~}
  ${prefix}-${index} ansible_host=${ip} 
  %{ endfor ~}
  
  [kube-master]
  %{ for index, ip in private ~}
  %{ if index &lt; masters_count ~}${prefix}-${index}%{ endif ~}
  %{ endfor ~}
  
  [calico-rr]
  
  [k8s-cluster:children]
  kube-master
  kube-node
  calico-rr
  </code></pre>
  <p>I have the problem that the <code>%{ if index &lt; masters_count ~}</code> condition does not work, I am getting the Terraform error:</p>
  <pre><code>Call to function &quot;templatefile&quot; failed: inventory.ini.tmpl:11,7-12: Invalid
  operand; Unsuitable value for left operand: a number is required., and 4 other
  diagnostic(s).
  </code></pre>
  <p>Looks like it doesn't know the index variable in the if block.
  How can I make the comparision of that line possible accessing the index variable?</p>