1

I had a requirment to do a certain action only given that a specific user exists. Looking at the following question Chef Users resource (check if exists?) I concluded that the following:

if node['etc']['passwd']['random']

would be a good method.

This actually worked when I tested it in kitchen, but when pushed to production this crushed on all servers with the error:

NoMethodError: undefined method `[]' for nil:NilClass

When using ohai from bash I do see etc: {...}

What exactly is happening here?

Community
  • 1
  • 1
Tom Klino
  • 2,358
  • 5
  • 35
  • 60
  • Are you sure it isn't that `node['etc']['passwd']` isn't the part which isn't loading? – coderanger Mar 06 '17 at 19:20
  • Yes. We ran `chef-shell -z` and `node['etc'] ` returned nil – Tom Klino Mar 06 '17 at 19:24
  • Please test with actual `chef-client`, `chef-shell` can be unpredictable at best. – coderanger Mar 06 '17 at 19:25
  • just ran it again with `node['etc']['passwd']` and it still fails. on etc being nil – Tom Klino Mar 06 '17 at 19:42
  • 1
    Run `chef-client -l debug` and you should see debugging output from Ohai at the top. Check for anything related to the etc plugin. Also check you didn't disable the plugin in either the Chef or Ohai config files. – coderanger Mar 06 '17 at 20:28
  • What is the OS, where Ohai can't find the `etc`? – Draco Ater Mar 07 '17 at 08:40
  • 1
    Don't you have disabled etc plugin in client.rb ? – Tensibai Mar 07 '17 at 09:00
  • found these 2 lines: `# https://tickets.opscode.com/browse/CHEF-5045` `Ohai::Config[:disabled_plugins] << 'passwd' << :Passwd << :Filesystem2` . since the issue mentioned is still relevant, I ended up checking for the existence of a user like so: `if 'grep td-agent /etc/passwd > /dev/null; echo $?' == '0'`. It's ugly, but it works. Thank you all for the help :-) – Tom Klino Mar 08 '17 at 16:59

1 Answers1

0

Eventually, with the help of the people in the comments, I have found that the Ohai passwd plugin was in fact disabled. Due to that, I had to work around the problem and query the existence of a user like so:

if 'grep td-agent /etc/passwd > /dev/null; echo $?' == '0'

Finally, for future reference, my entire resource:

group 'groupname' do
  action :create
  members 'td-agent' if 'grep td-agent /etc/passwd > /dev/null; echo $?' == '0'
  append true
end
Tom Klino
  • 2,358
  • 5
  • 35
  • 60