Is there a more elegant way of declaring this variable and its value?

205 views
Skip to first unread message

Fabien Delpierre

unread,
Oct 5, 2015, 11:51:14 AM10/5/15
to Puppet Users
Hey folks,
I have something like this in a manifest:

class foo(
  $python_pips   = {
    pbr => { ensure => '1.3.0', install_args => ['-I'] },
    linecache2 => { ensure => '1.0.0', install_args => ['-I'] },
    elasticsearch => { ensure => '1.6.0', install_args => ['-I'] },
  },
  ) {
  require python
  create_resources('python::pip', $python_pips)
}


I'm using this module: https://github.com/stankevich/puppet-python

I'm wondering if there's a more elegant way of telling the python::pip resource to use the -I flag when installing those three pip packages.
It's going to get even uglier since the $python_pips variable will ultimately live in Hiera, and I have some environments that require a proxy to access the Internet, so in Hiera for those environments it's going to look like this:

foo::python_pips:
  "pbr":
    ensure: 1.3.0
    install_args: ['-I']
    proxy: 'http://proxy.example.com:3128"
  "linecache2":
    ensure: 1.0.0
    install_args: ['-I']
    proxy:
"http://proxy.example.com:3128"
  "elasticsearch
":
    ensure: 1.6.0
    install_args: ['-I']
    proxy: "http://proxy.example.com:3128"

I don't like all this repetition. Wanted to double check if I can make this easier on the eyes before I move on.
I appreciate any input!

Henrik Lindberg

unread,
Oct 5, 2015, 6:48:38 PM10/5/15
to puppet...@googlegroups.com
If you are using 3.x with parser=future, or 4.x you can do like this:

python::pip {
default:
install_args => ['-I'],
proxy => 'http://proxy.example.com:3128' ;

"pbr": ensure => '1.3.0' ;
"linecached": ensure => '1.0.0' ;
"elasticsearch": ensure => '1.6.0' ;
}

The 'default' title defines attributes that are common to all other
bodies in the same resource expression.


- henrik

> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to puppet-users...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/aa823d3e-71bd-4cba-887c-d044656d945a%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/aa823d3e-71bd-4cba-887c-d044656d945a%40googlegroups..com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Fabien Delpierre

unread,
Oct 6, 2015, 9:04:25 AM10/6/15
to puppet...@googlegroups.com
Interesting, thank you. How would I know if I'm using parser=future? Our agents are Puppet v3.4.3. And I'm guessing parser=future is relative to the version you're currently using so I might not have access to the feature anyway.



--
You received this message because you are subscribed to a topic in the Google Groups "Puppet Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/puppet-users/LxqXmdzQQJc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/muuune%24i62%241%40ger.gmane.org.

Henrik Lindberg

unread,
Oct 6, 2015, 2:53:53 PM10/6/15
to puppet...@googlegroups.com
On 2015-06-10 6:03, Fabien Delpierre wrote:
> Interesting, thank you. How would I know if I'm using parser=future? Our
> agents are Puppet v3.4.3. And I'm guessing parser=future is relative to
> the version you're currently using so I might not have access to the
> feature anyway.
>

The setting parser=future is available in 3.x, but in version 3.4 it was
not near production quality. If you want to use it you should update to
the latest 3.8. It is a server side setting. Note that turning it on
requires you to carefully test, and you are most likely to encounter
several issues that needs attention. When you do this, you are also
"future proofing" your logic as it will make it much easier to
transition to puppet 4.x.

Regards
- henrik

> On Mon, Oct 5, 2015 at 6:48 PM, Henrik Lindberg
> <henrik....@cloudsmith.com <mailto:henrik....@cloudsmith.com>>
> <mailto:puppet-users%2Bunsu...@googlegroups.com>
> <mailto:puppet-users...@googlegroups.com
> <mailto:puppet-users%2Bunsu...@googlegroups.com>>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/aa823d3e-71bd-4cba-887c-d044656d945a%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/aa823d3e-71bd-4cba-887c-d044656d945a%40googlegroups..com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
> --
>
> Visit my Blog "Puppet on the Edge"
> http://puppet-on-the-edge.blogspot.se/
>
> --
> You received this message because you are subscribed to a topic in
> the Google Groups "Puppet Users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/puppet-users/LxqXmdzQQJc/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> puppet-users...@googlegroups.com
> <mailto:puppet-users%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to puppet-users...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/CADLuRbwuzSh60RUmuojdnkzroNNAsGYedXbRTcK8SWwK%3D6jBpw%40mail.gmail.com
> <https://groups.google.com/d/msgid/puppet-users/CADLuRbwuzSh60RUmuojdnkzroNNAsGYedXbRTcK8SWwK%3D6jBpw%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Fabien Delpierre

