If I want to add a file to a specific directory with Puppet I can use:
file { "/folder/file_name":
ensure => present,
source => [puppet///file_name],
}
Is there a way I can add a file to each user's home directory?
Something like:
file { "/home/$USER/filename":
ensure => present,
source => [puppet///filename],
}
As long as this is *nix, then you can add a custom fact to assemble the home directories on a system. I suggest naming it homedirs.rb
when you create it inside of the module's lib/facter/
directory.
# collect home directories
Facter.add(:homedirs) do
setcode do
# grab home directories on system and convert into array
`ls /home`.split("\n")
end
end
If you want this for all non-Windows you can add a:
unless Facter.value(:kernel) == 'Windows'
around the code block, or keep it to just Linux with:
confine kernel: linux
above the setcode
.
Then, you can use a lambda to iterate through this fact array and apply the file resource.
# iterate through homedirs in fact array
$facts['homedirs'].each |$homedir| {
# copy file to home directory
file { "/home/$homedir/filename":
ensure => file,
source => puppet:///filename,
}
}
I also fixed up a few issues with your file
resource there.
Some helpful doc links in case any of this is confusing:
https://docs.puppet.com/puppet/latest/type.html#file https://docs.puppet.com/facter/3.6/custom_facts.html#adding-custom-facts-to-facter https://docs.puppet.com/puppet/4.9/function.html#each