Ask Your Question

How do I use the generate function?

asked 2016-09-14 14:32:01 -0500

samt gravatar image

I am new to puppet. I have a situation where I have to put a file under particular user's home directory on all servers. I know the user's UID, the home directory varies between the server (i.e. it is historic).

I am trying to accomplish this by using a following puppet code.

$myhome_dir = generate("/bin/bash", "-c", "/bin/grep 666 /etc/passwd | /bin/grep -i abc | /bin/awk -F':' '{print \$6}'")

But when I call, it is throwing the following errors

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, Generators can only contain alphanumerics, file separators, and dashes at ...

Could someone help me to figure out what is causing this error or what I am doing wrong in my puppet code?

My advanced thanks to all.

Thanx, Sam

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-09-14 16:43:22 -0500

samt gravatar image

updated 2016-09-14 16:43:56 -0500

O.K I just realized that the generate function will allow one to run the command on the puppet server and not on the client servers. So, with this limitations, how can I do this extraction of user's home directory inside my puppet code. In short I am looking for a way to capture this into a variable in my puppet code and work with it.

Any thoughts will be greatly appreciated. Thanks for all your helps in advance.

Thanx, Sam

edit flag offensive delete link more

answered 2016-09-14 21:20:42 -0500

DarylW gravatar image

updated 2016-09-14 21:33:41 -0500

The only way to do what you want to do is to create a custom facter fact that would find the directory for you.

You can embed a shell command directly in your fact, similar to this example

# lib/facter/find_user_home.rb
Facter.add(:user_home) do
  setcode "/bin/bash", "-c", "/bin/grep 666 /etc/passwd | /bin/grep -i abc | /bin/awk -F':' '{print \$6}'"

You can then retrieve the value using top-scope notation in your puppet code.

class my_amazing_class(
  $user_home_dir = $::user_home,
) {
  #My special file
  file {"${user_home_dir}/special_file":
    ensure => present,
    content => 'my content',

It may be helpful to understand the whole process of how the puppet system works. 1) The puppet agent contacts the puppet master. Once the communication is completed, and if 'pluginsync' is true, the client will recieve plugins/facter facts from the server 1a) if this is your first time, you will request a cert

2) The client uses the plugin information to gather up information about the environment, and sends it back to the master

3) The master uses the environment information from the client, the hiera data, and the puppet modules to compile the catalog. This is when the generate calls happen.

4) The catalog is sent back to the client. The client gathers up what 'is' on the system, it looks at the catalog for what it 'should be', and comes up with the set of changes that need to be applied, and then applies them.

5) The result of the run is gathered up into a report, and sent back to the puppet master.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-09-14 14:32:01 -0500

Seen: 201 times

Last updated: Sep 14 '16