Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nb_inventory admin_service_name_with_ip_as_primary_ip : Use IP … #1295

Open
wants to merge 4 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- nb_inventory admin_service_name_with_ip_as_primary_ip : Use IP attached to a service name as ansible host
28 changes: 28 additions & 0 deletions plugins/inventory/nb_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@
description: Use out of band IP as `ansible host`
type: boolean
default: false
admin_service_name_with_ip_as_primary_ip:
description: Use IP attached to a service name as `ansible host`
type: string
default: None
rename_variables:
description:
- Rename variables evaluated by nb_inventory, before writing them.
Expand Down Expand Up @@ -854,6 +858,19 @@ def extract_oob_ip(self, host):
except Exception:
return

# service_name_with_ip_as_primary_ip
def extract_ip_with_service_name(self, host, service_name):
try:
for service in self.extract_services(host):
if service.get("name") == service_name:
ip_address_info = service.get("ipaddresses", [])
if ip_address_info:
cidr = ip_address_info[0].get("address")
return str(cidr.split("/")[0])
except Exception as e:
print(f"Error: {e}")
return

def extract_tags(self, host):
try:
tag_zero = host["tags"][0]
Expand Down Expand Up @@ -1948,6 +1965,14 @@ def _fill_host_variables(self, host, hostname):
if self.oob_ip_as_primary_ip:
self._set_variable(hostname, "ansible_host", extracted_oob_ip)

if self.admin_service_name_with_ip_as_primary_ip:
extracted_admin_service_ip = self.extract_ip_with_service_name(
host=host, service_name=self.admin_service_name_with_ip_as_primary_ip
)
self._set_variable(hostname, "admin_service_ip", extracted_admin_service_ip)
if self.admin_service_name_with_ip_as_primary_ip:
self._set_variable(hostname, "ansible_host", extracted_admin_service_ip)

for attribute, extractor in self.group_extractors.items():
extracted_value = extractor(host)

Expand Down Expand Up @@ -2140,6 +2165,9 @@ def parse(self, inventory, loader, path, cache=True):
self.key = self.get_option("key")
self.ca_path = self.get_option("ca_path")
self.oob_ip_as_primary_ip = self.get_option("oob_ip_as_primary_ip")
self.admin_service_name_with_ip_as_primary_ip = self.get_option(
"admin_service_name_with_ip_as_primary_ip"
)

self._set_authorization()

Expand Down
Loading