Puppet – How to Get User’s Home Directory

linuxpuppet

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?

Best Answer

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".

require 'etc'

Etc.passwd { |user|

   Facter.add("home_#{user.name}") do
      setcode do
         user.dir
      end
   end

}

and then you can use them inside your manifest file like this:

$username = "root"
$home = "home_$username"
$home_path = inline_template("<%= scope.lookupvar('::$home') %>")

file { "$home_path/test.txt":
   content => "huhu",
}

Perhaps there is a better way, but i'm afraid not.