Linux – Puppet – removing user who is logged in

linuxpuppet

I've just started learning puppet and have came across a problem I was wondering if anyone knew how to fix.I m trying to remove a user I get an error message stating that they are logged in so can't be removed.

Now in this case I know what server they're logged into so I could simply just log the user out and proceed from there.My question is however what do you do when you don't know what servers they're logged into? Keeping in mind it could be many servers.

Is there a force remove option or a way to log users out in puppet?

Code below:

user {'art':

ensure => absent,

}

Errors:

Error: Could not delete user art: Execution of '/usr/sbin/userdel art'
returned 8: userdel: user art is currently logged in

Error: /Stage[main]/Main/Node[demo]/User[art]/ensure: change from
present to absent failed: Could not delete user art: Execution of
'/usr/sbin/userdel art' returned 8: userdel: user art is currently
logged in

Best Answer

According to this post, you can disconnect users using pkill -STOP -u USERNAME.
You can create a resource definition something this in puppet:

define kill_and_delete {
    exec { "killing $title":
        command => "pkill -STOP -u $title",
        onlyif  => "grep '^$title' /etc/passwd",
        before => User[$title],
    }
    user { $title: ensure => absent}
}

Afterwards, you use it like this:
kill_and_delete {'art': }
Note: I didn't test this.
see resource ordering - before and require and type reference - exec.