From 43affc661b5cea75b7c75592dab5135ba66f8da1 Mon Sep 17 00:00:00 2001 From: Keith Resar Date: Thu, 15 Nov 2018 14:58:34 -0600 Subject: [PATCH] add want_ssh_ansible_host flag to foreman dynamic inventory (#34169) --- contrib/inventory/foreman.ini | 6 ++++++ contrib/inventory/foreman.py | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/contrib/inventory/foreman.ini b/contrib/inventory/foreman.ini index 055c82cdb46..e3946daf023 100644 --- a/contrib/inventory/foreman.ini +++ b/contrib/inventory/foreman.ini @@ -152,6 +152,12 @@ want_hostcollections = False # Disabled by default as the change would else not be backward compatible. rich_params = False +# Whether to populate the ansible_ssh_host variable to explicitly specify the +# connection target. Only tested with Katello (Red Hat Satellite). +# If the foreman 'ip' fact exists then the ansible_ssh_host varibale is populated +# to permit connections where DNS resolution fails. +want_ansible_ssh_host = False + [cache] path = . max_age = 60 diff --git a/contrib/inventory/foreman.py b/contrib/inventory/foreman.py index 7c431933e07..f51d073b17c 100755 --- a/contrib/inventory/foreman.py +++ b/contrib/inventory/foreman.py @@ -115,6 +115,11 @@ class ForemanInventory(object): except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): self.want_hostcollections = False + try: + self.want_ansible_ssh_host = config.getboolean('ansible', 'want_ansible_ssh_host') + except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): + self.want_ansible_ssh_host = False + # Do we want parameters to be interpreted if possible as JSON? (no by default) try: self.rich_params = config.getboolean('ansible', 'rich_params') @@ -434,6 +439,8 @@ class ForemanInventory(object): 'foreman': self.cache[hostname], 'foreman_params': self.params[hostname], } + if self.want_ansible_ssh_host and 'ip' in self.cache[hostname]: + self.inventory['_meta']['hostvars'][hostname]['ansible_ssh_host'] = self.cache[hostname]['ip'] if self.want_facts: self.inventory['_meta']['hostvars'][hostname]['foreman_facts'] = self.facts[hostname]