Skip to content

Commit

Permalink
plugins/inventory/cobbler: Collect IP addresses for hosts and add opt…
Browse files Browse the repository at this point in the history
…ion to collect all DNS name to IP address mappings
  • Loading branch information
opoplawski committed Jun 15, 2023
1 parent 473e557 commit f96ddd8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/6711-cobbler-ip-address.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- cobbler inventory plugin - add ``want_ip_addresses`` option to collect all interface DNS name to IP address mapping (https://github.com/ansible-collections/community.general/pull/6711).
- cobbler inventory plugin - add primay IP addess to ``ip_address`` and IPv6 address to ``ipv6_address`` host variable (https://github.com/ansible-collections/community.general/pull/6711)
44 changes: 44 additions & 0 deletions plugins/inventory/cobbler.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
description: Toggle, if V(true) the plugin will retrieve host facts from the server
type: boolean
default: true
want_ip_addresses:
description: Toggle, if V(true) the plugin will add a C(cobbler_ip_addresses) and C(cobbleer_ipv6_addresses) dictionary to the defined O(group) mapping interface DNS names to IP addresses
type: boolean
default: true
version_added: 7.1.0
'''

EXAMPLES = '''
Expand Down Expand Up @@ -246,6 +251,8 @@ def parse(self, inventory, loader, path, cache=True):
self.inventory.add_group(self.group)
self.display.vvvv('Added site group %s\n' % self.group)

ip_addresses = {}
ipv6_addresses = {}
for host in self._get_systems():
# Get the FQDN for the host and add it to the right groups
if self.inventory_hostname == 'system':
Expand Down Expand Up @@ -296,8 +303,45 @@ def parse(self, inventory, loader, path, cache=True):
self.inventory.add_child(self.group, hostname)

# Add host variables
ip_address = None
ipv6_address = None
for (iname, ivalue) in iteritems(interfaces):
# Set to first interface or management interface if defined or hostname matches dns_name
if ivalue['ip_address'] != "":
if ip_address == None:
ip_address = ivalue['ip_address']
elif ivalue['management']:
ip_address = ivalue['ip_address']
elif ivalue['dns_name'] == hostname and ip_address == None:
ip_address = ivalue['ip_address']
if ivalue['ipv6_address'] != "":
if ipv6_address == None:
ipv6_address = ivalue['ipv6_address']
elif ivalue['management']:
ipv6_address = ivalue['ipv6_address']
elif ivalue['dns_name'] == hostname and ipv6_address == None:
ipv6_address = ivalue['ipv6_address']

# Collect all interface name mappings for adding to group vars
if self.get_option('want_ip_addresses'):
if ivalue['dns_name'] != "":
if ivalue['ip_address'] != "":
ip_addresses[ivalue['dns_name']] = ivalue['ip_address']
if ivalue['ipv6_address'] != "":
ip_addresses[ivalue['dns_name']] = ivalue['ipv6_address']

# Add ip_address to host if defined
if ip_address is not None:
self.inventory.set_variable(hostname, 'ip_address', ip_address)
if ipv6_address is not None:
self.inventory.set_variable(hostname, 'ipv6_address', ipv6_address)

if self.get_option('want_facts'):
try:
self.inventory.set_variable(hostname, 'cobbler', host)
except ValueError as e:
self.display.warning("Could not set host info for %s: %s" % (hostname, to_text(e)))

if self.get_option('want_ip_addresses'):
self.inventory.set_variable(self.group, 'cobbler_ip_addresses', ip_addresses)
self.inventory.set_variable(self.group, 'cobbler_ipv6_addresses', ipv6_addresses)

0 comments on commit f96ddd8

Please sign in to comment.