简体   繁体   English

有没有办法只使用厨师编辑 /etc/passwd 文件上的 GID?

[英]Is there a way to only edit the GID on /etc/passwd file using chef?

Can we use a Chef resource to change the GID from 101 to 100(or any other number) in /etc/passwd file?我们可以使用 Chef 资源在 /etc/passwd 文件中将 GID 从 101 更改为 100(或任何其他数字)吗? libuuid:x:100:101::/var/lib/libuuid:

For all the GID's with 101 in /etc/passwd if one needs to change the GID, what is the way to do it using Chef resources?对于 /etc/passwd 中所有具有 101 的 GID,如果需要更改 GID,使用 Chef 资源的方法是什么?

Before suggesting a solution for this, I would like point out 2 things:在为此提出解决方案之前,我想指出两件事:

  1. Hand/scripted editing of /etc/passwd file is best avoided as it can lead to issues.最好避免手动/脚本编辑/etc/passwd文件,因为它可能会导致问题。
  2. Chef is not the tool for editing files. Chef 不是编辑文件的工具。 Chef resources are converged on the node they run, and bring the state of the resource to state defined in the recipe. Chef 资源在其运行的节点上聚合,并将资源的 state 带到配方中定义的 state。

If you would still like to use Chef, you could use Ruby code inside ruby_block resource.如果您仍想使用 Chef,您可以在ruby_block资源中使用 Ruby 代码。

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:包含以下内容的示例文件/tmp/userlist

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.不是 Ruby 的专家,但它就在这里。 The following ruby_block will read lines from a file, and create a new file with the lines with GID 101 replaced:以下ruby_block将从文件中读取行,并创建一个新文件,替换 GID 101 的行:

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.注意:使用 Ruby 或其他语言甚至 Shell 脚本可能有一种更清洁、更简单的方法。 Do consider the same.请考虑相同。

utilize the user resource利用user资源

user 'a user' do
  comment 'A random user'
  uid 1234
  gid 'groupname'
  home '/home/random'
  shell '/bin/bash'
  password '$1$JJsvHslasdfjVEroftprNn4JHtDi'
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM