In order to do what you describe, you'd need a custom Fact to check for the existence of user1 and do something conditionally based on the state on that system.
Maybe you parameterize UID based on user1 existence. If there's already a user1, don't manage uid.
$user1_uid = $::user1 ? {
true => undef,
default => 55555,
}
user { 'user1':
ensure => present,
comment => 'Appp user',
uid => $user1_uid,
gid => 55555,
home => '/home/user1',
shell => '/bin/bash',
}
Or, worse, wrap your user creation in an Exec resource with a shell script.
Both of these are not the best options, but given what you have asked this is what you can do. Personally, I'd look into what it would take to migrate the UID of user1 on the systems where it isn't consistent. That might be more of an up front effort, but should pay off over time.
EDIT:
I ran a scenario based on your comments and verified my concern. Puppet functions are executed by Puppetserver. So, the conditional you wrote is not dependent on the state of user1 on the client, but on the master.
I started by adding user1 to a Puppet client system. Then, I took your code and created a file user1.pp.
$user_id = inline_template("<%= `/usr/bin/getent passwd user1` %>")
if ("$user_id" == "") {
user { 'user1':
ensure => present,
comment => 'App user',
uid => 61234,
gid => 61234,
home => '/home/user1',
shell => '/bin/bash',
}
} else {
notify { "The group is already present. Skipping..": }
}
When I executed this file with puppet apply
, I get the expected result when the user already exists because the command inside inline_template()
is executed locally. (This is the same result if the user existed on the Puppetserver host.)
[root@localhost ~]# puppet apply /root/user1.pp
...
Notice: The group is already present. Skipping..
Notice: /Stage[main]/Main/Notify[The group is already present. Skipping..]/message: defined 'message' as 'The group is already present. Skipping..'
Notice: Applied catalog in 0.15 seconds
When I put the code on my Puppetserver and run puppet agent, it tries to modify the existing user. You have described this as an undesired result.
[root@localhost ~]# puppet agent -t
...
Notice: /Stage[main]/User[user1]/uid: uid changed 1000 to 61234
Error: Could not find group(s) 61234
Error: /Stage[main]/User[user1]/gid: change from 1000 to 61234 failed: Could not find group(s) 61234
Notice: /Stage[main]/User[user1]/comment: comment changed '' to 'App user'
...
If the user exists on the Puppetserver but not the client, the user is not added because the getent
function returns successfully on the Puppetserver.
[root@localhost ~]# id user1
id: user1: no such user
[root@localhost ~]# puppet agent -t
...
Notice: The group is already present. Skipping..
Notice: /Stage[main]/Notify[The group is already present. Skipping..]/message: defined 'message' as 'The group is already present. Skipping..'
Notice: Applied catalog in 19.73 seconds
I am afraid that you need to reconsider your solution to get your desired results. If you run your solution you may accidentally change UID.