If you redeclare the list *outside* the .each block, you'll end up with the same list. Also if you redeclare it outside, against the $php_remove array, you'll get something weird.
If you redeclare it *inside* the .each block, it'll be local to that block, get destroyed on the second run, and you'll end up with a fresh copy for the next value of your $php_remove array. You need to make sure you don't reuse variable names so you don't get confused, but it'll work. If you want to use the same package list in both places, you may be able to play some games with single- and double-quoting, but I don't think I'd recommend that. On Monday, October 12, 2020 at 7:51:12 AM UTC-4 jochen....@gmail.com wrote: > thx for the smart ;-) > > but when I redeclare the list, I end up keeping two lists of packages. The > guy who I am, I will definitely fuck this up regularly… and that’s back to > the root of my problem… I would like to somehow reuse the $myPackages > structure with different values for $php_version. > > Mhmm… as I write this this sounds like a use for a template… > > Regards > Jochen > > Am 12.10.2020 um 13:46 schrieb A Manzer <ama...@gmail.com>: > > Seems pretty smart to me, tbh. > > The only problem is that $myPackages is constant, so already includes the > "7.4" values from $php_version. You'll need to declare a new list (because > you can't change variables) inside your .each block. > > $php_remove.each | String $php_str_remove | { > $myRemovePackages = [ > "php${php_str_remove}-xml", > "php${php_str_remove}-zip", > "php${php_str_remove}-curl", > "php${php_str_remove}-mbstring", > "libapache2-mod-php${php_str_remove}", > ] > ensure_packages($myRemovePackages, > { > 'ensure' => 'absent', > } ) > } > } > > On Monday, October 12, 2020 at 5:10:34 AM UTC-4 jochen....@gmail.com > wrote: > >> Hi all, >> >> I posted a question on Stackoverflow before the weekend, but no responses >> and not many views yet, unfortunately. So please forgive me asking this >> again here. refactor ensure_packages to switch version of installed >> packages https://stackoverflow.com/q/64284862/13088564?sem=2 >> >> >> I am successfully installing several PHP modules by version with puppet >> on Debian linux like this: >> $php_version = '7.3' >> ensure_packages([ >> "php$php_version-xml", >> "php$php_version-zip", >> "php$php_version-curl", >> "php$php_version-mbstring", >> "libapache2-mod-php$php_version", >> ], >> { 'ensure' => 'present', } ) >> >> now I want to prepare for an update from PHP 7.3 to 7.4. This basically >> works, but the 7.3 packages stay installed. I would like to adapt the code >> to remove the old packages. I am looking for a way to reuse the list of >> packages of modules for uninstalling. >> >> I am thinking of a signature like this >> class profile::software::apache ( >> $php_version = '7.4', >> $php_remove = ['7.0‘, ‘7.3'] >> , #... >> ) { >> >> $myPackages = [ >> "php$php_version-xml", >> "php$php_version-zip", >> "php$php_version-curl", >> "php$php_version-mbstring", >> "libapache2-mod-php$php_version", >> ] >> >> ensure_packages($myPackages, { >> 'ensure' => 'present', >> } ) >> >> $php_remove.each | String $php_version | { >> ensure_packages($myPackages, >> { >> 'ensure' => 'absent', >> } ) >> } >> } >> >> Is there a way to solve this? >> >> thx >> > > -- > 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. > To view this discussion on the web visit > https://groups.google.com/d/msgid/puppet-users/470cea3c-b64f-4666-827f-0bccfb101afcn%40googlegroups.com > > <https://groups.google.com/d/msgid/puppet-users/470cea3c-b64f-4666-827f-0bccfb101afcn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > > -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/16877536-fb28-4234-a5c6-712b087dfce3n%40googlegroups.com.