How to Manage Ruby Code at Run-Time and Dependent Chef Resources


#1

I’m running Chef DK 0.12.0 and chef-client 12.5.1 in a local test kitchen environment.

I need to generate a list of users to remove from my server based on the users returned by Etc.passwd. However, I cannot run Etc.passwd until run-time, so I have to put this code inside a ruby_block resource. A sub-component of this code block includes the Chef user resource which does nothing when inside a ruby_block like so:

ruby_block 'Compile list of users to remove' do
  block do
    Etc.passwd do |account|
      if (conditions-here)
        user account.name do
          manage_home true
          action :remove
        end
      end
    end
  end
  action :run
end

How can I run my Etc.passwd code at run-time, and pass the results to the Chef user resource? I also tried replacing the user resource block with an array, and passing that array to the Chef user resource outside of this ruby_block, but that didn’t work either.