4

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.

gxx
  • 5,483
  • 2
  • 21
  • 42
ETL
  • 6,443
  • 1
  • 26
  • 47

1 Answers1

4

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 DSL 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.

Mike Pennington
  • 8,266
  • 9
  • 41
  • 86
ETL
  • 6,443
  • 1
  • 26
  • 47
  • 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? – gxx May 26 '16 at 09: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](https://github.com/puppetlabs/r10k/blob/master/doc/puppetfile.mkd#control-repo-branch-tracking). – gxx Apr 18 '17 at 08:52