Variable interpolation failing with structured facts

761 views
Skip to first unread message

Thomas Hager

unread,
Nov 4, 2014, 10:16:55 AM11/4/14
to puppet...@googlegroups.com
Hi,

While implementing custom facts in our environment I did some
experimenting with structured
facts in our Puppet manifests. Puppet is 3.7.2, Facter is 2.3.0, Master
and Agent run on the
same VM, future parser is on and stringify_facts is disabled.

I failed at using one of my custom facts (an array of hashes) in Puppet
manifests,
so I did some tests with the "os" core fact and failed as well.

To debug my manifest I added these two notice() calls

notice($os[family])
notice("$os[family]")

and observed this output:

: (Scope(Class[Dummy])) RedHat
: (Scope(Class[Dummy])) {family => RedHat, name => CentOS, release =>
{major => 6, minor => 5, full => 6.5}}[family]

This is kinda weird, it seems as if Puppet fails to interpolate
structured facts in double-quoted strings,
because both calls to notice() should output the same text, right?
Or did I miss something?

Cheers,
Tom.

--
Thomas "Duke" Hager du...@sigsegv.at
GPG: 2048R/791C5EB1 http://www.sigsegv.at/gpg/duke.gpg
=================================================================
"Never Underestimate the Power of Stupid People in Large Groups."

jcbollinger

unread,
Nov 5, 2014, 9:53:38 AM11/5/14
to puppet...@googlegroups.com


On Tuesday, November 4, 2014 9:16:55 AM UTC-6, duke wrote:
Hi,

While implementing custom facts in our environment I did some
experimenting with structured
facts in our Puppet manifests. Puppet is 3.7.2, Facter is 2.3.0, Master
and Agent run on the
same VM, future parser is on and stringify_facts is disabled.

I failed at using one of my custom facts (an array of hashes) in Puppet
manifests,
so I did some tests with the "os" core fact and failed as well.

To debug my manifest I added these two notice() calls

notice($os[family])
notice("$os[family]")

and observed this output:

: (Scope(Class[Dummy])) RedHat
: (Scope(Class[Dummy])) {family => RedHat, name => CentOS, release =>
{major => 6, minor => 5, full => 6.5}}[family]

This is kinda weird, it seems as if Puppet fails to interpolate
structured facts in double-quoted strings,


Well no, Puppet is interpolating.  It is interpolating the string value of the whole hash.  What it is not doing is interpreting the index as an index.  Try this:

notice("${os[family]}")

I make it a general rule to always use braces around variable references in quoted strings, even where not strictly necessary.  It is harmless to do so, and it saves having to determine on a case by case basis whether braces are needed.


John

Thomas Hager

unread,
Nov 6, 2014, 7:18:37 AM11/6/14
to puppet...@googlegroups.com
On 2014-11-05 15:53, jcbollinger wrote:
> Well no, Puppet IS interpolating. It is interpolating the string value
> of the whole hash. What it is not doing is interpreting the index as
> an index. Try this:
>
> notice("${os[family]}")
>
> I make it a general rule to _always_ use braces around variable
> references in quoted strings, even where not strictly necessary. It is
> harmless to do so, and it saves having to determine on a case by case
> basis whether braces are needed.
Yes, of course, stupid me. Thanks for the heads up.

I wrote the test manifest properly and now the result is as expected.

Thx,
Tom.
Reply all
Reply to author
Forward
0 new messages