summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorRichard Bywater <richard@byh2o.com>2018-02-02 02:33:51 +1100
committerSam Doran <sdoran@ansible.com>2018-02-01 10:33:51 -0500
commit7dbcf752c8e59e28eccf15f7fabcc3192be9bb58 (patch)
treeabf7dda7b7c9269d9b3ffe53064c3a827bb9a68c /contrib
parentfe067040d1acc2a6cd124032e695206bec9926f9 (diff)
downloadansible-7dbcf752c8e59e28eccf15f7fabcc3192be9bb58.tar.gz
Add ability to select to prefer IPv4 addresses for ansible_ssh_host (#35584)
Currently Cloudforms can return a mix of IPv4 and IPv6 addresses in the ipaddresses field and this mix comes in a "random" order (that is the first entry may be IPv4 sometimes but IPv6 other times). If you wish to always use IPv4 for the ansible_ssh_host value then this is problematic. This change adds a new prefer_ipv4 flag which will look for the first IPv4 address in the ipaddresses list and uses that instead of just the first entry.
Diffstat (limited to 'contrib')
-rw-r--r--contrib/inventory/cloudforms.ini3
-rwxr-xr-xcontrib/inventory/cloudforms.py15
2 files changed, 17 insertions, 1 deletions
diff --git a/contrib/inventory/cloudforms.ini b/contrib/inventory/cloudforms.ini
index dc055c1fb7..30b9aa609e 100644
--- a/contrib/inventory/cloudforms.ini
+++ b/contrib/inventory/cloudforms.ini
@@ -31,6 +31,9 @@ nest_tags = False
# Note: This suffix *must* include the leading '.' as it is appended to the hostname as is
# suffix = .example.org
+# If true, will try and use an IPv4 address for the ansible_ssh_host rather than just the first IP address in the list
+prefer_ipv4 = False
+
[cache]
# Maximum time to trust the cache in seconds
diff --git a/contrib/inventory/cloudforms.py b/contrib/inventory/cloudforms.py
index 82fbb6a50a..247d297e3d 100755
--- a/contrib/inventory/cloudforms.py
+++ b/contrib/inventory/cloudforms.py
@@ -182,6 +182,11 @@ class CloudFormsInventory(object):
else:
self.cloudforms_suffix = None
+ if config.has_option('cloudforms', 'prefer_ipv4'):
+ self.cloudforms_prefer_ipv4 = config.getboolean('cloudforms', 'prefer_ipv4')
+ else:
+ self.cloudforms_prefer_ipv4 = False
+
# Ansible related
try:
group_patterns = config.get('ansible', 'group_patterns')
@@ -362,7 +367,15 @@ class CloudFormsInventory(object):
# Set ansible_ssh_host to the first available ip address
if 'ipaddresses' in host and host['ipaddresses'] and isinstance(host['ipaddresses'], list):
- host['ansible_ssh_host'] = host['ipaddresses'][0]
+ # If no preference for IPv4, just use the first entry
+ if not self.cloudforms_prefer_ipv4:
+ host['ansible_ssh_host'] = host['ipaddresses'][0]
+ else:
+ # Before we search for an IPv4 address, set using the first entry in case we don't find any
+ host['ansible_ssh_host'] = host['ipaddresses'][0]
+ for currenthost in host['ipaddresses']:
+ if '.' in currenthost:
+ host['ansible_ssh_host'] = currenthost
# Create additional groups
for key in ('location', 'type', 'vendor'):