jebjeb
jebjeb

Reputation: 125

Calling multiple variables in chef code

I have the following set in my attributes file

default[:iptables][:ports] = [22,21]
default[:iptables][:ubnet] = ["192.168.0.0/24"]

I have the following in my template.erb file

-A INPUT -i lo -j ACCEPT

<%- node[:iptables][:ports].each do |port| -%>

-A INPUT -m state --state NEW -m tcp -p tcp --dport <%= port %> -j ACCEPT

<%- end -%>

<% node[:iptables][:subnet].each do |subnet| -%>

-A INPUT -m state --state NEW -s <%= subnet %> -m tcp -p tcp --dport <%= port %> -j ACCEPT

<%- end -%>

I want to end up with something like this when i run the code

-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT

-A INPUT -m state --state NEW -s 192.168.0.0/16 -m tcp -p tcp --dport  25 -j ACCEPT

Unfortunately when i run the code as is, i get the following error

Chef::Mixin::Template::TemplateError (undefined local variable or method 'port' for #<Chef::Mixin::Template::TemplateContext:0x00000006e7cd80>)

The first half of the code works as expected but the second block is the one giving me issues. I also do not want to hard code port 25. Can someone help me rewrite this?

Upvotes: 0

Views: 304

Answers (1)

coderanger
coderanger

Reputation: 54249

In the subnet line you put --dport <%= port %> but don't indicate where the 25 is supposed to come from.

All data has to come from somewhere, either put it in the template directly, or in node attribute data and then reference it in the template.

Upvotes: 1

Related Questions