2

I am trying to install the kernel-devel package matching the running kernel version.
My guess was:

  package { 'kernel-devel':
    ensure => "${facts['kernelrelease']}",
  }

but it doesn't work if more than one kernel-devel package are already installed. This is the error I get:

Error: Could not update: Failed to update to version 3.10.0-957.21.3.el7.x86_64, got version 3.10.0-957.21.3.el7; 3.10.0-1062.4.3.el7; 3.10.0-1062.9.1.el7 instead

So the package is already installed, but the Package class raises an error because (apparently) it performs a string comparison instead of looking in the versions list.

What is the proper way to handle this?

David
  • 25
  • 5

2 Answers2

3

Unless I am misunderstanding, you can solve this with the version in the resource title.

package { "kernel-devel-${facts['kernelrelease']}":
  ensure => present,
}

Or, if you have other resources which depend on 'kernel-devel' you can use the name attribute.

package { 'kernel-devel':
  name   => "kernel-devel-${facts['kernelrelease']}",
  ensure => present,
}

[root@aaron ~]# dnf list installed kernel-devel
Installed Packages
kernel-devel.x86_64                                                                5.3.11-200.fc30                                                                @updates
kernel-devel.x86_64                                                                5.3.14-200.fc30                                                                @updates
[root@aaron ~]# uname -a
Linux aaron 5.3.15-200.fc30.x86_64 #1 SMP Thu Dec 5 15:18:00 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
[root@aaron ~]# puppet apply kernel-devel.pp 
Notice: Compiled catalog for aaron.tsp in environment production in 0.54 seconds
Notice: /Stage[main]/Main/Package[kernel-devel-5.3.15-200.fc30.x86_64]/ensure: created
Notice: Applied catalog in 62.18 seconds
[root@aaron ~]# dnf list installed kernel-devel
Installed Packages
kernel-devel.x86_64                                                                5.3.11-200.fc30                                                                @updates
kernel-devel.x86_64                                                                5.3.14-200.fc30                                                                @updates
kernel-devel.x86_64                                                                5.3.15-200.fc30                                                                @updates
Aaron Copley
  • 12,525
  • 5
  • 47
  • 68
  • This does seem to work although I think it's by sheer fluke rather than design. Better than using an `exec` anyway so gets my vote! – bodgit Dec 17 '19 at 09:37
  • I agree. I can't see the rationale behind this but it works. Thank you! – David Dec 17 '19 at 10:01
  • 2
    P.S.: In this case, I'd prefer not to change the title, to avoid breaking all the dependencies on `Package['kernel-devel']`. I am going to use the `name` attribute instead. – David Dec 17 '19 at 10:10
  • 1
    P.P.S. I proposed an edit of the solution, but I'm new to ServerFault so I'm not sure if it is appropriate. Should I have asked @Aaron to do that? – David Dec 17 '19 at 10:20
  • I approved the edit. That was perfectly fine to suggest that. The rationale is just an "abuse" of how Yum works. Since the package resource passes the title / name to Yum you can use it however Yum permits. :) – Aaron Copley Dec 17 '19 at 14:44
  • @bodgit I wouldn't have deleted yours. It's not technically incorrect.. I think it's fair to leave multiple options up. Allows the reader to determine what is best for them. Also, you never know when someone may draw inspiration from it for another problem. – Aaron Copley Dec 17 '19 at 14:46
  • 1
    @AaronCopley I suppose so, the power of undelete and it's back. – bodgit Dec 17 '19 at 14:51
1

I've recently run into the same thing. I'm not sure there is a nice way to handle this, from bug tickets I've found apparently Puppet don't really want to acknowledge that there could be multiple versions of a package installed as it breaks their resource model.

I think the only thing you can do is fall back to using an exec, something like

exec { "yum install kernel-devel-${facts['kernelrelease']}":
  path   => $facts['path'],
  unless => "rpm -qa kernel-devel | grep -q ${facts['kernelrelease']}",
}
bodgit
  • 4,751
  • 16
  • 27