I hope someone can help me with this.
I have this method in ruby:
def puppetrun_oneClass!
ProxyAPI::Puppet.new({:url => puppet_proxy.url}).runSingle fqdn
end
which I then call within this other method:
def update_multiple_puppetrun_oneClass_deploy
if @hosts.map(&:puppetrun_oneClass!).uniq == [true]
notice "Successfully executed, check reports and/or log files for more details"
else
error "Some or all hosts execution failed, Please check log files for more information"
end
end
where @hosts is an array of hostnames.
Now, I would like to extend puppetrun_oneClass! to accept the @myDeploy parameter, where @myDeploy parameter is a variable containing a string.
How could I do that?? and how should I then call the modified method?
Thanks!!
You should just add it as an argument, however this means you need to declare a long-form block to your map
loop.
New method:
def puppetrun_oneClass!(deploy)
# ... Code using `deploy` variable
end
New call:
@hosts.map { |h| host.puppetrun_oneClass!(@myDeploy) }.uniq
Note that uniq
is a pretty heavy handed approach here if you just want to see if any of them failed. You might want to try find
which would stop at the first one that fails rather than blindly executing them all:
!@hosts.find { |h| !host.puppetrun_oneClass!(@myDeploy) }
This will ensure that none of them returned a false condition. If you want to run them all and look for errors, you might try:
failures = @hosts.reject { |h| host.puppetrun_oneClass!(@myDeploy) }
if (failures.empty?)
# Worked
else
# Had problems, failures contains list of failed `@hosts`
end
The first part returns an array of any @hosts
entries that failed. It might be useful to capture this list and use it to produce a more robust error message, perhaps describing those that didn't work.