Ask Your Question
1

How can I set a variable in a function and use it outside? [closed]

asked 2015-08-14 12:02:03 -0500

Woefdram gravatar image

updated 2015-08-17 19:50:23 -0500

I'm trying to create a profile that will call apt::source to create all the right repositories in apt with create_resources. In Hiera I created a hash with every repository known to me. Params.pp should create a new hash, containing only those elements that are named in the array $wanted_repos.

Because I have to do more of the same (pin, ppa, keys etc.), I created a function to create that hash. Problem is that the hash I create in this function, is empty in params.pp. This is (part of) my params.pp:

class profile::mymodule::params {

  $allrepos     = hiera('profile::apt::allrepos')
  $repokeys     = keys($allrepos)
  $wanted_repos = [ 'debian_wheezy', 'puppetlabs_wheezy' ]
  $repolist     = {}

  createhash { $repokeys:
    total  => $allrepos,
    wanted => $wanted_repos,
    result => $repolist,
  }

}

My function gets called for every key in the hash of all repositories, it then checks if this key is part of the array. If so: it copies that element from the $allrepos to the $repolist. The idea is that $repolist starts out empty, and this function only adds the correct hashes to it:

define createhash ($total, $wanted, $result) {
  if ( $name in $wanted ) {
    $result[$name] = $total[$name]
    }
}

Inside the function, the hash gets the right elements/members. But outside it, in params.pp notably, $result is empty. And not only that one, but also $repolist, $params::repolist, $profile::mymodule::params::repolist and $::profile::mymodule::params::repolist.

I must have missed something in the documentation about variables and scope, but I have no idea what. I tried putting the function in the same file as params.pp, tried it seperate, tried to include it in the parent. I tried absolute and relative paths, but to no avail.

Am I on the right track here? And if so, how do I get this working? In C I would simply pass $repolist to my function as a pointer, but Puppet isn't C :)

Thanks for any suggestions.

Hans

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Woefdram
close date 2015-08-15 15:04:15.193075

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-08-15 14:45:23 -0500

Woefdram gravatar image

updated 2015-08-17 19:54:07 -0500

I guess I missed the part called "No Reassignment" in the documentation about variables...

[edit] I thought it would make sense to explain how I finally resolved the real problem.

I ended up creating a custom function, createhash(), that provides the function I was trying to create in a class. Creating this function was as easy as adding the file lib/puppet/parser/functions/createhash.rb inside my module, with this simple content:

module Puppet::Parser::Functions
  newfunction(:createhash, :type => :rvalue) do |args|

        total  = args[0]
        wanted = args[1]
        result = Hash.new

        wanted.each do |name|
          result.store(name, total[name])
        end

        return result
  end
end

In params.pp I can simply call it to assign the hash to the variable:

class profile::mymodule::params {

  $allrepos     = hiera('profile::apt::allrepos')
  $wanted_repos = [ 'debian_wheezy', 'puppetlabs_wheezy' ]

  $repolist = createhash($allrepos, $wanted_repos)

}

And after that, I can feed it to create_resources() like this:

create_resources('::apt::source', $params::repolist)

It took me some time to figure out the Ruby, guess I'm used to older-generation languages.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-08-14 12:02:03 -0500

Seen: 1,144 times

Last updated: Aug 17 '15