I find the 'if' statements in here a bit hard to read and it duplicates the .find lines, they could be tidied I think, perhaps into a couple of lines to find the correct ID and then an if statement for switching between host/hostgroup.