How to save attribute type at definition?


I'm trying to merge two attributes which are both arrays and got into the issue that on join I get a string and can't use it for iteration.

Also, I have noted that a simple attribute assignment changes the data type from array to string:

node.default['hosts'] = ["a", "b"]
node.default['hosts_new'] = "#{node.default['hosts']}"

puts "Initial data type: #{node['hosts'].class}"
puts "New data type: #{node['hosts_new'].class}"

Initial data type: Array
New data type: String

And my question if there a way to persist data type on attributes assignment?
Or maybe we should use a different approach for such cases.

Thank you!

Hi stmx38,

Seems that you have quoted the node.default['hosts'] with a double-quote, it gets converted to string due to string interpolation.

you can simply put the assigned array itself i.e.

node.default['hosts'] = ["a", "b"]
node.default['hosts_new'] = node.default['hosts']

puts "Initial data type: #{node['hosts'].class}" // Array
puts "New data type: #{node['hosts_new'].class}" // Array
1 Like

@vsingh, thank you for the reply - it working as expected and using such behavior join also working now:

joined = node['hosts']+ node['hosts_env']