I am creating a user as follow
user { $username:
comment => "$name",
shell => "$shell",
managehome => false,
password => "$password",
groups => $groups
}
Now as u can see I am doing a managehome is false Now later down the lane I need to push a file to the user’s home directory.
$key = "${homedir}/${name}/file"
file { $key:
ensure => present,
owner => $username,
group => $username,
mode => 600,
content => "$keyvalue",
subscribe => User[$username],
}
How can I get the user’s home directory for this?
Hm, i think there you'll need a facter modul to do that and a little hacky manifest file...
facter module: This will register facter variables for all users. like "home_root" or "home_apache".
and then you can use them inside your manifest file like this:
Perhaps there is a better way, but i'm afraid not.
I tried to find a solution for the exactly same problem, and it turned out it's best to take a slightly different approach.
Define home directory explicitly, for example:
When
managehome
is false, the home directory is not even created. So you have to specifically define it. It's often best to make a custom definition for the whole user:You can add more parameters, for example
$keyvalue
, and create a keyfile if that parameter is given.You can also define a global variable
$home = "/home"
(OS specific, if needed) and get home dir with"${home}/${username}"
.Edit: Using hash to define user-specific home directories
More recent Puppet versions (>= 2.6) support hashes. It would be possible to define a hash containing
username => /path/to/home
mappings for each user:For any username, it is then easy to get home directory with
$home['username']
.Home directory hash with fallback
Most of the time, it would be best to have a "fallback default" if user does not exist in the hash. In theory this is possible, although syntax becomes a little cryptic and bloated:
This question is old, but still relevant. There is in fact a better way now. Add a custom fact to [module]/lib/facter/home_dirs.rb containing the following:
Then you can access the data in the manifest thusly:
Bear in mind that this only works if the user already exists prior to the puppet run. If the user is getting created during the run, the home directory should be already known or at least predictable. Puppet is designed to create order, after all.
Hope this helps someone.
When the accounts module is available and all accounts are created via hiera, then below block creates a file in each users home directory, assuming you don't care about system user accounts besides root.
The
$user_home_dir
should be correct for all users that areaccounts
,present
home
directories.