Skip to content
This repository has been archived by the owner on May 1, 2021. It is now read-only.

Feature/primary vnic #110

Merged
merged 9 commits into from
Apr 23, 2020
5 changes: 5 additions & 0 deletions inventory-script/oci_inventory.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 27 additions & 9 deletions inventory-script/oci_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -320,6 +326,7 @@ def __init__(self):
"regions": None,
"exclude_regions": None,
"strict_hostname_checking": "no",
"primary_vnic_only": "no",
}
boolean_options = [
"sanitize_names",
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions library/oci_database_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down