How to add multiple members to a group by chef
-
21-12-2019 - |
Question
How can I add multiple members to a group by chef?
I tried like this, but it fails.
group "git" do
action :modify
members "foo, bar"
append true
end
I can do like this, but members
seems to accept multiple users. How can I use it?
%w(foo bar).each do |m|
group "git" do
action :modify
members m
append true
end
end
Solution
Most arguments that accept multiple inputs in Chef work with standard ruby array syntax. I can't find an example just now, and it's very frustrating that the group
docs don't provide an example of multiple users. In fact, they just say
members
Indicates which users should be set or appended to a group.
And only provide examples with single users. That said, members ["foo", "bar"]
should be the way to go.
OTHER TIPS
Here is a standard Group block with multiple members:
group "rbenv" do
action :manage
members ['member1','member2','member3']
end
You could also use a loop from a whitespace array:
%w{ erin john nick jenna kris }.each do |memberlist|
group "rbenv" do
action :manage
members memberlist
end
end
With this whitespace array option, you're now set to either continue with that type of array, or get an array from Chef data bags or even a MySQL database. In my case, Chef connects to MySQL to get info about users, and provisions them accordingly.