Possible Bug? - Array Variable + Double Quotes

126 views
Skip to first unread message

Ron

unread,
Aug 13, 2012, 7:47:50 AM8/13/12
to puppet...@googlegroups.com
All, I am trying to have a unique type name be having "${username}-${dotfile}" where $dotfile is an array of filenames.
This is based on the Puppet 2.7 Cookbook to manage user dotfiles and ssh keys.

I opened a bug here:
http://projects.puppetlabs.com/issues/15919

I have a github gist here:

Any insight would be greatly appreciated.

Thanks,
Ron

Nan Liu

unread,
Aug 13, 2012, 2:20:27 PM8/13/12
to puppet...@googlegroups.com
Puppet notify resource converts the array to a string. It's not that
the variable isn't an array, but you are looking at the .to_s output.
The following example should demonstrate this better:

var = [1, 2]
notice("user-${var}")
$arr = inline_template("<%= var.inspect %>")
notice("user-${arr}")

notice: Scope(Class[main]): user-12
notice: Scope(Class[main]): user-["1", "2"]
notice: Finished catalog run in 0.04 seconds

A common misconception, but most people expect in puppet string+array
results in an array value with the string prefix every array element,
what you get is actually string + array.to_s. I haven't seen the
cookbook recipe, but I think what you meant is something closer to
this:

https://gist.github.com/7860c7d8c157432381e3

The crazy parsejson (from stdlib) inline_template is just getting
around inline_template returning string. That should be rewritten as a
puppet function. Anyhow the manifests should give an idea what you are
trying to do.

Nan

jcbollinger

unread,
Aug 13, 2012, 5:14:46 PM8/13/12
to puppet...@googlegroups.com


On Monday, August 13, 2012 1:20:27 PM UTC-5, Nan Liu wrote:

A common misconception, but most people expect in puppet string+array
results in an array value with the string prefix every array element,
what you get is actually string + array.to_s.

Yes, we seem to have seen a lot of that around here recently.  We also sometimes see the closely related mistake of using "$my_array" instead of simply $my_array.  I suppose it's a sign that there is interest in combinatorial approaches to declaring resources.

 
I haven't seen the
cookbook recipe, but I think what you meant is something closer to
this:

https://gist.github.com/7860c7d8c157432381e3


For cases such as this one, where decorated versions of the elements of a single array are desired, the built-in regsubst() function can do the job relatively easily.  Note that the result should be assigned to a new variable, as function return values cannot directly be used as resource titles.


John

Ron

unread,
Aug 13, 2012, 5:23:17 PM8/13/12
to puppet...@googlegroups.com


On Monday, August 13, 2012 5:14:46 PM UTC-4, jcbollinger wrote:


On Monday, August 13, 2012 1:20:27 PM UTC-5, Nan Liu wrote:

A common misconception, but most people expect in puppet string+array
results in an array value with the string prefix every array element,
what you get is actually string + array.to_s.

Yes, we seem to have seen a lot of that around here recently.  We also sometimes see the closely related mistake of using "$my_array" instead of simply $my_array.  I suppose it's a sign that there is interest in combinatorial approaches to declaring resources.

Yeah the reason I need the $name-$dotfile is to ensure they are unique. That said, without the quotes or the requirement for $name then it would work just fine as $dotfile.
 

 
I haven't seen the
cookbook recipe, but I think what you meant is something closer to
this:

https://gist.github.com/7860c7d8c157432381e3


For cases such as this one, where decorated versions of the elements of a single array are desired, the built-in regsubst() function can do the job relatively easily.  Note that the result should be assigned to a new variable, as function return values cannot directly be used as resource titles.


How would I go about running a substring on something without a delimeter. Maybe I am just missing something obvious here.
 

John

Ron

unread,
Aug 13, 2012, 5:44:58 PM8/13/12
to puppet...@googlegroups.com
HUGE Thanks to Nan Liu!

Here is my updated gist on github. This is now working and in production.
Ron

Nan Liu

unread,
Aug 13, 2012, 6:02:15 PM8/13/12
to puppet...@googlegroups.com
On Mon, Aug 13, 2012 at 2:44 PM, Ron <ronald....@gmail.com> wrote:
> HUGE Thanks to Nan Liu!
>
> Here is my updated gist on github. This is now working and in production.
> https://gist.github.com/3323851

Whoops, please don't abuse inline_template despite my terrible example.

John, thanks for the suggestion. The regsubst solution should work
something like:

$uname = 'jcb'
$var = ['a', 'b', 'c-d']
$exp = regsubst($var, '^(.+)$', "/home/${uname}/\\0", 'G')
notice($exp)

Thanks,

Nan
Reply all
Reply to author
Forward
0 new messages