Puppet: add file for all users?
If I want to add a file to a specific directory using Puppet, I can use:
file { "/folder/file_name":
ensure => present,
source => [puppet///file_name],
}
Is there a way to add the file to every user's home directory?
Something like:
file { "/home/$USER/filename":
ensure => present,
source => [puppet///filename],
}
source to share
While it is * nix, you can add a custom fact to build the home directories on the system. I suggest to name it homedirs.rb
when you create it inside a directory lib/facter/
.
# 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:
unless Facter.value(:kernel) == 'Windows'
around the code block, or keep it only with Linux:
confine kernel: linux
above setcode
.
Then you can use a lambda to iterate through that 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 several issues with your resource file
.
Some helpful doc links in case all 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
source to share