0

In my chef environment I have a variable like this:

"repl_set_members": ["tmongo01", "tmongo02", "tmongo03"]

I need to create JSON to be sent to a Mongo instance and build the replica set.

I created a bash file from a template with:

template "/opt/create_repl_set.sh" do
  source "create_repl_set.sh.erb"
  owner 'root'
  group 'root'
  mode "0755"
  variables(
      :repl_set_name => node['mongodb']['shardname'],
      :members => node['mongodb']['repl_set_members']
  )
end

And in the bash template I would have something like this:

config = "{_id: '<%= @repl_set_name %>', members: [
<% @members.each_with_index do |name,number| %>
{_id: <%= "#{number}" %>, host: '<%= "#{name}" %>:27017'},
<% end %>
] }"

mongo localhost:27091 --eval "JSON.stringify(db.adminCommand({'replSetInitiate' : $config}))"

But the resulting JSON includes a last comma which I don't know how to get rid of.

Does anyone have a better idea?

1
  • 3
    Don't create JSON by hand like that. Use Ruby's built-in JSON class, pass it an array or a hash, and let it generate the JSON. There's more to the format than you think and the well-tested JSON class knows what to do. Commented Mar 6, 2017 at 19:22

2 Answers 2

1

A quick and dirty way to remove your last comma would be to use bash string manipulation and call your script as follow :

mongo localhost:27091 --eval "JSON.stringify(db.adminCommand({'replSetInitiate' : ${config/%,/}))"

Note that this will always delte the last comma in your JSON value, so it will only work if your resulting JSON always has an extra comma (i.e. even when your JSON is empty)

Sign up to request clarification or add additional context in comments.

2 Comments

This isn't really related.
@coderanger In a way, it is, it doesn't tackle the way the JSON is generated but it allows OP to convert a bad JSON into a valid one by removing the extra comma
1

What you probably want is an execute resource an .to_json

mongo_cmd = {
  'replSetInitiate' => {
    '_id' => node['mongodb']['shardname'],
    'members' => node['mongodb']['repl_set_members'].each_with_index.map { |name, number|
      {'_id' => number, 'host' => "#{name}:27017"}
    },
  },
}

execute "mongo localhost:27091 --eval 'JSON.stringify(db.adminCommand(#{mongo_cmd.to_json}))'"

4 Comments

@theTinMan you are right. This is why I was asking for a better idea. Unfortunately, I'm not proficient in Ruby. Could you please give an example, point me in the right direction? Thank you!
I think you meant to reply to the above comment, but I have the example right here for you ...
I did as you said. it's working. Thank you. In the same time, I wanted to see an example of what The Tin Man said.
It's the same thing, I just showed you using to_json instead of the JSON module methods.

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.