puppetlabs / puppetlabs-stdlib

Puppet Labs Standard Library module
http://forge.puppetlabs.com/puppetlabs/stdlib
Apache License 2.0
348 stars 580 forks source link

Addition of stdlib::getpwnam to return `/etc/passwd` entry. #1419

Closed traylenator closed 7 months ago

traylenator commented 9 months ago

Summary

Given a username stdlib::getpwnam returns the password entry for that user.

Example

$passwd_entry = stdlib::getpwnam('root')

then $passwd_entry would contain:

{
  'name'=>'root',
  'passwd'=>'x',
  'uid'=>0,
  'gid'=>0,
  'gecos'=>'root',
  'dir'=>'/root',
  'shell'=>'/bin/bash',
}

Checklist

$passwd = stdlib::getpwnam('steve')
notify{"${passwd}":}
notify{"${passwd['uid']}":}

results in:

Notice: {name => steve, passwd => x, uid => 1000, gid => 1000, gecos => Steve, dir => /home/steve, shell => /bin/bash}
Notice: /Stage[main]/Main/Notify[{name => steve, passwd => x, uid => 1000, gid => 1000, gecos => Steve, dir => /home/steve, shell => /bin/bash}]/message: defined 'message' as '{name => steve, passwd => x, uid => 1000, gid => 1000, gecos => Steve Traylen, dir => /home/steve, shell => /bin/bash}'
Notice: 1000
Notice: /Stage[main]/Main/Notify[1000]/message: defined 'message' as '1000'
traylenator commented 9 months ago

Perhaps a note about Windows (in)compatibility?

Added a note about POSIX not windows. .. I dont have a windows machine to hand to see what it does.

ekohl commented 9 months ago

Added a note about POSIX not windows. .. I dont have a windows machine to hand to see what it does.

Me neither, but quoting https://ruby-doc.org/core-3.1.2/Etc.html

The Etc module provides access to information typically stored in files in the /etc directory on Unix systems.

Reading that, I think the behavior is completely undefined on Windows so I'd say it's just incompatible as you did now.

traylenator commented 9 months ago

Interesting I added to the CI :

On a random VM here with just an apply the ordering is fine. In github CI it seems the require from the exec to the user was not respected.

traylenator commented 9 months ago

Interesting I added to the CI :

  • Adding a user
  • Using that user in Deferred call to exec .

On a random VM here with just an apply the ordering is fine. In github CI it seems the require from the exec to the user was not respected.

It's a lack of https://puppet.atlassian.net/browse/PUP-11526 on Puppet 7 causing problems. Trying to mark it pending for 7 but totally failing. :cry:

traylenator commented 7 months ago

I started adding this as it was needed for https://github.com/voxpupuli/puppet-systemd/pull/443

however the final solution used completely avoids the need for this.

Close this unless someone thinks it's really worth having.

traylenator commented 7 months ago

This was needed to get the uid for a user to find the path $XDG_RUNTIME_DIR=/run/user/<uid>

This is now avoided by running an exec wrapped in systemd-run as per https://github.com/voxpupuli/puppet-systemd/pull/443

This function is no longer required and given its a bit of a non-trivial one lets not add it.