0

Within Chef, the attribute looks as below:

default['cluster']['ipaddress'] = ["10.211.130.108", "10.211.242.203"]

Within Chef recipe, I have put every array element in double quotes, using map:

json_nodes = node['consul']['cluster']['ipaddress'].map { |s| "#{s.to_s}:8300" }

bash 'configuring file.json' do
  code <<-EOH
    echo #{json_nodes} > "/home/user1/file.json"
  EOH
end

I get the following output within the file /home/user1/file.json:

[10.211.130.108:8300, 10.211.242.203:8300]

The output should have double quotes as follows:

["10.211.130.108:8300", "10.211.242.203:8300"]
5
  • The output is really as you described, not "10.211.130.108:8300,10.211.242.203:8300"? Commented Jul 8, 2016 at 14:51
  • How are you "outputting" this in Chef? Commented Jul 8, 2016 at 18:30
  • I have edited my question. please help me out. thanks @StephenKing Commented Jul 9, 2016 at 0:49
  • Have a look at the file resource. Also this question here on SO. Commented Jul 9, 2016 at 8:17
  • That you want to put that into a file via chef's bash resource is kind of the most important information! Commented Jul 9, 2016 at 9:31

4 Answers 4

1

How about:

json_nodes = node['cluster']['ipaddress'].map { |s| "#{s.to_s}:8300" }

I don't have your data but here is an example from my console:

[3] pry(main)> arr = [1,2,3,4,5]
=> [1, 2, 3, 4, 5]
[4] pry(main)> arr.map { |s| "#{s.to_s}:3000" }
=> ["1:3000", "2:3000", "3:3000", "4:3000", "5:3000"]

Note that when I put the .join on the end, it concats them all in a single string.

[8] pry(main)> arr.map { |s| "#{s.to_s}:3000" }.join(',')
=> "1:3000,2:3000,3:3000,4:3000,5:3000"
Sign up to request clarification or add additional context in comments.

6 Comments

I don't see the double quotes. The output is still [10.211.130.108:8300,10.211.242.203:8300]
Are you still doing the .join(',') on the end? If so, that's going to concat all the elements into a comma delmited string. If you want the elements of the array to be double quoted, my answer should work.
It is not working. can you help with the answer that I have posted recently
I am getting this output without putting .join(',') [10.211.130.108:8300, 10.211.242.203:8300]
I added some example code straight from my console. Hope it helps.
|
1

Things become a lot easier, when you use Chef in the way it is made for (and don't call bash..). Your problem translated to solving it in Chef is:

file "/home/user1/file.json" do
  content node['consul']['cluster']['ipaddress'].map { |s| "#{s}:8300" }.to_s
end

If you need non-root ownership and read permissions, have a look at the owner and mode options of the file resource.

Comments

0

This code will do what you specified in your own answer. It's a bit strange though, the string you are aiming looks like an array declaration.

a = ['10.211.130.108','10.211.242.203']
a.map! { |s| "\"#{s}:8300\"" }
result = "[#{a.join(',')}]"
puts result
# Output: ["10.211.130.108:8300","10.211.242.203:8300"]

7 Comments

I don't see the double quotes. The output is still [10.211.130.108:8300,10.211.242.203:8300]
By replacing map with each, I am still not getting the double quotes. Please check at your end.
Sorry about the confusion. It works now. See edited answer which includes a test. Beware that the map! method will modify your original array in-place. If that is undesired, use the "map" method (without the exclamation mark) and store the returned array in another variable.
I have created an answer, can you help with that. I want output to be surrounded with [] also
See my updated answer. If your question has been answered, please make sure to accept an answer for further references.
|
-1

I was able to get double quotes using the following:

json_nodes = node['cluster']['ipaddress'].map { |s| "\"#{s.to_s}:8300\"" }

1 Comment

Sure, but if you still use the bash resource for that, you're simply using Chef in a completely wrong way. You just shouldn't do it this way, then you don't run into that quoting problem because you don't have to escape the bash command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.