Skip to content

Instantly share code, notes, and snippets.

@nickanderson
Created June 4, 2025 13:39
Show Gist options
  • Save nickanderson/c43e654ec4404a3ce6989b796649ca79 to your computer and use it in GitHub Desktop.
Save nickanderson/c43e654ec4404a3ce6989b796649ca79 to your computer and use it in GitHub Desktop.
Get the UID of a user, but only if the user exists as a local user on the system.

Using getuid() to get a users uid avoiding error from call where user does not exist

A question from Wolfgang in #CFEngine:matrix.org

I’m confused. I have a bundle that should only run on one specific host. What confuses me is that CFE tries to evaluate the variables. In that bundle, I have something like “uid” int => getuid(“my-user”), but of course this user does not exist on the other systems. However, cfe shows an error saying that it could not get the uid for that user.

CFEngine is eager to resolve variables (classes in common bundles too), for more read about pre-evaluation.

https://docs.cfengine.com/docs/3.26/reference-language-concepts-policy-evaluation.html#agent-pre-evaluation-step

Get the UID of a user, but only if the user exists as a local user on the system.

bundle agent main
{
  vars:
      "i" int => getuid( "no-such-user" );
      "j" int => getuid( "nickanderson" );
}
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
error: Could not get UID for user 'no-such-user', (getpwnam: not found)
bundle agent main
{
  vars:
      "filter" slist => { "name=no-such-user" };
      "i"
        int => getuid( "no-such-user" ),
        if => isgreaterthan( length( getindices( findlocalusers( "@(filter)" ) ) ), 0 );

      "filter" slist => { "name=nickanderson" };
      "j" int => getuid( "nickanderson" ),
        if => isgreaterthan( length( getindices( findlocalusers( "@(filter)" ) ) ), 0 );

      reports:
        "i = $(i)";
        "j = $(j)";
}

R: j = 1000
R: i = $(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment