From 96a40288a1471b5dc27e2b8b45b6b61c57f9ed47 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 | 26 ++++++++++++++++++++------ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/docs/inventory_plugin/index.rst b/docs/inventory_plugin/index.rst index 5e8cf85be1..8735ea6ea1 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 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_only_running_hosts: False + 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 8abf368e49..106da818c6 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_only_running_hosts: + description: When set along with fetch_compute_hosts, only compute nodes in a RUNNING lifecycle state are fetched. Default value set to True. + 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_only_running_hosts: True # Process only the primary vnic of a compute instance primary_vnic_only: True @@ -736,6 +740,12 @@ def _fetch_compute_hosts(self): return True return self.get_option("fetch_compute_hosts") + def _fetch_only_running_hosts(self): + # check if we should fetch only running hosts + if self.get_option("fetch_only_running_hosts") is None: + return True + return self.get_option("fetch_only_running_hosts") + @staticmethod def create_instance_principal_signer(delegation_token_location=None): """ @@ -1845,16 +1855,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 self._fetch_only_running_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(