Skip to content

Commit 17c09d8

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 17db579 commit 17c09d8

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
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 All Running 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_all_compute_hosts: True
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: 25 additions & 9 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_all_compute_hosts:
128+
description: When set along with fetch_compute_hosts, compute nodes in any lifecycle state are fetched. Default value set to False.
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_all_compute_hosts: False
357361
358362
# Process only the primary vnic of a compute instance
359363
primary_vnic_only: True
@@ -1845,16 +1849,28 @@ def get_filtered_instances(self, compartment_ocid, region):
18451849
)
18461850
try:
18471851
compute_client = self.get_compute_client_for_region(region)
1852+
limit = 2000
1853+
1854+
if self.get_option("fetch_all_compute_hosts"):
1855+
instances = self.get_filtered_resources(
1856+
oci_common_utils.list_all_resources(
1857+
target_fn=compute_client.list_instances,
1858+
compartment_id=compartment_ocid,
1859+
limit=limit,
1860+
),
1861+
compartment_ocid,
1862+
)
1863+
else:
1864+
instances = self.get_filtered_resources(
1865+
oci_common_utils.list_all_resources(
1866+
target_fn=compute_client.list_instances,
1867+
compartment_id=compartment_ocid,
1868+
lifecycle_state="RUNNING",
1869+
limit=limit,
1870+
),
1871+
compartment_ocid,
1872+
)
18481873

1849-
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-
),
1856-
compartment_ocid,
1857-
)
18581874
return instances
18591875
except ServiceError as ex:
18601876
self.debug(

0 commit comments

Comments
 (0)