diff --git a/action_plugins/jiffybox.py b/action_plugins/jiffybox.py index cd22da1b5eef3f54217f1a731aee49af6f6df899..61242ef9ea9537d7d4291858429fc3fd5edacac4 100644 --- a/action_plugins/jiffybox.py +++ b/action_plugins/jiffybox.py @@ -1,8 +1,9 @@ import json -import requests - from ansible.errors import AnsibleError as ae from ansible.plugins.action import ActionBase + +import requests + try: from __main__ import display except ImportError: @@ -131,6 +132,12 @@ class ActionModule(ActionBase): return self.devices[block] return False + def find_host_in_group(self, group, excluded): + for name in group: + if name != excluded: + return self.find_host(name) + return False + def find_ip(self, address): self.load_ips() for block in self.ips: @@ -194,12 +201,15 @@ class ActionModule(ActionBase): def move_ip(self): display.vv("Moving IP ...") target = self._task.args.get('target', False) - if not target: + group = self._task.args.get('group', False) + if not target and not group: raise ae("'target' is a required argument.") - targethost = self.find_host(target) - if not targethost: - raise ae("Target host %s unknown in this JiffyBox account." % target) - display.vv("- target host: %s" % target) + if target: + targethost = self.find_host(target) + if not targethost: + raise ae("Target host %s unknown in this JiffyBox account." % target) + else: + targethost = False ips = self.find_floating_ip() for ip in ips: @@ -213,8 +223,14 @@ class ActionModule(ActionBase): else: display.vv("- source host: %s" % sourcehost.get('name')) - if sourcehost.get('id') == targethost.get('id'): + if not target: + targethost = self.find_host_in_group(group, sourcehost.get('name')) + + if not targethost: + pass + elif sourcehost.get('id') == targethost.get('id'): display.vv('- cancelled as source and target are the same') else: + display.vv("- target host: %s" % targethost.get('name')) path = 'ips/' + str(sourcehost.get('id')) + '/' + str(ip.get('id')) + '/move' self._request(path, {'targetid': targethost.get('id')})