Search code examples
rubyregexlinuxpuppet

puppet inline_template ignoring awk


I have following code in puppet and when i run it is ignoring awk filter But if i use cut -d ' ' -f8 it works!

$hugepage=inline_template("<%= `grep Hugepagesize /proc/meminfo | awk '{print $2}'` %>")
notify {"Variable testing, hugepage size is ${hugepage}":}

Result following:

Notice: /Stage[main]/Sysctl::Pgsql/Notify[Variable testing, hugepage size is Hugepagesize:       2048 kB

Why it is printing Hugepagesize: 2048 kB? look like awk not working :(

On irb shell its working.

irb(main):002:0> `grep Hugepagesize /proc/meminfo | awk '{print $2}'`
=> "2048\n"

UPDATE

I tried following also but same result :(

$hugepage = generate("/bin/sh","-c", "/bin/grep Hugepagesize /proc/meminfo | /bin/awk '{print $2}'")

Solution

  • You are passing a quotation-mark-delimited string to either inline_template() or to generate(). Puppet will interpolate variable references it finds within; in particular, it will interpolate the value of variable $2. Supposing that that variable is undefined, an empty string will be interpolated. The result is then identical to

    $hugepage=inline_template("<%= `grep Hugepagesize /proc/meminfo | awk '{print }'` %>")
    

    which is precisely what you observe. To avoid this, you can escape the $, or you can change the outermost quotes to apostrophes (requiring you to do something about the inner apostrophes). I'd probably choose the former:

    $hugepage=inline_template("<%= `grep Hugepagesize /proc/meminfo | awk '{print \$2}'` %>")