diff --git a/inventory-script/oci_inventory.ini b/inventory-script/oci_inventory.ini index b1ed6b7..c4dde35 100644 --- a/inventory-script/oci_inventory.ini +++ b/inventory-script/oci_inventory.ini @@ -65,6 +65,11 @@ hostname_format = public_ip # Valid values are "yes" or "no". # strict_hostname_checking = no +# The default behavior of this script is to report all network interfaces for each host, leading to duplicates. +# When set to yes, the script will only report each host once based on its primary interface. +# Valid values are "yes" or "no". +# primary_vnic_only = no + # Whether to replace all non-alphanumeric characters except HASH(#), EQUALS(=), PERIOD(.) in the group and host names # in the inventory with an UNDERSCORE(_) character. sanitize_names = True diff --git a/inventory-script/oci_inventory.py b/inventory-script/oci_inventory.py index 62df75f..1c27d48 100755 --- a/inventory-script/oci_inventory.py +++ b/inventory-script/oci_inventory.py @@ -31,6 +31,7 @@ [--exclude-regions EXCLUDE_REGIONS] [--hostname-format {fqdn,private_ip,public_ip}] [--strict-hostname-checking {yes,no}] + [--primary-vnic-only {yes,no}] Produce an Ansible Inventory file based on OCI @@ -113,6 +114,10 @@ without valid hostnames(determined according to the hostname format). When set to yes, the script fails when any host does not have a valid hostname. + --primary-vnic-only {yes,no} + The default behavior of this script is to list all VNIC's + attached to a host as separate inventory items. When set + to yes, the script will only report each instance once. The script reads following environment variables: OCI_CONFIG_FILE, @@ -132,6 +137,7 @@ OCI_INVENTORY_EXCLUDE_REGIONS OCI_COMPARTMENT_OCID OCI_STRICT_HOSTNAME_CHECKING +OCI_PRIMARY_VNIC_ONLY The inventory generated is by default grouped by each of the following: region @@ -320,6 +326,7 @@ def __init__(self): "regions": None, "exclude_regions": None, "strict_hostname_checking": "no", + "primary_vnic_only": "no", } boolean_options = [ "sanitize_names", @@ -585,6 +592,7 @@ def read_env_vars(self): OCI_INVENTORY_EXCLUDE_REGIONS="exclude_regions", OCI_COMPARTMENT_OCID="compartment_ocid", OCI_STRICT_HOSTNAME_CHECKING="strict_hostname_checking", + OCI_PRIMARY_VNIC_ONLY="primary_vnic_only", ) for env_var in os.environ: @@ -1110,15 +1118,16 @@ def build_inventory_for_instance(self, instance, region): for sec_list_id in subnet.security_list_ids: groups.add(sec_list_id) - self.log("Creating inventory for host {0}.".format(host_name)) - self.create_instance_inventory_for_host( - instance_inventory, - host_name, - vars=instance_vars, - groups=groups, - parents=[ad, subnet.vcn_id, region_grp, region_grp], - children=[subnet.id, subnet.id, ad, subnet.vcn_id], - ) + if (self.params["primary_vnic_only"] == "no") or (vnic.is_primary): + self.log("Creating inventory for host {0}.".format(host_name)) + self.create_instance_inventory_for_host( + instance_inventory, + host_name, + vars=instance_vars, + groups=groups, + parents=[ad, subnet.vcn_id, region_grp, region_grp], + children=[subnet.id, subnet.id, ad, subnet.vcn_id], + ) return instance_inventory @@ -1392,6 +1401,15 @@ def parse_cli_args(self): "valid hostname.", ) + parser.add_argument( + "--primary-vnic-only", + action="store", + choices=["yes", "no"], + help="The default behavior of this script is to list all VNIC's attached to a host as separate inventory " + "items. When set to yes, the script will only report each instance once.", + ) + + self.args = parser.parse_args() def read_settings_config(self, boolean_options, dict_options): diff --git a/library/oci_database_facts.py b/library/oci_database_facts.py index f122dbf..0c8aad2 100644 --- a/library/oci_database_facts.py +++ b/library/oci_database_facts.py @@ -210,6 +210,9 @@ def list_databases(db_client, module): compartment_id=compartment_id, db_home_id=db_home_id, ) + else: + get_logger().error("Invalid parameter combination; need either database_id, or compartment_id and db_home_id") + except ServiceError as ex: get_logger().error("Unable to list Databases due to %s", ex.message) module.fail_json(msg=ex.message)