简体   繁体   English

Chef用户资源(检查是否存在?)

[英]Chef Users resource (check if exists?)

I know how to create a user resource: 我知道如何创建用户资源:

user "random" do
  supports :manage_home => true
  comment "Random User"
  uid 1234
  gid "users"
  home "/home/random"
  shell "/bin/bash"
  password "$1$JJsvHslV$szsCjVEroftprNn4JHtDi."
end

But I'm unsure how to use Chef to find a list of all users on a current node. 但是我不确定如何使用Chef查找当前节点上所有用户的列表。 I looked at inspecting node[:users] during a chef-client run, but only node[:current_user] is available to me. 我看了在厨师客户机运行期间检查node [:users]的过程,但是只有node [:current_user]对我可用。 Is there a way, in a Chef recipe, to ask if a regular user exists? 在厨师食谱中,有没有办法询问普通用户是否存在?

I'm in a situation where I shouldn't/can't create users (due to company regulations, but I definitely shouldn't proceed with the installation of other things defined in my cookbook unless xyz users already exist.) 我处在不应该/不能创建用户的情况下(由于公司法规的限制,但除非xyz用户已经存在,否则我绝对不应该继续安装本食谱中定义的其他内容。)

Ohai queries the users on the system for you: Ohai为您查询系统上的用户:

if node['etc']['passwd']['random']
  # Do deploy
end

That works only for local accounts, but if the accounts are managed by LDAP or AD the above does not hold. 这仅适用于本地帐户,但是如果这些帐户由LDAP或AD管理,则上述内容不成立。 I would recommend using: 我建议使用:

"getent group #{mygroup}" 
"getent passwd #{myuser}" 

in a ruby block. 在红宝石块中。

This does not work, if accounts are not local: 如果帐户不是本地帐户,这将不起作用:

if node['etc']['passwd']['random']
  # Do deploy
end

I've got "passwd: files sss" in my nsswitch.conf, as the accounts are in IPA. 我的nsswitch.conf中有“ passwd:files sss”,因为帐户在IPA中。 I guess only the solution from SorinS works. 我想只有SorinS的解决方案有效。

I was getting the same undefined method '[]' for nil:NilClass error message as Tom Klino . undefined method '[]' for nil:NilClass错误消息,我得到了与Tom Klino相同的undefined method '[]' for nil:NilClass I suspect that those having trouble with coderanger's solution have disabled the passwd Ohai plugin. 我怀疑那些对coderanger解决方案有困难人禁用了passwd Ohai插件。

It is very common for those of us with large directory environments to disable the passwd plugin in client.rb to avoid the 413 error ("Request Entity Too Large") when the client report runs. 对于拥有大型目录环境的我们来说,在客户端报告运行时禁用client.rb的passwd插件以避免413错误(“ Request Entity Too Large”)很常见。 Check /etc/chef/client.rb for: 检查/etc/chef/client.rb中的内容:

ohai.disabled_plugins [:Passwd]

With this plugin disabled, node['etc']['passwd'] is unavailable to your recipes, hence the error. 在禁用此插件的情况下, node['etc']['passwd'] 无法用于您的食谱,因此会出现错误。 In my environment, re-enabling the plugin fixes this error. 在我的环境中,重新启用插件可以解决此错误。

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

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