Ask Your Question
0

How to set a ruby variable on erb template

asked 2016-03-11 09:13:43 -0500

ZeMiw gravatar image

Hello,

I'm trying to do set a variable on erb template. I explain with an example.

I create a vhost file for apache.

I have many server level : dev / int / prd, I set the $server_level on nodes.pp

And I want set the vhost IP on the erb template. The IP depends of the level, and the IP is set by facter. Like this :

<% if @server_level == "dev" %> <% @IP_site=@ipaddress_eth0%><% end %>
<% if @server_level == "int" %> <% @IP_site=@ipaddress_eth1%><% end %>
<% if @server_level == "prd" %> <% @IP_site=@ipaddress_eth2%><% end %>

Goal is to use on the erb file the IP_site variable and not do this if statement each time I want to print the IP.

But the expression I wrote before is wrong and I'm not à rubyman... Can you help me?

Thanks a lot!

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2016-03-11 22:48:52 -0500

I tested that and it works fine:

$server_level = 'dev'
$ipaddress_eth0 = '1.1.1.1'
$content = inline_template('
<% if @server_level == "dev" %> <% @IP_site=@ipaddress_eth0%><% end %>
<% if @server_level == "int" %> <% @IP_site=@ipaddress_eth1%><% end %>
<% if @server_level == "prd" %> <% @IP_site=@ipaddress_eth2%><% end %>
<%= @IP_site %>
')
notify { $content: }

I then cleaned it up as:

$server_level = 'dev'
$ipaddress_eth0 = '1.1.1.1'
$content = inline_template('<% if @server_level == "dev" -%><% @IP_site=@ipaddress_eth0 -%><% end -%>
<% if @server_level == "int" -%><% @IP_site=@ipaddress_eth1 -%><% end -%>
<% if @server_level == "prd" -%><% @IP_site=@ipaddress_eth2 -%><% end -%>
<%= @IP_site -%>')
notify { $content: }

And apply:

[root@centos-72-x64 ~]# puppet apply /tmp/foo.pp 
Notice: Compiled catalog for centos-72-x64 in environment production in 0.05 seconds
Notice: 1.1.1.1
Notice: /Stage[main]/Main/Notify[1.1.1.1]/message: defined 'message' as '1.1.1.1'
Notice: Applied catalog in 0.07 seconds
edit flag offensive delete link more
1

answered 2016-03-15 08:54:47 -0500

ZeMiw gravatar image

Hum, I'm confused, I copy your example and it's working fine... I think I failed when I print the value. I don't know.

By the way, many thanks Alex, you solve my issue !

edit flag offensive delete link more

Comments

No worries, glad it's sorted out.

Alex Harvey gravatar imageAlex Harvey ( 2016-03-15 09:12:49 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-03-11 09:13:43 -0500

Seen: 643 times

Last updated: Mar 15 '16