'Create exported resource inside Puppet custom type

I try to create custom type in Puppet in order to generate some content based on some host state (eg generate token file in /etc directory).

Also I want to create File[/etc/token] inside this custom type and export it into PuppetDB in order to collect it on all other hosts within cluster

I've tried such code but it does not work as expected:

  def generate
    return [] unless self[:export]

    cluster = self[:cluster]
    user    = self[:name]
    should  = self.should(:ensure) || :present

    path    = "/etc/#{cluster}.#{user}.token"
    return [] if catalog.resource(:file, path)

    token = Puppet::Resource.new(:file, path)
    token[:ensure] = should
    token[:content] = provider.create_token
    token.virtual = true
    token.exported = true

    [Puppet::Type.type(:file).new(token)]
  end

Without token.virtual = true and token.exported = true this function can generate required file locally as expected but I can not export it

The only workaround is to use custom fact with required content eg $fact['user_token'] and export this content using default way:

  if $facts['user_token'] {
    @@file { '/etc/clustername.username.token':
      ensure  => file,
      content => $facts['user_token'],
    }
  }

Does anybody know how to generate exported resource inside custom type code?

Thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source