0

I'm sure the answer to this is straightforward, but I can't find out how to do this. I have a line in my erb template file that needs to write exactly as-is into a server's config file (for OpenVPN):

push "dhcp-option DNS 192.16.23.12"

I need to change this so that the ip address gets looked up from the facter listing on the server, rather than having it hard-coded, as this conf file needs to be used on different servers now. The fact holding this ip address is "ipaddress_tun0". I've tried putting it into my line, thus:

push "dhcp-option DNS <%= @ipaddress_tun0 %>"

.. but the fact doesn't get interpolated properly, presumably because of the double quotes. Unfortunately the double quotes are needed as a literal part of the line in the conf file, so they have to be there (single quotes don't work). Can anyone advise how I can get this line to look up and insert the value of ipaddress_tun0, please?

HelenH
  • 55
  • 3
  • 8
  • `push "dhcp-option DNS <%= @ipaddress_tun0 %>"` works for me no problem. Have you tried running `facter ipaddress_tun0` on the machine to make sure it's actually a fact? Or just run `facter` to see what your options for facts are, and make sure you have the correct fact name/spelling. – drewyupdrew Apr 19 '16 at 14:20
  • 1
    Thanks, it's definitely a fact (just double-checked) and the fact is correctly spelled. In the end a colleague suggested I needed to use scope.lookupvar, so: push "dhcp-option DNS <%= scope.lookupvar('ipaddress_tun0') %>" and this seems to work. – HelenH Apr 19 '16 at 14:47
  • Ah I see, seems like the `ipaddress_tun0` was out of your current scope. Thanks, I learned something too. Glad you got it to work! – drewyupdrew Apr 19 '16 at 14:57

1 Answers1

1
push "dhcp-option DNS <%= scope.lookupvar('ipaddress_tun0') %>"   

was what was needed, as my fact was out of the current scope.

HelenH
  • 55
  • 3
  • 8
  • This became important in Puppet 4, as they changed the scoping rules so that only vars inside the current scope can be referenced with `@variable` [docs](https://docs.puppet.com/puppet/latest/reference/lang_scope.html) – jaxxstorm Apr 20 '16 at 14:19
  • 1
    Are you sure, @Frap? Your claim seems to conflict with [the current (v4.4) documentation](https://docs.puppet.com/puppet/latest/reference/lang_template_erb.html#variable): "All variables in the current scope (including global variables) are passed to templates as Ruby instance variables, which begin with 'at' signs (@)." IOW, "in the current scope" is not the same thing as "local to the innermost current scope". – John Bollinger Apr 22 '16 at 14:00