From dda36d95ee740ad4927c8442813d2e61290804ec Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Tue, 16 Jan 2018 19:19:32 +0100
Subject: [PATCH] New option "group" which can be used instead of "target" to
 move to any host within the same group

---
 action_plugins/jiffybox.py | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/action_plugins/jiffybox.py b/action_plugins/jiffybox.py
index cd22da1..61242ef 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')})
-- 
GitLab