簡體   English   中英

有沒有辦法只使用廚師編輯 /etc/passwd 文件上的 GID?

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

我們可以使用 Chef 資源在 /etc/passwd 文件中將 GID 從 101 更改為 100(或任何其他數字)嗎? libuuid:x:100:101::/var/lib/libuuid:

對於 /etc/passwd 中所有具有 101 的 GID,如果需要更改 GID,使用 Chef 資源的方法是什么?

在為此提出解決方案之前,我想指出兩件事:

  1. 最好避免手動/腳本編輯/etc/passwd文件,因為它可能會導致問題。
  2. Chef 不是編輯文件的工具。 Chef 資源在其運行的節點上聚合,並將資源的 state 帶到配方中定義的 state。

如果您仍想使用 Chef,您可以在ruby_block資源中使用 Ruby 代碼。

但是,處理此問題的最簡潔方法是(單獨)識別用戶並使用用戶資源。

例子:

# 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

更新

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

不是 Ruby 的專家,但它就在這里。 以下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

注意:使用 Ruby 或其他語言甚至 Shell 腳本可能有一種更清潔、更簡單的方法。 請考慮相同。

利用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