Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker_container: improve publish all ports functionality #46594

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- "docker_container - ``publish_ports: all`` was not used correctly when checking idempotency."
7 changes: 5 additions & 2 deletions lib/ansible/modules/cloud/docker/docker_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
- "Use docker CLI syntax: C(8000), C(9000:8000), or C(0.0.0.0:9000:8000), where 8000 is a
container port, 9000 is a host port, and 0.0.0.0 is a host interface."
- Container ports must be exposed either in the Dockerfile or via the C(expose) option.
- A value of all will publish all exposed container ports to random host ports, ignoring
- A value of C(all) will publish all exposed container ports to random host ports, ignoring
any other mappings.
- If C(networks) parameter is provided, will inspect each network to see if there exists
a bridge network with optional parameter com.docker.network.bridge.host_binding_ipv4.
Expand Down Expand Up @@ -1500,7 +1500,8 @@ def has_different_configuration(self, image):
expected_volumes=config.get('Volumes'),
expected_binds=host_config.get('Binds'),
volumes_from=host_config.get('VolumesFrom'),
working_dir=config.get('WorkingDir')
working_dir=config.get('WorkingDir'),
publish_all_ports=host_config.get('PublishAllPorts'),
)
if self.parameters.restart_policy:
config_mapping['restart_retries'] = restart_policy.get('MaximumRetryCount')
Expand Down Expand Up @@ -2291,6 +2292,8 @@ def _parse_comparisons(self):
comparisons[key_main]['comparison'] = value
else:
self.fail("Unknown comparison mode '%s'!" % value)
# Add implicit options
comparisons['publish_all_ports'] = dict(type='value', comparison='strict', name='published_ports')
# Check legacy values
if self.module.params['ignore_image'] and comparisons['image']['comparison'] != 'ignore':
self.module.warn('The ignore_image option has been overridden by the comparisons option!')
Expand Down
94 changes: 94 additions & 0 deletions test/integration/targets/docker_container/tasks/tests/ports.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
---
- name: Registering container name
set_fact:
cname: "{{ cname_prefix ~ '-options' }}"

####################################################################
## published_ports: all ############################################
####################################################################

- name: published_ports -- all
docker_container:
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
state: started
exposed_ports:
- 8080
- 8081
published_ports:
- all
stop_timeout: 1
register: published_ports_1

- name: published_ports -- all (idempotency)
docker_container:
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
state: started
exposed_ports:
- 8080
- 8081
published_ports:
- all
stop_timeout: 1
register: published_ports_2

- name: published_ports -- all (writing out 'all')
docker_container:
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
state: started
exposed_ports:
- 8080
- 8081
published_ports:
- 8080
- 8081
stop_timeout: 1
register: published_ports_3

- name: published_ports -- all (idempotency 2)
docker_container:
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
state: started
exposed_ports:
- 8080
- 8081
published_ports:
- 8080
- 8081
stop_timeout: 1
register: published_ports_4

- name: published_ports -- all (switching back to 'all')
docker_container:
image: alpine:3.8
command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}"
state: started
exposed_ports:
- 8080
- 8081
published_ports:
- all
stop_timeout: 1
register: published_ports_5

- name: cleanup
docker_container:
name: "{{ cname }}"
state: absent
stop_timeout: 1

- assert:
that:
- published_ports_1 is changed
- published_ports_2 is not changed
- published_ports_3 is changed
- published_ports_4 is not changed
- published_ports_5 is changed