cant get variable to show in template (erb)

62 views
Skip to first unread message

Adam Winberg

unread,
May 16, 2015, 1:46:42 PM5/16/15
to puppet...@googlegroups.com
Hi,

trying to populate an ifcfg template (network config) with ipv6 config, but cant get one of my variables to show. The one I want to use is "$ipv6_addr_cidr", but it just doesnt work, when my template is created the variable is not defined. I can see the 'notify' which I put right after defining $ipv6_addr_cidr, so I know I havent lost myself in my if statements. I have other variables in my network.pp (not shown below bc of snippet) which works.

What am i doing wrong? Suspect it's something trivial, but I just cant see it. Help would be really appreciated.


---------------------------------------
network.pp (snippet)

  ################
  # IPv6 stuff
  ################
  if str2bool($ipv6_enabled) {

    # Get the fourth octet from the node’s IPv4 address:
    $i4 = regsubst($ip,'^([0-9]+)[.]([0-9]+)[.]([0-9]+)[.]([0-9]+)$','\4')

    # Set ipv6 address space
    case $::network_eth0 {
      "172.31.28.0":  { $ipv6_space = "2001:67c:274:XXXX" }
      "172.31.24.0":  { $ipv6_space = "2001:67c:274:XXYY" }
      default:        { $ipv6_space = "undef" }
    }

    if $ipv6_space != "undef" {
      # Construct ipv6 address
      $ipv6_addr = "${ipv6_space}::${i4}"

       # Add network to address string, used in ifcfg template later
        $ipv6_addr_cidr = "${ipv6_addr}/64"
        notify { "ipv6 addr: $ipv6_addr_cidr":}
    }

  }
  ################
  # END IPv6 stuff
  ################

    file { "ifcfg-$device":
      path    => "$cfgdir/ifcfg-$device",
      owner   => root,
      group   => root,
      mode    => 644,
      ensure  => present,
      content => $ifcfg_template,
    }

---------------------------------------

ifcfg.erb
<% if @ipv6_addr_cidr -%>
#IPV6ADDR <%= @ipv6_addr_cidr %>
<% end -%>

---------------------------------------

The results of above code is just:
#IPV6ADDR

but no variable value.

Regards,
Adam

Felix Frank

unread,
May 17, 2015, 1:18:26 PM5/17/15
to puppet...@googlegroups.com
Hi,

can you move the notify next to the file resource instead of the variable declaration?

It seems most likely that there is some kind of scoping issue.

HTH,
Felix

Adam Winberg

unread,
May 17, 2015, 1:41:55 PM5/17/15
to puppet...@googlegroups.com
thanks for the tip, i tried and the notify outputs correct information (i.e the ipv6_addr_cidr variable value) even if put next to the file resource. But still no content in my template.

I've been looking up scoping but as far as I've found there is no special scoping in if statements, or conditionals of any kind. But i might have missed something (clearly I have).

//Adam

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/5558CD5B.2090600%40Alumni.TU-Berlin.de.
For more options, visit https://groups.google.com/d/optout.

Felix Frank

unread,
May 17, 2015, 2:22:53 PM5/17/15
to puppet...@googlegroups.com
Ah, sorry I missed that earlier: You are compiling the template
elsewhere, yes? Because the file resources uses the value of the
$ifcfg_template variable.

Please also debug the scope of the declaration of that variable. Is it a
parameter?

HTH,
Felix

Adam Winberg

unread,
May 17, 2015, 3:38:15 PM5/17/15
to puppet...@googlegroups.com
yes!

Man, did not think of that. Earlier in the network.pp i have:

  $ifcfg_template = template("ip/ifcfg.erb--TEMPLATE")

I dont recall why I did it like that to start with (the base code of this class is pretty old, just trying to add the ipv6 stuff now), but if i move the template call directly to the file resource's 'content' parameter it all works like i would expect.

Thank you for opening my eyes!

//Adam

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages