Skip to content

Latest commit

 

History

History
 
 

lab-08.2-Create-a-custom-function

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Lab 8.2: Create a custom function

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

Steps

  1. Create a new function in a ruby file called homedir.rb.
    • Edit [modulepath]/system/lib/puppet/functions/homedir.rb
  2. Validate your code.
    • ruby -c [modulepath]/system/lib/puppet/functions/homedir.rb
  3. Test your new function using notify resources to display the function output.
    • Edit [control-repo]/manifests/site.pp
  4. Deploy your code.
  5. Run a puppet agent -t

Sample test manifest

notify { "Root's home directory is ${homedir('root')}": }
notify { "Test's home directory is ${homedir('test')}": }

Extra Credit

Revisit your system::managed_user defined type and use this function to calculate the user's home directory.

Solution

Your module structure should resemble

[root@training modules]# tree system/
system/
├── examples
│   └── homedir.pp
└── lib
    └── puppet
            └── functions
                └── homedir.rb

Example file: system/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

Example file: system/examples/homedir.pp

$roothome = homedir('root')
$testhome = homedir('test')

notify { "Root's home directory is ${roothome}": }
notify { "Test's home directory is ${testhome}": }

| Previous Lab | Next Lab |