How to avoid passing environment variables to puppet agent

893 views Asked by At

I am trying to use a 3rd party puppet module which relies on the fact the puppet agent which will run has the JAVA_HOME correctly set.

The JAVA_HOME is required because there is a command defined in a provider (see here).

I have 2 options which I can use now:

  • Pass the variable when executing the puppet agent (but works only in interactive)
  • Put in place a file to be sourced by the user executing puppet with the cron (self managed by puppet)

My question is: is it possible to pass the environment to a provider?

POSSIBLE SOLUTION

Taking inspiration from the following post

I created a new type and a provider for it which inside sets the ENV[myvar] = myvalue. Note that the type is not ensurable. The variable will "leak" and will allow other modules relying on it to work properly.

E.g.

### ... Provider code
def ensure
  if value = ENV[resource[:name]]
    value
  else
    :absent
  end
end

def ensure=(new_value)
  if new_value == :absent
    ENV.delete(resource[:name])
  else
    ENV[resource[:name]] = new_value
  end
end

### Usage in puppet code

mytype { 'MYVAR':
  ensure => 'MYVAL',
}

Note that puppet will report at each run the value has changed from absent to a specific value. To avoid so, I think it is enough to always return :absent (I must verify that).

1

There are 1 answers

4
John Bollinger On

is it possible to pass the environment to a provider?

No, Puppet has no mechanism for customizing the environment for external commands on a per-provider basis. I think you have more options than those you've enumerated, however. Among them:

  • wrap the puppet command in a shell script that sets the environment variables you want

  • if you are indeed using cron to schedule agent runs, then use cron's built-in support for setting environment variables for the commands it runs.

The latter seems a promising alternative.