Skip to content

Instantly share code, notes, and snippets.

@stefhen
Last active November 8, 2016 22:33
Show Gist options
  • Save stefhen/956c898f5cecff834da971c11ac6033d to your computer and use it in GitHub Desktop.
Save stefhen/956c898f5cecff834da971c11ac6033d to your computer and use it in GitHub Desktop.
Reload ohai data after creating user and get its uid
  • Chef store all users in the node object under the key etc.

  • Also use lazy evaluation and you don't need a ruby_block

  • This tree is populated by ohai at start of run, so to update it and be able to access node['etc']['passwd']['myuser']['uid'] you have to ask ohai to run the etc plugin again like this:

ohai 'reload_passwd' do
  action :nothing
  plugin 'etc'
end

user 'daemonuser' do
  home '/dev/null'
  shell '/sbin/nologin'
  system true
  notifies :reload, 'ohai[reload_passwd]', :immediately
end

ruby_block 'just an example' do
  block do
    # These variables will now have the new values
    puts node['etc']['passwd']['daemonuser']['uid']
    puts node['etc']['passwd']['daemonuser']['gid']
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment