I'm using R10K with Puppet. The Puppetfile is basically DSL:

Because the Puppetfile format is actually implemented using a Ruby DSL any valid Ruby expression can be used.

Ref: https://github.com/puppetlabs/r10k/blob/master/doc/puppetfile.mkd

So based on that, I can write some Ruby code in the Puppetfile. I tried and it does work. But what I don't find and know, is how to access some variables.

Ultimately, I'm trying to do something like this:

mod 'app',
  :git    => 'https://github.com/apps/app.git',
  :branch => ${environment}

Such that the module branch that is checked out is the same as the environment for which it is checked out. Obviouslt ${environment} isn't the right syntax and isn't a real variable name.

So the best answer to this question would be how to get a variable of the environment and the second best answer would be how to reference a variable (and what variables are available) in the Puppetfile.


Of course, 10 minutes after I ask the question, I figured out the answer (been trying to solve this for hours)...

In the Puppetfile you have access to the variables of the DSL class (see DLS class in GitHub. Thus you have access to @librarian which is an instance of the Puppetfile class (see puppetfile.rb). And from there, you have access to its attributes.

So the answer to getting a branch of the name of the current environment is:

mod 'app',
  :git    => 'https://github.com/apps/app.git',
  :branch => @librarian.basedir.split('/').last

Not elegant and I wish the puppetfile.rb would give us a direct access to the environment but that works for me.

  • One question: Did you still use this? If I'm not mistaken, this doesn't work for branch names including a /, like feature/foo. Any idea how to solve this? – gf_ May 26 '16 at 9:02
  • 1
    @gf_ I still use it. Haven't upgraded Puppet to latest however. I don't know the solution to a branch with a slash particularly. – ETL May 26 '16 at 13:22
  • Since r10k 2.4, there is a feature called "Control Repo Branch Tracking". Have a look. – gf_ Apr 18 '17 at 8:52

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.