From 3f785b38cd7a58cdd812376503de3a6a5be38b62 Mon Sep 17 00:00:00 2001 From: Samuel McKendrick Date: Thu, 10 Oct 2024 11:04:56 +1100 Subject: [PATCH] feat(inventory): allow instances in any state to be fetched optionally Signed-off-by: Samuel McKendrick --- docs/inventory_plugin/index.rst | 17 +++++++++++++++-- plugins/inventory/oci.py | 20 ++++++++++++++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/docs/inventory_plugin/index.rst b/docs/inventory_plugin/index.rst index 5e8cf85be1..a8c26b1c56 100644 --- a/docs/inventory_plugin/index.rst +++ b/docs/inventory_plugin/index.rst @@ -526,8 +526,8 @@ as simple as the following example: .. important:: ``use_extra_vars`` option will work with ansible v2.11 or higher. -Fetch All Compute Hosts -~~~~~~~~~~~~~~~~~~~~~~~ +Fetch All Running Compute Hosts +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ To fetch all hosts, your configuration can be as simple as the following example: @@ -536,6 +536,18 @@ example: plugin: oracle.oci.oci +Fetch All Compute Hosts (Regardless Of State) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +To fetch all hosts regardless of the instance's lifecycle state, your configuration would look +similar to the following example: + +.. code:: yaml + + plugin: oracle.oci.oci + + fetch_all_compute_hosts: True + Fetch Only DB Hosts ~~~~~~~~~~~~~~~~~~~ @@ -733,6 +745,7 @@ with more configuration options: # Compute Hosts (bool type) fetch_compute_hosts: True + fetch_all_compute_hosts: False # Process only the primary vnic of a compute instance primary_vnic_only: True diff --git a/plugins/inventory/oci.py b/plugins/inventory/oci.py index 491a3000df..ac76c77eee 100644 --- a/plugins/inventory/oci.py +++ b/plugins/inventory/oci.py @@ -124,6 +124,9 @@ fetch_compute_hosts: description: When set, the compute nodes are fetched. Default value set to True. type: bool + fetch_all_compute_hosts: + description: When set along with fetch_compute_hosts, compute nodes in any lifecycle state are fetched. Default value set to False. + type: bool primary_vnic_only: description: The default behavior of the plugin is to process all VNIC's attached to a compute instance. This might result in instance having multiple entries. When this parameter is set to True, @@ -354,6 +357,7 @@ # Compute Hosts (bool type) fetch_compute_hosts: True +fetch_all_compute_hosts: False # Process only the primary vnic of a compute instance primary_vnic_only: True @@ -1845,16 +1849,20 @@ def get_filtered_instances(self, compartment_ocid, region): ) try: compute_client = self.get_compute_client_for_region(region) + list_all_resources_args = { + "target_fn": compute_client.list_instances, + "compartment_id": compartment_ocid, + "limit": 2000, + } + + if not self.get_option("fetch_all_compute_hosts"): + list_all_resources_args["lifecycle_state"] = "RUNNING" instances = self.get_filtered_resources( - oci_common_utils.list_all_resources( - target_fn=compute_client.list_instances, - compartment_id=compartment_ocid, - lifecycle_state="RUNNING", - limit=2000, - ), + oci_common_utils.list_all_resources(**list_all_resources_args), compartment_ocid, ) + return instances except ServiceError as ex: self.debug(