Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to get UID from user within Chef?

I am using "user" resource to define the user within Chef. Now, I want to retrieve the value of UID and GID for the same user.

Can this be done using the ruby code within chef?

Right now, I am trying to use bash resource and running the following command:

id -u $USERNAME
like image 232
meallhour Avatar asked Oct 27 '25 12:10

meallhour


1 Answers

You can use the automatic attributes contributed by Ohai.

That information is accessible via

# uid
node['etc']['passwd']['$USERNAME']['uid']
# gid
node['etc']['passwd']['$USERNAME']['gid']

From the command line, you can explore the attributes as follows:

$ ohai etc/passwd/vagrant
{
  "dir": "/home/vagrant",
  "gid": 900,
  "uid": 900,
  "shell": "/bin/bash",
  "gecos": "vagrant,,,"
}

$ ohai etc/passwd/vagrant/uid
900

If you create a user during the chef run and want to access its information within the same chef run, you probably have to trigger a reload of the responsible ohai plugin. (It might be possible that triggers this automatically, but I wouldn't expect so.)

ohai 'reload passwd' do
  plugin 'passwd'
  action :reload
end

user 'john' do
  notifies :reload, 'ohai[reload passwd]', :immediately
end

As of Chef 15, the etc plugin is optional

like image 191
StephenKing Avatar answered Oct 29 '25 05:10

StephenKing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!