Skip to content

Commit

Permalink
Make docker_swarm_service option publish.published_port optional (#101)
Browse files Browse the repository at this point in the history
* Change docker_swarm_service option publish.published_port from required to optional. If not specified random high port is assigned by docker.

* Update changelogs/fragments/101-make-service-published-port-optional.yaml

Co-authored-by: Felix Fontein <[email protected]>

Co-authored-by: Felix Fontein <[email protected]>
  • Loading branch information
sgpinkus and felixfontein authored Mar 13, 2021
1 parent b42b76f commit 5b74313
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- docker_swarm_service - change ``publish.published_port`` option from mandatory
to optional. Docker will assign random high port if not specified (https:/ansible-collections/community.docker/issues/99).
9 changes: 5 additions & 4 deletions plugins/modules/docker_swarm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@
description:
- The port to make externally available.
type: int
required: yes
required: no
target_port:
description:
- The port inside the container to expose.
Expand Down Expand Up @@ -2066,9 +2066,10 @@ def build_endpoint_spec(self):
for port in self.publish:
port_spec = {
'Protocol': port['protocol'],
'PublishedPort': port['published_port'],
'TargetPort': port['target_port']
}
if port.get('published_port'):
port_spec['PublishedPort'] = port['published_port']
if port.get('mode'):
port_spec['PublishMode'] = port['mode']
ports.append(port_spec)
Expand Down Expand Up @@ -2214,7 +2215,7 @@ def get_service(self, name):
ds.publish.append({
'protocol': port['Protocol'],
'mode': port.get('PublishMode', None),
'published_port': int(port['PublishedPort']),
'published_port': port.get('PublishedPort', None),
'target_port': int(port['TargetPort'])
})

Expand Down Expand Up @@ -2618,7 +2619,7 @@ def main():
options=dict(type='dict'),
)),
publish=dict(type='list', elements='dict', options=dict(
published_port=dict(type='int', required=True),
published_port=dict(type='int', required=False),
target_port=dict(type='int', required=True),
protocol=dict(type='str', default='tcp', choices=['tcp', 'udp']),
mode=dict(type='str', choices=['ingress', 'host']),
Expand Down

0 comments on commit 5b74313

Please sign in to comment.