Create a Custom function in puppet

0 votes
Could somebody specify how to create a new custom function in puppet?
Jul 29, 2019 in Puppet by Karan
• 19,610 points
1,502 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, 2019 by Sirajul
• 59,230 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, 2019 in Puppet by Jason
1,516 views
0 votes
1 answer

Creating a cron job in puppet

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

answered Jul 29, 2019 in Puppet by Sirajul
• 59,230 points
2,670 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, 2019 in Puppet by Sirajul
• 59,230 points
1,288 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, 2019 in Puppet by Sirajul
• 59,230 points
615 views
+15 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 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,034 views
+2 votes
1 answer
0 votes
1 answer

Puppet+Kubernetes: Is there a way to create resources in Kubernetes using Puppet?

Yes, it is quite possible to create ...READ MORE

answered Aug 23, 2019 in Puppet by Sirajul
• 59,230 points
977 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP