2

I'm trying to write an if statement that compares two variables in a puppet erb template:

<% @array_of_ip_addresses.each_with_index do |ip, idx| -%>
  <% if @ip == @ipaddress_eth0 -%>
    <%= "doing something with #{idx}" -%>
  <% end -%>
<% end -%>

I cannot figure out why, but the condition on my if statement keeps returning false (needless to say I confirmed there should be a match).

What dumb thing am I missing?

1 Answer 1

2

ip is a block scope variable and not a variable instantiated from the invoking code (in this case, Puppet's template function), so you should not specify it as a class instance variable with @. When you remove that and specify it as a block scope variable ip, the template will look like:

<% @array_of_ip_addresses.each_with_index do |ip, idx| -%>
  <% if ip == @ipaddress_eth0 -%>
    <%= "doing something with #{idx}" -%>
  <% end -%>
<% end -%>

Using the class instance variable @ip would likely result in a resolution of nil, which would definitely almost always be false as nil != @ipaddress_eth0 unless Facter failed to resolve a value for your eth0 ipaddress, which would be quite uncommon (but still possible).

Sign up to request clarification or add additional context in comments.

1 Comment

Dude, you rock!

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.