Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I already found some examples where I can check if a variable has a specific value

if $var == 'somevalue' {
    ...
}

of if something is defined

if defined(Package['somepackage']) {
    ...
}

but how can I check if a user exists and fail if it doesn't. It would be sufficient to know how to use the result of a script or shell test because /usr/bin/id -u ${name} >/dev/null 2>&1 is all I need, even a check if the user directory exists would be ok.

file and exec do not accept the onlyif parameter, otherwise this wouldn't be a problem at all

share|improve this question
    
An what if you just use ensure parameter of user? –  m0nhawk Jan 24 '13 at 8:46
    
would that just fail if the user doesn't exist or would it try to create that user? –  Dehalion Jan 24 '13 at 8:50
    
I think if you set ensure=present it would create user and do nothing if the user exist. –  m0nhawk Jan 24 '13 at 8:51
    
OK, that was what I thought/was afraid of. I don't want to create a user, i just want to do some things if it exists. Thanks anyway, found something else useful in the puppet reference you linked. But my main problem remains... –  Dehalion Jan 24 '13 at 8:56
    
And what about using require => User['foo'], for what you want to do? –  m0nhawk Jan 24 '13 at 8:58

1 Answer 1

Why not just add in a facter fact and check on it?

with my setup on puppet 3, I'd place the following into modules/facts/lib/facter/user_exists_blah.rb

require 'facter'
Facter.add(:user_exists_blah) do
  setcode do
    name = "blah"
    Facter::Util::Resolution.exec("/usr/bin/id -u #{name} 2>/dev/null")
  end
end

And then use it as

if $::user_exists_blah {
}

An empty string evaluates to false, so you can just use it as a boolean

share|improve this answer
    
That looks like what I want to do. Is it also possible to parametrize this facter (give the user name as a parameter)? –  Dehalion Jan 27 '13 at 21:05
    
Sadly AFAIK, no, you can't –  kormoc Jan 27 '13 at 22:25
2  
You can have a Facter fact return all the users on the system, though, and have the master make decisions based on that. –  jgoldschrafe Feb 3 '13 at 6:20
    
That's actually a really good point jgoldschrafe! Thanks for that insight. –  kormoc Feb 4 '13 at 7:52
1  
You implementation always returns an empty string. I changed the command to "/usr/bin/id -u #{name} 2>/dev/null". Now the variable $user_exists_blah in the if-statement is truthy (non empty string containing the uid) or falsy (an empty string). –  geekQ May 30 '13 at 19:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.