Skip to content

Commit 0b1e7ba

Browse files
committed
feat(inventory): allow instances in any state to be fetched optionally
Signed-off-by: Samuel McKendrick <sam.macca9@gmail.com>
1 parent bd2a27c commit 0b1e7ba

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

docs/inventory_plugin/index.rst

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,8 @@ as simple as the following example:
526526
.. important::
527527
``use_extra_vars`` option will work with ansible v2.11 or higher.
528528

529-
Fetch All Compute Hosts
530-
~~~~~~~~~~~~~~~~~~~~~~~
529+
Fetch Compute Hosts
530+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
531531

532532
To fetch all hosts, your configuration can be as simple as the following
533533
example:
@@ -536,6 +536,18 @@ example:
536536
537537
plugin: oracle.oci.oci
538538
539+
Fetch All Compute Hosts (Regardless Of State)
540+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
541+
542+
To fetch all hosts regardless of the instance's lifecycle state, your configuration would look
543+
similar to the following example:
544+
545+
.. code:: yaml
546+
547+
plugin: oracle.oci.oci
548+
549+
fetch_only_running_hosts: False
550+
539551
Fetch Only DB Hosts
540552
~~~~~~~~~~~~~~~~~~~
541553

@@ -733,6 +745,7 @@ with more configuration options:
733745
734746
# Compute Hosts (bool type)
735747
fetch_compute_hosts: True
748+
fetch_all_compute_hosts: False
736749
737750
# Process only the primary vnic of a compute instance
738751
primary_vnic_only: True

plugins/inventory/oci.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@
124124
fetch_compute_hosts:
125125
description: When set, the compute nodes are fetched. Default value set to True.
126126
type: bool
127+
fetch_only_running_hosts:
128+
description: When set along with fetch_compute_hosts, only compute nodes in a RUNNING lifecycle state are fetched. Default value set to True.
129+
type: bool
127130
primary_vnic_only:
128131
description: The default behavior of the plugin is to process all VNIC's attached to a compute instance.
129132
This might result in instance having multiple entries. When this parameter is set to True,
@@ -354,6 +357,7 @@
354357
355358
# Compute Hosts (bool type)
356359
fetch_compute_hosts: True
360+
fetch_only_running_hosts: True
357361
358362
# Process only the primary vnic of a compute instance
359363
primary_vnic_only: True
@@ -736,6 +740,12 @@ def _fetch_compute_hosts(self):
736740
return True
737741
return self.get_option("fetch_compute_hosts")
738742

743+
def _fetch_only_running_hosts(self):
744+
# check if we should fetch compute hosts
745+
if self.get_option("fetch_only_running_hosts") is None:
746+
return True
747+
return self.get_option("fetch_only_running_hosts")
748+
739749
@staticmethod
740750
def create_instance_principal_signer(delegation_token_location=None):
741751
"""
@@ -1845,16 +1855,20 @@ def get_filtered_instances(self, compartment_ocid, region):
18451855
)
18461856
try:
18471857
compute_client = self.get_compute_client_for_region(region)
1858+
list_all_resources_args = {
1859+
"target_fn": compute_client.list_instances,
1860+
"compartment_id": compartment_ocid,
1861+
"limit": 2000,
1862+
}
1863+
1864+
if self._fetch_only_running_hosts():
1865+
list_all_resources_args["lifecycle_state"] = "RUNNING"
18481866

18491867
instances = self.get_filtered_resources(
1850-
oci_common_utils.list_all_resources(
1851-
target_fn=compute_client.list_instances,
1852-
compartment_id=compartment_ocid,
1853-
lifecycle_state="RUNNING",
1854-
limit=2000,
1855-
),
1868+
oci_common_utils.list_all_resources(**list_all_resources_args),
18561869
compartment_ocid,
18571870
)
1871+
18581872
return instances
18591873
except ServiceError as ex:
18601874
self.debug(

0 commit comments

Comments
 (0)