diff --git a/changelogs/fragments/49794-docker_container-network-mode.yml b/changelogs/fragments/49794-docker_container-network-mode.yml new file mode 100644 index 00000000000..dcf8e9c610e --- /dev/null +++ b/changelogs/fragments/49794-docker_container-network-mode.yml @@ -0,0 +1,2 @@ +bugfixes: +- docker_container - fix ``network_mode`` idempotency if the ``container:`` form is used (as opposed to ``container:``) (https://github.com/ansible/ansible/issues/49794) diff --git a/lib/ansible/modules/cloud/docker/docker_container.py b/lib/ansible/modules/cloud/docker/docker_container.py index 40d9eb9d3b3..a500674103a 100644 --- a/lib/ansible/modules/cloud/docker/docker_container.py +++ b/lib/ansible/modules/cloud/docker/docker_container.py @@ -814,6 +814,7 @@ class TaskParameters(DockerBaseClass): self.volume_binds = self._get_volume_binds(self.volumes) self.pid_mode = self._replace_container_names(self.pid_mode) self.ipc_mode = self._replace_container_names(self.ipc_mode) + self.network_mode = self._replace_container_names(self.network_mode) self.log("volumes:") self.log(self.volumes, pretty_print=True) diff --git a/test/integration/targets/docker_container/tasks/tests/options.yml b/test/integration/targets/docker_container/tasks/tests/options.yml index e5391cb1a42..a4772366959 100644 --- a/test/integration/targets/docker_container/tasks/tests/options.yml +++ b/test/integration/targets/docker_container/tasks/tests/options.yml @@ -2031,20 +2031,54 @@ name: "{{ cname }}" state: started network_mode: none - stop_timeout: 1 + force_kill: yes register: network_mode_3 +- name: network_mode (container mode setup) + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 10m"' + name: "{{ cname_h1 }}" + state: started + +- name: network_mode (container mode) + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 10m"' + name: "{{ cname }}" + state: started + network_mode: "container:{{ cname_h1 }}" + force_kill: yes + register: network_mode_4 + +- name: network_mode (container mode idempotency) + docker_container: + image: alpine:3.8 + command: '/bin/sh -c "sleep 10m"' + name: "{{ cname }}" + state: started + network_mode: "container:{{ cname_h1 }}" + register: network_mode_5 + - name: cleanup docker_container: - name: "{{ cname }}" + name: "{{ container_name }}" state: absent - stop_timeout: 1 + force_kill: yes + loop: + - "{{ cname }}" + - "{{ cname_h1 }}" + loop_control: + loop_var: container_name + diff: no - assert: that: - network_mode_1 is changed - network_mode_2 is not changed - network_mode_3 is changed + - network_mode_4 is changed + - network_mode_5 is not changed #################################################################### ## networks, purge_networks ########################################