Before suggesting a solution for this, I would like point out 2 things:
- Hand/scripted editing of
/etc/passwd
file is best avoided as it can lead to issues.
- Chef is not the tool for editing files. Chef resources are converged on the node they run, and bring the state of the resource to state defined in the recipe.
If you would still like to use Chef, you could use Ruby code inside ruby_block
resource.
However, the cleanest way to handle this would be identify the users (separately) and use the user resource.
Example:
# This will set gid as 1001 for user1, user2, user3
%w(
user1
user2
user3
).each do |u|
user u do
# add any other properties as required
gid '1001'
end
end
Update:
A sample file /tmp/userlist
with below contents:
john:x:100:101::/bin/bash:
david:x:207:100::/bin/bash:
joe:x:100:101::/bin/nologin:
mike:x:101:100::/bin/bash:
rich:x:103:207::/bin/bash:
fred:x:105:111::/bin/nologin:
Not an expert at Ruby, but here it goes. The following ruby_block
will read lines from a file, and create a new file with the lines with GID 101 replaced:
ruby_block "Write file" do
block do
puts
userlist = File.readlines('/tmp/userlist')
fp = File.open('/tmp/newuserlist', mode='w')
userlist.each do |line|
gid = line.split(':')[3]
if gid == '101'
fp.write line.sub(/.*\K101/, '1001')
else
fp.write line
end
end
fp.close
end
end
Note: There may be a cleaner and easier way to do this with Ruby, or with some other language or even Shell script. Do consider the same.