Add ability to ignore deployed machines in wait_for_ready state

Fixes: PROD-33758

Change-Id: I9ada6415ead91fa5e9bc9c0ff7422f61e16ecd03
diff --git a/_modules/maas.py b/_modules/maas.py
index cfc0f89..5976d72 100644
--- a/_modules/maas.py
+++ b/_modules/maas.py
@@ -930,8 +930,10 @@
         req_status = kwargs.get("req_status", "Ready")
         to_discover = kwargs.get("machines", None)
         ignore_machines = kwargs.get("ignore_machines", None)
+        ignore_deployed_machines = kwargs.get("ignore_deployed_machines", False)
         attempts = kwargs.get("attempts", 0)
         counter = {}
+        ignored_deployed = []
         if not to_discover:
             try:
                 to_discover = __salt__['config.get']('maas')['region'][
@@ -947,6 +949,9 @@
             for machine in to_discover:
                 for discovered in MachinesStatus.execute()['machines']:
                     if machine == discovered['hostname'] and machine in total:
+                        if ignore_deployed_machines and discovered['status'].lower() == 'deployed':
+                            total.remove(machine)
+                            ignored_deployed.append(machine)
                         if discovered['status'].lower() == req_status.lower():
                             total.remove(machine)
                         elif attempts > 0 and (machine not in counter or counter[machine] < attempts):
@@ -982,8 +987,18 @@
                                 DeployMachines().process()
                                 counter[machine] = 1 if machine not in counter else (counter[machine] + 1)
             if len(total) <= 0:
-                LOG.debug(
-                    "Machines:{} are:{}".format(to_discover, req_status))
+                if len(ignored_deployed) > 0:
+                    for mach in ignored_deployed:
+                        to_discover.remove(mach)
+                    if len(to_discover) > 0:
+                        LOG.debug(
+                        "Machines:{} are:{} and machines:{} were ignored as already deployed.".format(to_discover, req_status, ignored_deployed))
+                    else:
+                        LOG.debug(
+                        "All required machines already exist and were ignored as already deployed:{}".format(ignored_deployed))
+                else:
+                    LOG.debug(
+                        "Machines:{} are:{}".format(to_discover, req_status))
                 return True
             if (timeout - (time.time() - started_at)) <= 0:
                 raise Exception(