unread,
Oct 6, 2015, 3:42:18 PM10/6/15
to Puppet Users
Ah, I see. That's a bigger deal than just a few lines of duplicated code in Hiera. We are looking at upgrading our server but we'll approach this much more carefully.
Thanks for the insight!
>         <mailto:puppet-users%2Bunsu...@googlegroups.com>>.
>         To view this discussion on the web visit
>         https://groups.google.com/d/msgid/puppet-users/aa823d3e-71bd-4cba-887c-d044656d945a%40googlegroups.com
>         <https://groups.google.com/d/msgid/puppet-users/aa823d3e-71bd-4cba-887c-d044656d945a%40googlegroups..com?utm_medium=email&utm_source=footer>.
>         For more options, visit https://groups.google.com/d/optout.
>
>
>
>     --
>
>     Visit my Blog "Puppet on the Edge"
>     http://puppet-on-the-edge.blogspot.se/
>
>     --
>     You received this message because you are subscribed to a topic in
>     the Google Groups "Puppet Users" group.
>     To unsubscribe from this topic, visit
>     https://groups.google.com/d/topic/puppet-users/LxqXmdzQQJc/unsubscribe.
>     To unsubscribe from this group and all its topics, send an email to
>     puppet-users...@googlegroups.com
>     <mailto:puppet-users%2Bunsu...@googlegroups.com>.
>     To view this discussion on the web visit
>     https://groups.google.com/d/msgid/puppet-users/muuune%24i62%241%40ger.gmane.org.
>
>     For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to puppet-users...@googlegroups.com

Joao Morais

unread,
Oct 6, 2015, 6:37:25 PM10/6/15
to puppet...@googlegroups.com
The syntax below does the same on 3.x? Note the resource name capitalized.

Python::Pip {
install_args => ['-I'],
proxy => 'http://proxy.example.com:3128',
}

https://docs.puppetlabs.com/puppet/3.8/reference/lang_defaults.html


Andreas Ntaflos

unread,
Oct 6, 2015, 8:29:16 PM10/6/15
to puppet...@googlegroups.com, fabien.d...@gmail.com
On 2015-10-05 17:51, Fabien Delpierre wrote:
> Hey folks,
> I have something like this in a manifest:
>
> class foo(
> $python_pips = {
> pbr => { ensure => '1.3.0', install_args => ['-I'] },
> linecache2 => { ensure => '1.0.0', install_args => ['-I'] },
> elasticsearch => { ensure => '1.6.0', install_args => ['-I'] },
> },
> ) {
> require python
> create_resources('python::pip', $python_pips)
> }
>
> I'm using this module: https://github.com/stankevich/puppet-python
>
> I'm wondering if there's a more elegant way of telling the python::pip
> resource to use the -I flag when installing those three pip packages.

The create_resources function supports a third argument for just this
use case: a hash with defaults for each created resource
(https://docs.puppetlabs.com/references/3.stable/function.html#createresources).
This works just fine in Puppet 3.x.

In your case you would do:

```
$python_pips = {
pbr => { ensure => '1.3.0' },
linecache2 => { ensure => '1.0.0' },
elasticsearch => { ensure => '1.6.0' },
}

$python_pip_defaults = {
install_args => [ '-I' ],
}

require python

create_resources('::python::pip', $python_pips, $python_pip_defaults)
```

You can define that defaults hash in Hiera as well:

```
foo::python_pip_defaults:
install_args: [ '-I' ]
proxy: 'http://proxy.example.com:3128'

foo::python_pips:
"pbr":
ensure: 1.3.0
"linecache2":
ensure: 1.0.0
"elasticsearch":
ensure: 1.6.0
```

Then in your profile (or whatever kind of wrapper class you use):

```
$pips = hiera_hash('foo::python_pips', {})
$pip_defaults = hiera_hash('foo::python_pip_defaults', {})

create_resources('::python::pip', $pips, $pip_defaults)
```

We make use of this pattern extensively in our profile classes whenever
there are resources to create (usually defined or native types).

HTH Andreas

signature.asc

Fabien Delpierre

unread,
Oct 6, 2015, 10:18:10 PM10/6/15
to Puppet Users, fabien.d...@gmail.com
That looks like what I need, I will test it all later. Thanks João and Andreas!
Reply all
Reply to author
Forward
0 new messages