0

I'm wondering if one can put variables in resource names, for example:

$c6release = $::operatingsystemmajrelease
  ##  [EPEL(5, 6, or 7)-x86_64]
yumrepo { 'EPEL' + $c6release + '-x86_64':  ##  This is the part I'm asking about.
    baseurl  => "http://repo.centos.com/cobbler/repo_mirror/EPEL" + $c6release + "-x86_64/",
    gpgcheck => false,
    enabled  => true,
    priority => '2';
}

I've been able to use arrays to make multiple file resources before, but I can't seem to use it in the way described above.

I've found the following in my research:

http://www.nico.schottelius.org/blog/puppet-name-is-not-as-expected-but-classname/ (This is for Puppet 2.7, though)

https://ask.puppet.com/question/13266/trying-to-use-namevar-list-with-name-or-title-variable/

https://docs.puppet.com/puppet/latest/reference/lang_resources.html

Could one use a defined resource type to take care of this?

https://docs.puppet.com/puppet/latest/reference/lang_defined_types.html

Anyway, the above seem to indicate that I can use variables for Puppet resource names, but I can't seem to get them to work.

May one use variables in resource names?

Nathan Basanese
  • 8,475
  • 10
  • 37
  • 66

1 Answers1

2

Yes, the Puppet Language supports interpolation inside resource names. See the documentation here.

What you need:

  $c6release = $::operatingsystemmajrelease
  yumrepo { "EPEL${c6release}-x86_64":
    baseurl  => "http://repo.centos.com/cobbler/repo_mirror/EPEL${c6release}-x86_64/",
    gpgcheck => false,
    enabled  => true,
    priority => '2',
  }
Alex Harvey
  • 14,494
  • 5
  • 61
  • 97
  • Also he should really just use `$::operatingsystemmajrelease` in the interpolation. – Matthew Schuchard Sep 24 '16 at 12:13
  • // , Yes, I likely shall. This is just an example, though. Also, I have begun to pay a little more attention to that 80 col puppet-lint limit. – Nathan Basanese Sep 24 '16 at 19:27
  • // , Speaking of puppet-lint, I guess I'd ask another question. Does using this sort of interpolation make for good Puppet style? – Nathan Basanese Sep 24 '16 at 19:47
  • @Nathan Basanese, I think you need to review etiquette for editing another user's answers. Bash has complex rules for variable interpolation that do not apply in Puppet. If you have a comment on my answer, please make a comment. – Alex Harvey Sep 25 '16 at 07:07
  • 1
    Sorry, I said in my edit summary that you also added a syntax error. Actually, I see I copied the semi-colon from your question. The semi-colon after priority is allowed by Puppet, but it is not recommended and violates the Style Guide. I've updated my answer to show preferred syntax. – Alex Harvey Sep 25 '16 at 07:16
  • @MattSchuchard I'm not so sure that defining a short alias for a long ugly string like $::operatingsystemmajrelease is always necessarily bad style. – Alex Harvey Sep 25 '16 at 08:02
  • @NathanBasanese It's sometimes impossible to avoid the 80 column limit that Puppet-lint complains about, so a lot of people simply disable that check. – Alex Harvey Sep 25 '16 at 08:03
  • @NathanBasanese By the way, Puppet's syntax was historically modelled on Nagios by Puppet's author. – Alex Harvey Sep 25 '16 at 08:05
  • 1
    @AlexHarvey The style is the same between the two, but one is obviously slightly more computationally efficient. Also, the Puppet style guidelines have been 140 chars/line for over a year and not 80, so that guideline is rather reasonable now. – Matthew Schuchard Sep 25 '16 at 12:23
  • Ta, didn't know that. I'm not sure I'd enable 140 characters either. Keeping the lines short is a good habit for readability, but if it's not possible, it's not possible. – Alex Harvey Sep 25 '16 at 12:46
  • 2
    @AlexHarvey Honestly, people have been widely disabling the 140 char check too. I am pretty sure I ignore it most of the time. People not reading the release notes for puppet-lint or linter-puppet-lint (which I maintain) gave me a headache because of the invalid arg (80-->140) and how puppet-lint outputs errors to stdout. – Matthew Schuchard Sep 26 '16 at 00:57