3

I have a terraform template file that has a section like this to fill out host information. I'd like to add logic to it to only fill out this section if another variable, i.e. type = "ocp". So, if $type = "ocp", then run this loop. How would I do this?

ocp_hosts:
%{ for host in host_names }
  - name: ${host}
    device: "eth0"
    management:
      ip: ${ip_addrs[index(host_names, host)]}
%{ endfor }

I first tried adding an "if contains(${type}, "ocp")" to the for loop and I also tried adding in a condition block but I must not be formatting or understanding function correctly because it errors out on me.

1 Answer 1

2

I don't work with templates that much, but from the documentation, it seems this might work for you.

%{if type == "ocp" }
ocp_hosts:
%{ for host in host_names }
  - name: ${host}
    device: "eth0"
    management:
      ip: ${ip_addrs[index(host_names, host)]}
%{ endfor }
%{ endif }
Sign up to request clarification or add additional context in comments.

3 Comments

"from the documentation" - which one? Any link?
Directives - found this one in the documentation
It would help if we knew your variables and could give you a more precise example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.