From c5d0d3ec822cc9b4b824c2e71d8249c1609b89f0 Mon Sep 17 00:00:00 2001 From: Kevin Honka Date: Wed, 13 Mar 2024 09:11:28 +0100 Subject: [PATCH] add an option to use only IPv4 only for ansible_host and ansible_ssh_host By default the openstack inventory fetches the first fixed ip address for "ansible_host" and "ansible_ssh_host". Due to the random sorting of addresses by OpenStack, this can either be a ipv4 or ipv6 address, this is not desirable in some legacy setups where Ansible runs in a ipv4 only setup, but OpenStack is already ipv6 enabled. To prevent this, a new option called "only_ipv4" was added, which forces the inventory plugin to use only fixed ipv4 addresses. Closes-Bug: #2051249 Change-Id: I3aa9868c9299705d4b0dcbf9b9cb561c0855c11c --- plugins/inventory/openstack.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/plugins/inventory/openstack.py b/plugins/inventory/openstack.py index 826e304b..7245c42e 100644 --- a/plugins/inventory/openstack.py +++ b/plugins/inventory/openstack.py @@ -96,6 +96,12 @@ options: only. type: bool default: false + only_ipv4: + description: + - Use only ipv4 addresses for ansible_host and ansible_ssh_host. + - Using I(only_ipv4) helps when running Ansible in a ipv4 only setup. + type: bool + default: false show_all: description: - Whether all servers should be listed or not. @@ -384,10 +390,17 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): if address['OS-EXT-IPS:type'] == 'floating'), None) - fixed_ip = next( - (address['addr'] for address in addresses - if address['OS-EXT-IPS:type'] == 'fixed'), - None) + if self.get_option('only_ipv4'): + fixed_ip = next( + (address['addr'] for address in addresses + if (address['OS-EXT-IPS:type'] == 'fixed' and address['version'] == 4)), + None) + + else: + fixed_ip = next( + (address['addr'] for address in addresses + if address['OS-EXT-IPS:type'] == 'fixed'), + None) ip = floating_ip if floating_ip is not None and not self.get_option('private') else fixed_ip