Skip to content

Commit

Permalink
feat(falcon_discover): allow duplicate hostnames to be listed (#551)
Browse files Browse the repository at this point in the history
* feat(falcon_discover): allow duplicate hostnames to be listed

Fixes #550

This PR adds a new option `allow_duplicates` that will allow the user to
display all hosts with the same hostname, differentiated by Asset ID
from the Discover API.

* chore: add changelog fragment
  • Loading branch information
carlosmmatos authored Aug 30, 2024
1 parent caa8e3c commit f8ee0d8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/duplicate_hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- falcon_discover - Added ability to allow duplicate hosts in the same environment (https://github.com/CrowdStrike/ansible_collection_falcon/pull/551)
20 changes: 20 additions & 0 deletions plugins/inventory/falcon_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,18 @@
- See the L(Falcon documentation,https://falcon.crowdstrike.com/documentation/page/a9df69ec/asset-management-apis#t0e123bd)
for more information about what filters are available for this inventory.
type: str
allow_duplicates:
description:
- Allow duplicate hosts to be added to the inventory by adding the asset ID as a suffix to the hostname.
- By default, duplicate hostnames are not allowed.
type: bool
default: false
requirements:
- python >= 3.6
- crowdstrike-falconpy >= 1.3.0
notes:
- If no credentials are provided, FalconPy will attempt to use the API credentials via environment variables.
- Hostnames are set to the C(hostname) hostvar if it exists, otherwise the IP address is used.
- The current behavior for assigning an IP address to a host is to use the external IP address if it exists,
otherwise the current local IP address is used. If neither of those exist, the host is skipped as Ansible
would not be able to connect to it.
Expand Down Expand Up @@ -100,6 +107,13 @@
# return all AWS assets
#filter: "cloud_provider:'AWS'"
# allow duplicate hostnames to be added to the inventory
# example: If you two hosts with the same hostname, they will be added as:
# hostnameA
# hostnameA_1234567890abcdef12345678
#
#allow_duplicates: true
# place hosts into dynamically created groups based on variable values
keyed_groups:
# places host in a group named cloud_<cloud_provider> (e.g. cloud_AWS) if the asset is a cloud asset
Expand Down Expand Up @@ -359,6 +373,12 @@ def _add_host_to_inventory(self, host_details):
# Get the hostname
hostname = self._get_hostname(hostvars, ip_address)

# Check if we allow duplicate hostnames
if self.get_option("allow_duplicates"):
# If the hostname already exists, add the asset ID as a suffix
if hostname in self.inventory.hosts:
hostname = f"{hostname}_{hostvars['id']}"

# Add the host to the inventory
self.inventory.add_host(hostname)
self.inventory.set_variable(hostname, "ansible_host", ip_address)
Expand Down

0 comments on commit f8ee0d8

Please sign in to comment.