Ricardo García
Ricardo García

Reputation: 325

Call puppet function inside a resource to define a class atribute

I'm trying to call a new function inside a puppet resource in a manifest, but I don't know how to create a resource to assing a value retrieved from a function in a resource.

Define the function

Directory puppet-module/lib/puppet/parser/functions/.

module Puppet::Parser::Functions
  newfunction(:retrive_pub_key, :type => :rvalue, :doc => <<-EOS
    Returns content of ssh key pub file.
    EOS
  ) do |args|
    File.read(args[0])
  end
end

Call the function:

$key = retrive_pub_key('/opt/ssh-keys/admin_rsa.pub')

How Do I call the function inside resource to define $key variable with rvalue ? after 'admin_rsa.pub' is created ?

Calling the function

Inside Puppet manifest init.pp.

??? {"retrieve_key":
    ???,
    ???,
    require => [File["/opt/ssh-keys/admin_rsa.pub"],
}

I've been following this tutorial about functions in puppet

Here is the sample project to test functionalities.

Upvotes: 1

Views: 1927

Answers (1)

Marvin Pinto
Marvin Pinto

Reputation: 30990

If I understand you correctly, you wish to set the content of some file to the content of the corresponding public key file in your /opt/ssh-keys directory.

Would something like this work?

file { 'admin_rsa'
    content => retrieve_pub_key("/opt/ssh-keys/${name}.pub"),
    path => '/home/admin/.ssh/id_rsa.pub',
    ....
    require => File["/opt/ssh-keys/${name}.pub"],
}

Upvotes: 1

Related Questions