Writing custom functions allows us to quickly and easily extend the Puppet language to meet our needs. Functions are implemented in Ruby, which is beyond the scope of this class, but this exercise should give you a taste of the power you have at your fingertips. Your task is to write a custom function that accepts a username as a string and returns the expected home directory.
If we were writing code that would run on the node in which the user exists, this would be easy to do with something like:
[root@training ~]# getent passwd "training" | cut -d: -f6
/home/training
However, since all functions run on the master, we'll have to make some assumptions about the home directory layout.
The Ruby conditional for this logic might look like
case user
when 'root'
return '/root'
else
return "/home/#{user}"
end
- Create a new function in a ruby file called
homedir.rb
.- Edit
[modulepath]/system/lib/puppet/functions/homedir.rb
- Edit
- Validate your code.
ruby -c [modulepath]/system/lib/puppet/functions/homedir.rb
- Test your new function using
notify
resources to display the function output.- Edit
[control-repo]/manifests/site.pp
- Edit
- Deploy your code.
- Run a
puppet agent -t
notify { "Root's home directory is ${homedir('root')}": }
notify { "Test's home directory is ${homedir('test')}": }
Revisit your system::managed_user
defined type and use this function to calculate the user's home directory.
[root@training modules]# tree system/
system/
├── examples
│ └── homedir.pp
└── lib
└── puppet
└── functions
└── homedir.rb
Puppet::Functions.create_function(:homedir) do
dispatch :homedir do
param 'String', :use
return_type 'String'
end
# @param [String] user The username to return the conventional Linux home directory of
# @example Returning the root users homedir
# homedir('root') => '/root'
def homedir(user)
case user
when 'root'
'/root'
else
"/home/#{user}"
end
end
end
$roothome = homedir('root')
$testhome = homedir('test')
notify { "Root's home directory is ${roothome}": }
notify { "Test's home directory is ${testhome}": }
| Previous Lab | Next Lab |