docker_container: add driver_opts and gw_priority (#1143)

closes #1142
This commit is contained in:
salty 2025-10-07 18:26:25 +02:00 committed by GitHub
parent d0f4ef57a4
commit ebb8569b5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,4 @@
---
minor_changes:
- "docker_container - add ``driver_opts`` option in ``networks`` (https://github.com/ansible-collections/community.docker/issues/1142, https://github.com/ansible-collections/community.docker/pull/1143)."
- "docker_container - add ``gw_priority`` option in ``networks`` (https://github.com/ansible-collections/community.docker/issues/1142, https://github.com/ansible-collections/community.docker/pull/1143)."

View File

@ -1144,6 +1144,8 @@ OPTION_NETWORK = (
aliases=dict(type="list", elements="str"), aliases=dict(type="list", elements="str"),
links=dict(type="list", elements="str"), links=dict(type="list", elements="str"),
mac_address=dict(type="str"), mac_address=dict(type="str"),
driver_opts=dict(type="dict"),
gw_priority=dict(type="int"),
), ),
) )
) )

View File

@ -255,11 +255,25 @@ class DockerAPIEngineDriver(EngineDriver):
"links": "Links", "links": "Links",
"aliases": "Aliases", "aliases": "Aliases",
"mac_address": "MacAddress", "mac_address": "MacAddress",
"driver_opts": "DriverOpts",
}.items(): }.items():
value = parameters.pop(para, None) value = parameters.pop(para, None)
if value: if value:
if para == "links": if para == "links":
value = normalize_links(value) value = normalize_links(value)
elif para == "driver_opts":
# Ensure driver_opts values are strings
for key, val in value.items():
if not isinstance(val, str):
raise Exception(
f"driver_opts values must be strings, got {type(val).__name__} for key '{key}'"
)
params[dest_para] = value
for para, dest_para in {
"gw_priority": "GwPriority",
}.items():
value = parameters.pop(para, None)
if value is not None:
params[dest_para] = value params[dest_para] = value
if parameters: if parameters:
ups = ", ".join([f'"{p}"' for p in sorted(parameters)]) ups = ", ".join([f'"{p}"' for p in sorted(parameters)])
@ -1785,6 +1799,20 @@ OPTION_NETWORK.add_engine(
for net_info in (c.module.params["networks"] or []) for net_info in (c.module.params["networks"] or [])
), ),
}, },
"networks.driver_opts": {
"docker_api_version": "1.32",
"detect_usage": lambda c: any(
net_info.get("driver_opts") is not None
for net_info in (c.module.params["networks"] or [])
),
},
"networks.gw_priority": {
"docker_api_version": "1.48",
"detect_usage": lambda c: any(
net_info.get("gw_priority") is not None
for net_info in (c.module.params["networks"] or [])
),
},
}, },
), ),
) )

View File

@ -751,6 +751,21 @@ options:
Daemon at least in some cases. When passed on creation, this seems to work better. Daemon at least in some cases. When passed on creation, this seems to work better.
type: str type: str
version_added: 3.6.0 version_added: 3.6.0
driver_opts:
description:
- Dictionary of driver options for this network endpoint.
- Allows setting endpoint-specific driver options like C(com.docker.network.endpoint.ifname) to set a custom network interface name.
- Requires Docker API version 1.32 or newer.
type: dict
version_added: 5.0.0
gw_priority:
description:
- Gateway priority for this network endpoint.
- When a container is connected to multiple networks, this controls which network's gateway is used as the default gateway.
- Higher values indicate higher priority.
- Requires Docker API version 1.48 or newer.
type: int
version_added: 5.0.0
networks_cli_compatible: networks_cli_compatible:
description: description:
- If O(networks_cli_compatible=true) (default), this module will behave as C(docker run --network) and will B(not) add - If O(networks_cli_compatible=true) (default), this module will behave as C(docker run --network) and will B(not) add