1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

[PR #6711/24f27a0b backport][stable-7] plugins/inventory/cobbler: Collect IP addresses for hosts and add opt… (#6727)

plugins/inventory/cobbler: Collect IP addresses for hosts and add opt… (#6711)

plugins/inventory/cobbler: Collect IP addresses for hosts and add option to collect all DNS name to IP address mappings

(cherry picked from commit 24f27a0bdf)

Co-authored-by: Orion Poplawski <orion@nwra.com>
This commit is contained in:
patchback[bot] 2023-06-19 08:01:37 +02:00 committed by GitHub
parent 546eb77fd0
commit a406fb1e0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 9 deletions

View file

@ -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 primary IP addess to ``cobbler_ipv4_address`` and IPv6 address to ``cobbler_ipv6_address`` host variable (https://github.com/ansible-collections/community.general/pull/6711).

View file

@ -13,7 +13,9 @@ DOCUMENTATION = '''
version_added: 1.0.0 version_added: 1.0.0
description: description:
- Get inventory hosts from the cobbler service. - Get inventory hosts from the cobbler service.
- "Uses a configuration file as an inventory source, it must end in C(.cobbler.yml) or C(.cobbler.yaml) and has a C(plugin: cobbler) entry." - "Uses a configuration file as an inventory source, it must end in C(.cobbler.yml) or C(.cobbler.yaml) and have a C(plugin: cobbler) entry."
- Adds the primary IP addresses to C(cobbler_ipv4_address) and C(cobbler_ipv6_address) host variables if defined in Cobbler. The primary IP address is
defined as the management interface if defined, or the interface who's DNS name matches the hostname of the system, or else the first interface found.
extends_documentation_fragment: extends_documentation_fragment:
- inventory_cache - inventory_cache
options: options:
@ -32,12 +34,12 @@ DOCUMENTATION = '''
env: env:
- name: COBBLER_USER - name: COBBLER_USER
password: password:
description: Cobbler authentication password description: Cobbler authentication password.
required: false required: false
env: env:
- name: COBBLER_PASSWORD - name: COBBLER_PASSWORD
cache_fallback: cache_fallback:
description: Fallback to cached results if connection to cobbler fails description: Fallback to cached results if connection to cobbler fails.
type: boolean type: boolean
default: false default: false
exclude_profiles: exclude_profiles:
@ -66,20 +68,27 @@ DOCUMENTATION = '''
default: hostname default: hostname
version_added: 7.1.0 version_added: 7.1.0
group_by: group_by:
description: Keys to group hosts by description: Keys to group hosts by.
type: list type: list
elements: string elements: string
default: [ 'mgmt_classes', 'owners', 'status' ] default: [ 'mgmt_classes', 'owners', 'status' ]
group: group:
description: Group to place all hosts into description: Group to place all hosts into.
default: cobbler default: cobbler
group_prefix: group_prefix:
description: Prefix to apply to cobbler groups description: Prefix to apply to cobbler groups.
default: cobbler_ default: cobbler_
want_facts: want_facts:
description: Toggle, if V(true) the plugin will retrieve host facts from the server description: Toggle, if V(true) the plugin will retrieve host facts from the server.
type: boolean type: boolean
default: true default: true
want_ip_addresses:
description:
- Toggle, if V(true) the plugin will add a C(cobbler_ipv4_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 = ''' EXAMPLES = '''
@ -94,7 +103,6 @@ import socket
from ansible.errors import AnsibleError from ansible.errors import AnsibleError
from ansible.module_utils.common.text.converters import to_text from ansible.module_utils.common.text.converters import to_text
from ansible.module_utils.six import iteritems
from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable, to_safe_group_name from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable, to_safe_group_name
# xmlrpc # xmlrpc
@ -246,6 +254,8 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
self.inventory.add_group(self.group) self.inventory.add_group(self.group)
self.display.vvvv('Added site group %s\n' % self.group) self.display.vvvv('Added site group %s\n' % self.group)
ip_addresses = {}
ipv6_addresses = {}
for host in self._get_systems(): for host in self._get_systems():
# Get the FQDN for the host and add it to the right groups # Get the FQDN for the host and add it to the right groups
if self.inventory_hostname == 'system': if self.inventory_hostname == 'system':
@ -260,7 +270,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
# hostname is often empty for non-static IP hosts # hostname is often empty for non-static IP hosts
if hostname == '': if hostname == '':
for (iname, ivalue) in iteritems(interfaces): for iname, ivalue in interfaces.items():
if ivalue['management'] or not ivalue['static']: if ivalue['management'] or not ivalue['static']:
this_dns_name = ivalue.get('dns_name', None) this_dns_name = ivalue.get('dns_name', None)
if this_dns_name is not None and this_dns_name != "": if this_dns_name is not None and this_dns_name != "":
@ -296,8 +306,45 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
self.inventory.add_child(self.group, hostname) self.inventory.add_child(self.group, hostname)
# Add host variables # Add host variables
ip_address = None
ipv6_address = None
for iname, ivalue in interfaces.items():
# Set to first interface or management interface if defined or hostname matches dns_name
if ivalue['ip_address'] != "":
if ip_address is None:
ip_address = ivalue['ip_address']
elif ivalue['management']:
ip_address = ivalue['ip_address']
elif ivalue['dns_name'] == hostname and ip_address is None:
ip_address = ivalue['ip_address']
if ivalue['ipv6_address'] != "":
if ipv6_address is None:
ipv6_address = ivalue['ipv6_address']
elif ivalue['management']:
ipv6_address = ivalue['ipv6_address']
elif ivalue['dns_name'] == hostname and ipv6_address is 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, 'cobbler_ipv4_address', ip_address)
if ipv6_address is not None:
self.inventory.set_variable(hostname, 'cobbler_ipv6_address', ipv6_address)
if self.get_option('want_facts'): if self.get_option('want_facts'):
try: try:
self.inventory.set_variable(hostname, 'cobbler', host) self.inventory.set_variable(hostname, 'cobbler', host)
except ValueError as e: except ValueError as e:
self.display.warning("Could not set host info for %s: %s" % (hostname, to_text(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_ipv4_addresses', ip_addresses)
self.inventory.set_variable(self.group, 'cobbler_ipv6_addresses', ipv6_addresses)