Create a Custom function in puppet.

0 votes
Could somebody specify how to create a new custom function in puppet?
Jul 29 in Puppet by Karan
• 6,670 points
80 views

1 answer to this question.

0 votes

All the custom functions are implemented as separate .rb files and are distributed among modules. 

One needs to put custom functions in lib/puppet/parser/function.

Creating a New Function :

New functions are created or defined using the newfunction method inside the puppet::parser::Functions module. 

One needs to pass the function name as a symbol to newfunction method and the code to run as a block. 

The following example is a function, which is used to write a string to the file inside the /user directory.

module Puppet::Parser::Functions 
   newfunction(:write_line_to_file) do |args| 
      filename = args[0] 
      str = args[1] 
      File.open(filename, 'a') {|fd| fd.puts str } 
   end 
end

Once the user has the function declared, it can be used in the manifest file as shown below.

write_line_to_file('/user/Sam.txt, "Hello Sam!")
answered Jul 29 by Sirajul
• 41,000 points

Related Questions In Puppet

0 votes
1 answer

Is it possible to install puppet agent in a docker container on linux?

Hey @Janice, this is pretty simple. Create ...READ MORE

answered Mar 12 in Puppet by Jason
207 views
0 votes
1 answer

Creating a cron job in puppet

Running a Puppet agent with cron can ...READ MORE

answered Jul 29 in Puppet by Sirajul
• 41,000 points
136 views
0 votes
1 answer

Puppet : Provisioning a Replica in PE for High availability.

Provisioning a replica duplicates specific components and ...READ MORE

answered Aug 1 in Puppet by Sirajul
• 41,000 points
120 views
0 votes
1 answer

Is there a provision of on failure ability in puppet?

Not a built-in one, no.  They do log failures, of ...READ MORE

answered Aug 6 in Puppet by Sirajul
• 41,000 points
31 views
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,380 points
261 views
0 votes
1 answer
0 votes
1 answer