feat(inventory): Add unset_ansible_port option

This adds the unset_ansible_port option to the inventory, which allows to
control if the value of ansible_port should be unset if no non-default
value was found.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
This commit is contained in:
Felix Matouschek
2025-04-08 16:38:50 +02:00
parent a1fde268a1
commit da8e04243b
3 changed files with 40 additions and 1 deletions

View File

@@ -63,6 +63,12 @@ options:
- Services are only used if no O(network_name) was provided.
type: bool
default: True
unset_ansible_port:
description:
- Try to unset the value of C(ansible_port) if no non-default value was found.
type: bool
default: True
version_added: 2.2.0
create_groups:
description:
- Enable the creation of groups from labels on C(VirtualMachines) and C(VirtualMachineInstances).
@@ -185,6 +191,7 @@ class InventoryOptions:
network_name: Optional[str] = None
kube_secondary_dns: Optional[bool] = None
use_service: Optional[bool] = None
unset_ansible_port: Optional[bool] = None
create_groups: Optional[bool] = None
base_domain: Optional[str] = None
append_base_domain: Optional[bool] = None
@@ -223,6 +230,11 @@ class InventoryOptions:
if self.use_service is not None
else config_data.get("use_service", True)
)
self.unset_ansible_port = (
self.unset_ansible_port
if self.unset_ansible_port is not None
else config_data.get("unset_ansible_port", True)
)
self.create_groups = (
self.create_groups
if self.create_groups is not None
@@ -861,7 +873,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
ansible_host = ip_address
self.inventory.set_variable(hostname, "ansible_host", ansible_host)
self.inventory.set_variable(hostname, "ansible_port", ansible_port)
if opts.unset_ansible_port or ansible_port is not None:
self.inventory.set_variable(hostname, "ansible_port", ansible_port)
def _set_composable_vars(self, hostname: str) -> None:
"""

View File

@@ -18,6 +18,7 @@ def test_inventory_options_defaults():
assert opts.network_name is None
assert opts.kube_secondary_dns is False
assert opts.use_service is True
assert opts.unset_ansible_port is True
assert opts.create_groups is False
assert opts.base_domain is None
assert opts.append_base_domain is False
@@ -30,6 +31,7 @@ def test_inventory_options_override_defaults():
network_name = "test-network"
kube_secondary_dns = True
use_service = False
unset_ansible_port = False
create_groups = True
base_domain = "test-domain.com"
append_base_domain = True
@@ -41,6 +43,7 @@ def test_inventory_options_override_defaults():
network_name=network_name,
kube_secondary_dns=kube_secondary_dns,
use_service=use_service,
unset_ansible_port=unset_ansible_port,
create_groups=create_groups,
base_domain=base_domain,
append_base_domain=append_base_domain,
@@ -51,6 +54,7 @@ def test_inventory_options_override_defaults():
assert opts.network_name == network_name
assert opts.kube_secondary_dns == kube_secondary_dns
assert opts.use_service == use_service
assert opts.unset_ansible_port == unset_ansible_port
assert opts.create_groups == create_groups
assert opts.base_domain == base_domain
assert opts.append_base_domain == append_base_domain

View File

@@ -272,3 +272,25 @@ def test_no_service_if_network_name(mocker, inventory):
mocker.call(hostname, "ansible_port", None),
]
)
@pytest.mark.parametrize(
"opts,expected",
[
(InventoryOptions(), True),
(InventoryOptions(unset_ansible_port=True), True),
(InventoryOptions(unset_ansible_port=False), False),
],
)
def test_unset_ansible_port(mocker, inventory, opts, expected):
set_variable = mocker.patch.object(inventory.inventory, "set_variable")
hostname = "default-testvm"
ip_address = "1.1.1.1"
inventory._set_ansible_host_and_port({}, hostname, ip_address, None, opts)
calls = [mocker.call(hostname, "ansible_host", ip_address)]
if expected:
calls.append(mocker.call(hostname, "ansible_port", None))
set_variable.assert_has_calls(calls)