2

I wanted to enable Passwd ohai plugin in my node in test kitchen. On a regular node, I've done it by adding:

ohai.optional_plugins = [
      :Passwd
]

to /etc/chef/client.rb.

I wanted to achieve the same in a test node, so I added to my .kitchen.yml:

provisioner:
  name: chef_zero
  client_rb:
    Ohai::Config[:optional_plugins] =
      - passwd

But when I converge the node, the plugin is not enabled. On a test node, in /home/vagrant/.chef/client.rb there is no entry about ohai optional plugins and templates that I am using during converge, can not see node['etc']['group']['SOME_USERNAME']['gid'] attribute (even after second converge, because Ohai have to run first time and then save attributes).

How can I enable Ohai plugin on a test kitchen node?

Learner
  • 162
  • 11
  • Did you ever figure out a working solution? I'm encountering the [same issue](https://github.com/test-kitchen/test-kitchen/issues/1836). – TrinitronX Jan 16 '22 at 06:39
  • Unfortunately not @TrinitronX . I moved on and decided that it is just the way that Test Kitchen operates. – Learner Jan 17 '22 at 08:26
  • 1
    So as it turns out... my issue was similar yet different (due to another issue presenting itself with the same symptoms). After tinkering for a while I was able to get `test-kitchen` to output the right format in `client.rb`: `'ohai.optional_plugins = ': [:Passwd]`. – TrinitronX Jan 18 '22 at 01:34

3 Answers3

1

Try

client_rb:
    ohai.optional_plugins: [':Passwd']
Draco Ater
  • 20,820
  • 8
  • 62
  • 86
  • 1
    This adds `ohai.optional_plugins [:Passwd]` to `/home/vagrant/.chef/client.rb`. But still I can't use attributes like this: `node['etc']['group']['SOME_USERNAME']['gid']` in templates. Even if I converge twice (because Ohai needs to run and collect attributes), or add to the begining of the recipe resource "ohai" with action `:reload` to reload Ohai attributes. How can I verify if the plugin is enabled on the node? The templates that I am using are working on regular nodes with Passwd plugin enabled. – Learner Jun 09 '20 at 08:42
1

I don't have an exact answer to your question, but one thing to know is that when running test kitchen, any references to the node object in a recipe under test won't have the actual node object from one run to the next, since it's not being saved back to any chef server. It's kind of a 'clean slate' for each run. You can save the node object to a local file on one run then access it again later, but really can't get changing ohai data from within test kitchen easily in the cookbook as far as I know. You can do something like this to save it and get it in a test:

http://www.hurryupandwait.io/blog/accessing-chef-node-attributes-from-kitchen-tests

not sure if that's very helpful but I think that fact might be pertinent

deef
  • 26
  • 1
  • Thanks, that's probably the reason why it was not working for me, even when I `ohai.optional_plugins` added proper line to `client.rb` on a test node. I am marking this as resolved, since this problem is related to the way that Test Kitchen itself operates (as you said, attributes are not being saved back to any chef-server) – Learner Feb 11 '21 at 09:36
1

I was able to get this working in recent Chef version 18.0.6 with the following in .kitchen.yml:

provisioner:
  name: chef_zero
  client_rb:
    'ohai.optional_plugins = ':
        - :Passwd

Including the = equals sign in the hash key as a quoted string ('foo =': bar) forces test-kitchen to template the client.rb correctly:

ohai.optional_plugins =  [:Passwd]
TrinitronX
  • 4,959
  • 3
  • 39
  • 66