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

Add Constructed to Foreman inventory plugin #62542

Merged
merged 5 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- foreman inventory plugin - Add support for constructed options.
24 changes: 16 additions & 8 deletions lib/ansible/plugins/inventory/foreman.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- "Uses a configuration file as an inventory source, it must end in ``.foreman.yml`` or ``.foreman.yaml`` and has a ``plugin: foreman`` entry."
extends_documentation_fragment:
- inventory_cache
- constructed
options:
plugin:
description: the name of this plugin, it should always be set to 'foreman' for this plugin to recognize it as it's own.
Expand Down Expand Up @@ -74,7 +75,7 @@
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_bytes, to_native, to_text
from ansible.module_utils.common._collections_compat import MutableMapping
from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable, to_safe_group_name
from ansible.plugins.inventory import BaseInventoryPlugin, Cacheable, to_safe_group_name, Constructable

# 3rd party imports
try:
Expand All @@ -87,7 +88,7 @@
from requests.auth import HTTPBasicAuth


class InventoryModule(BaseInventoryPlugin, Cacheable):
class InventoryModule(BaseInventoryPlugin, Cacheable, Constructable):
''' Host inventory parser for ansible using foreman as source. '''

NAME = 'foreman'
Expand Down Expand Up @@ -197,38 +198,45 @@ def _populate(self):
for host in self._get_hosts():

if host.get('name'):
self.inventory.add_host(host['name'])
host_name = self.inventory.add_host(host['name'])

# create directly mapped groups
group_name = host.get('hostgroup_title', host.get('hostgroup_name'))
if group_name:
group_name = to_safe_group_name('%s%s' % (self.get_option('group_prefix'), group_name.lower().replace(" ", "")))
group_name = self.inventory.add_group(group_name)
self.inventory.add_child(group_name, host['name'])
self.inventory.add_child(group_name, host_name)

# set host vars from host info
try:
for k, v in host.items():
if k not in ('name', 'hostgroup_title', 'hostgroup_name'):
try:
self.inventory.set_variable(host['name'], self.get_option('vars_prefix') + k, v)
self.inventory.set_variable(host_name, self.get_option('vars_prefix') + k, v)
except ValueError as e:
self.display.warning("Could not set host info hostvar for %s, skipping %s: %s" % (host, k, to_text(e)))
except ValueError as e:
self.display.warning("Could not get host info for %s, skipping: %s" % (host['name'], to_text(e)))
self.display.warning("Could not get host info for %s, skipping: %s" % (host_name, to_text(e)))

# set host vars from params
if self.get_option('want_params'):
for p in self._get_all_params_by_id(host['id']):
try:
self.inventory.set_variable(host['name'], p['name'], p['value'])
self.inventory.set_variable(host_name, p['name'], p['value'])
except ValueError as e:
self.display.warning("Could not set hostvar %s to '%s' for the '%s' host, skipping: %s" %
(p['name'], to_native(p['value']), host, to_native(e)))

# set host vars from facts
if self.get_option('want_facts'):
self.inventory.set_variable(host['name'], 'ansible_facts', self._get_facts(host))
self.inventory.set_variable(host_name, 'ansible_facts', self._get_facts(host))

strict = self.get_option('strict')

hostvars = self.inventory.get_host(host_name).get_vars()
self._set_composite_vars(self.get_option('compose'), hostvars, host_name, strict)
self._add_host_to_composed_groups(self.get_option('groups'), hostvars, host_name, strict)
self._add_host_to_keyed_groups(self.get_option('keyed_groups'), hostvars, host_name, strict)

def parse(self, inventory, loader, path, cache=True):

Expand Down