Skip to content

Commit

Permalink
feat(inventory): allow instances in any state to be fetched optionally
Browse files Browse the repository at this point in the history
Signed-off-by: Samuel McKendrick <[email protected]>
  • Loading branch information
smckend committed Oct 10, 2024
1 parent 17db579 commit 17c09d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
17 changes: 15 additions & 2 deletions docs/inventory_plugin/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -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
Expand Down
34 changes: 25 additions & 9 deletions plugins/inventory/oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1845,16 +1849,28 @@ def get_filtered_instances(self, compartment_ocid, region):
)
try:
compute_client = self.get_compute_client_for_region(region)
limit = 2000

if self.get_option("fetch_all_compute_hosts"):
instances = self.get_filtered_resources(
oci_common_utils.list_all_resources(
target_fn=compute_client.list_instances,
compartment_id=compartment_ocid,
limit=limit,
),
compartment_ocid,
)
else:
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=limit,
),
compartment_ocid,
)

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,
),
compartment_ocid,
)
return instances
except ServiceError as ex:
self.debug(
Expand Down

0 comments on commit 17c09d8

Please sign in to comment.