docker_container: deprecate ignore_image and purge_networks (#487)

* Deprecate ignore_image and purge_networks.

* Fix YAML.

* Simple replacement doesn't work in this case.
This commit is contained in:
Felix Fontein 2022-11-01 19:57:56 +01:00 committed by GitHub
parent 1ac3a99e7c
commit 51d5744cb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 31 deletions

View File

@ -0,0 +1,3 @@
deprecated_features:
- "docker_container - the ``ignore_image`` option is deprecated and will be removed in community.docker 4.0.0. Use ``image: ignore`` in ``comparisons`` instead (https://github.com/ansible-collections/community.docker/pull/487)."
- "docker_container - the ``purge_networks`` option is deprecated and will be removed in community.docker 4.0.0. Use ``networks: strict`` in ``comparisons`` instead, and make sure to provide ``networks``, with value ``[]`` if all networks should be removed (https://github.com/ansible-collections/community.docker/pull/487)."

View File

@ -75,7 +75,6 @@ class ContainerManager(DockerBaseClass):
self.param_output_logs = self.module.params['output_logs'] self.param_output_logs = self.module.params['output_logs']
self.param_paused = self.module.params['paused'] self.param_paused = self.module.params['paused']
self.param_pull = self.module.params['pull'] self.param_pull = self.module.params['pull']
self.param_purge_networks = self.module.params['purge_networks']
self.param_recreate = self.module.params['recreate'] self.param_recreate = self.module.params['recreate']
self.param_removal_wait_timeout = self.module.params['removal_wait_timeout'] self.param_removal_wait_timeout = self.module.params['removal_wait_timeout']
self.param_restart = self.module.params['restart'] self.param_restart = self.module.params['restart']
@ -128,7 +127,7 @@ class ContainerManager(DockerBaseClass):
# Process legacy ignore options # Process legacy ignore options
if self.module.params['ignore_image']: if self.module.params['ignore_image']:
self.all_options['image'].comparison = 'ignore' self.all_options['image'].comparison = 'ignore'
if self.param_purge_networks: if self.module.params['purge_networks']:
self.all_options['networks'].comparison = 'strict' self.all_options['networks'].comparison = 'strict'
# Process comparsions specified by user # Process comparsions specified by user
if self.module.params.get('comparisons'): if self.module.params.get('comparisons'):
@ -177,7 +176,7 @@ class ContainerManager(DockerBaseClass):
# Check legacy values # Check legacy values
if self.module.params['ignore_image'] and self.all_options['image'].comparison != 'ignore': if self.module.params['ignore_image'] and self.all_options['image'].comparison != 'ignore':
self.module.warn('The ignore_image option has been overridden by the comparisons option!') self.module.warn('The ignore_image option has been overridden by the comparisons option!')
if self.param_purge_networks and self.all_options['networks'].comparison != 'strict': if self.module.params['purge_networks'] and self.all_options['networks'].comparison != 'strict':
self.module.warn('The purge_networks option has been overridden by the comparisons option!') self.module.warn('The purge_networks option has been overridden by the comparisons option!')
def _update_params(self): def _update_params(self):
@ -630,7 +629,15 @@ class ContainerManager(DockerBaseClass):
self.results['changed'] = True self.results['changed'] = True
updated_container = self._add_networks(container, network_differences) updated_container = self._add_networks(container, network_differences)
if (self.all_options['networks'].comparison == 'strict' and self.module.params['networks'] is not None) or self.param_purge_networks: purge_networks = self.all_options['networks'].comparison == 'strict' and self.module.params['networks'] is not None
if not purge_networks and self.module.params['purge_networks']:
purge_networks = True
self.module.deprecate(
'The purge_networks option is used while networks is not specified. In this case purge_networks=true cannot'
' be replaced by `networks: strict` in comparisons, which is necessary once purge_networks is removed.'
' Please modify the docker_container invocation by adding `networks: []`',
version='4.0.0', collection_name='community.docker')
if purge_networks:
has_extra_networks, extra_networks = self.has_extra_networks(container) has_extra_networks, extra_networks = self.has_extra_networks(container)
if has_extra_networks: if has_extra_networks:
if self.diff.get('differences'): if self.diff.get('differences'):
@ -799,7 +806,7 @@ def run_module(engine_driver):
command_handling=dict(type='str', choices=['compatibility', 'correct'], default='correct'), command_handling=dict(type='str', choices=['compatibility', 'correct'], default='correct'),
default_host_ip=dict(type='str'), default_host_ip=dict(type='str'),
force_kill=dict(type='bool', default=False, aliases=['forcekill']), force_kill=dict(type='bool', default=False, aliases=['forcekill']),
ignore_image=dict(type='bool', default=False), ignore_image=dict(type='bool', default=False, removed_in_version='4.0.0', removed_from_collection='community.docker'),
image=dict(type='str'), image=dict(type='str'),
image_comparison=dict(type='str', choices=['desired-image', 'current-image'], default='desired-image'), image_comparison=dict(type='str', choices=['desired-image', 'current-image'], default='desired-image'),
image_label_mismatch=dict(type='str', choices=['ignore', 'fail'], default='ignore'), image_label_mismatch=dict(type='str', choices=['ignore', 'fail'], default='ignore'),
@ -810,7 +817,7 @@ def run_module(engine_driver):
output_logs=dict(type='bool', default=False), output_logs=dict(type='bool', default=False),
paused=dict(type='bool'), paused=dict(type='bool'),
pull=dict(type='bool', default=False), pull=dict(type='bool', default=False),
purge_networks=dict(type='bool', default=False), purge_networks=dict(type='bool', default=False, removed_in_version='4.0.0', removed_from_collection='community.docker'),
recreate=dict(type='bool', default=False), recreate=dict(type='bool', default=False),
removal_wait_timeout=dict(type='float'), removal_wait_timeout=dict(type='float'),
restart=dict(type='bool', default=False), restart=dict(type='bool', default=False),

View File

@ -396,6 +396,8 @@ options:
stop this behavior by setting I(ignore_image) to C(true). stop this behavior by setting I(ignore_image) to C(true).
- "B(Warning:) This option is ignored if C(image: ignore) or C(*: ignore) is specified in the - "B(Warning:) This option is ignored if C(image: ignore) or C(*: ignore) is specified in the
I(comparisons) option." I(comparisons) option."
- "This option is deprecated since community.docker 3.2.0 and will be removed in community.docker 4.0.0.
Use C(image: ignore) in I(comparisons) instead of I(ignore_image=true)."
type: bool type: bool
default: false default: false
image: image:
@ -626,10 +628,10 @@ options:
description: description:
- List of networks the container belongs to. - List of networks the container belongs to.
- For examples of the data structure and usage see EXAMPLES below. - For examples of the data structure and usage see EXAMPLES below.
- To remove a container from one or more networks, use the I(purge_networks) option. - "To remove a container from one or more networks, use C(networks: strict) in the I(comparisons) option."
- If I(networks_cli_compatible) is set to C(false), this will not remove the default network if I(networks) is specified. - "If I(networks_cli_compatible) is set to C(false), this will not remove the default network if I(networks) is specified.
This is different from the behavior of C(docker run ...). You need to explicitly use I(purge_networks) to enforce This is different from the behavior of C(docker run ...). You need to explicitly use C(networks: strict) in I(comparisons)
the removal of the default network (and all other networks not explicitly mentioned in I(networks)) in that case. to enforce the removal of the default network (and all other networks not explicitly mentioned in I(networks)) in that case."
type: list type: list
elements: dict elements: dict
suboptions: suboptions:
@ -666,8 +668,8 @@ options:
via the I(networks) option, the module behaves differently than C(docker run --network): via the I(networks) option, the module behaves differently than C(docker run --network):
C(docker run --network other) will create a container with network C(other) attached, C(docker run --network other) will create a container with network C(other) attached,
but the default network not attached. This module with I(networks: {name: other}) will but the default network not attached. This module with I(networks: {name: other}) will
create a container with both C(default) and C(other) attached. If I(purge_networks) is create a container with both C(default) and C(other) attached. If C(networks: strict)
set to C(true), the C(default) network will be removed afterwards." or C(*: strict) is set in I(comparisons), the C(default) network will be removed afterwards."
type: bool type: bool
default: true default: true
oom_killer: oom_killer:
@ -745,16 +747,19 @@ options:
- ports - ports
pull: pull:
description: description:
- If true, always pull the latest version of an image. Otherwise, will only pull an image - If true, always pull the latest version of an image. Otherwise, will only pull an image
when missing. when missing.
- "B(Note:) images are only pulled when specified by name. If the image is specified - "B(Note:) images are only pulled when specified by name. If the image is specified
as a image ID (hash), it cannot be pulled." as a image ID (hash), it cannot be pulled."
type: bool type: bool
default: false default: false
purge_networks: purge_networks:
description: description:
- Remove the container from ALL networks not included in I(networks) parameter. - Remove the container from ALL networks not included in I(networks) parameter.
- Any default networks such as C(bridge), if not found in I(networks), will be removed as well. - Any default networks such as C(bridge), if not found in I(networks), will be removed as well.
- "This option is deprecated since community.docker 3.2.0 and will be removed in community.docker 4.0.0.
Use C(networks: strict) in I(comparisons) instead of I(purge_networks=true) and make sure that
I(networks) is specified. If you want to remove all networks, specify I(networks: [])."
type: bool type: bool
default: false default: false
read_only: read_only:
@ -824,8 +829,8 @@ options:
state. Use I(restart) to force a matching container to be stopped and restarted.' state. Use I(restart) to force a matching container to be stopped and restarted.'
- 'C(stopped) - Asserts that the container is first C(present), and then if the container is running moves it to a stopped - 'C(stopped) - Asserts that the container is first C(present), and then if the container is running moves it to a stopped
state.' state.'
- To control what will be taken into account when comparing configuration, see the I(comparisons) option. To avoid that the - "To control what will be taken into account when comparing configuration, see the I(comparisons) option. To avoid that the
image version will be taken into account, you can also use the I(ignore_image) option. image version will be taken into account, you can also use the C(image: ignore) in the I(comparisons) option."
- Use the I(recreate) option to always force re-creation of a matching container, even if it is running. - Use the I(recreate) option to always force re-creation of a matching container, even if it is running.
- If the container should be killed instead of stopped in case it needs to be stopped for recreation, or because I(state) is - If the container should be killed instead of stopped in case it needs to be stopped for recreation, or because I(state) is
C(stopped), please use the I(force_kill) option. Use I(keep_volumes) to retain anonymous volumes associated with a removed container. C(stopped), please use the I(force_kill) option. Use I(keep_volumes) to retain anonymous volumes associated with a removed container.
@ -1065,12 +1070,14 @@ EXAMPLES = '''
name: sleepy name: sleepy
networks: networks:
- name: TestingNet2 - name: TestingNet2
purge_networks: true comparisons:
networks: strict
- name: Remove container from all networks - name: Remove container from all networks
community.docker.docker_container: community.docker.docker_container:
name: sleepy name: sleepy
purge_networks: true comparisons:
networks: strict
- name: Start a container and use an env file - name: Start a container and use an env file
community.docker.docker_container: community.docker.docker_container:

View File

@ -179,7 +179,8 @@
command: '/bin/sh -c "sleep 10m"' command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}" name: "{{ cname }}"
state: started state: started
purge_networks: yes comparisons:
networks: strict
networks: networks:
- name: bridge - name: bridge
- name: "{{ nname_1 }}" - name: "{{ nname_1 }}"
@ -193,7 +194,8 @@
command: '/bin/sh -c "sleep 10m"' command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}" name: "{{ cname }}"
state: started state: started
purge_networks: yes comparisons:
networks: strict
networks: networks:
- name: "{{ nname_1 }}" - name: "{{ nname_1 }}"
- name: bridge - name: bridge
@ -217,7 +219,8 @@
command: '/bin/sh -c "sleep 10m"' command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}" name: "{{ cname }}"
state: started state: started
purge_networks: yes comparisons:
networks: strict
networks: networks:
- name: bridge - name: bridge
networks_cli_compatible: no networks_cli_compatible: no
@ -230,7 +233,8 @@
command: '/bin/sh -c "sleep 10m"' command: '/bin/sh -c "sleep 10m"'
name: "{{ cname }}" name: "{{ cname }}"
state: started state: started
purge_networks: yes comparisons:
networks: strict
networks: networks:
- name: bridge - name: bridge
- name: "{{ nname_2 }}" - name: "{{ nname_2 }}"
@ -350,7 +354,8 @@
state: started state: started
networks: [] networks: []
networks_cli_compatible: yes networks_cli_compatible: yes
purge_networks: yes comparisons:
networks: strict
force_kill: yes force_kill: yes
register: networks_5 register: networks_5
@ -387,7 +392,11 @@
name: "{{ cname }}" name: "{{ cname }}"
state: started state: started
networks_cli_compatible: yes networks_cli_compatible: yes
purge_networks: yes purge_networks: true
# To replace `purge_networks=true`, we have to specify `networks: []`:
# comparisons:
# networks: strict
# networks: []
force_kill: yes force_kill: yes
register: networks_8 register: networks_8

View File

@ -2205,7 +2205,8 @@
- name: ignore_image - name: ignore_image
docker_container: docker_container:
image: "{{ docker_test_image_hello_world }}" image: "{{ docker_test_image_hello_world }}"
ignore_image: yes comparisons:
image: ignore
name: "{{ cname }}" name: "{{ cname }}"
state: started state: started
register: ignore_image register: ignore_image
@ -2214,7 +2215,8 @@
- name: ignore_image (labels and env differ in image, image_comparison=current-image) - name: ignore_image (labels and env differ in image, image_comparison=current-image)
docker_container: docker_container:
image: "{{ docker_test_image_registry_nginx }}" image: "{{ docker_test_image_registry_nginx }}"
ignore_image: yes comparisons:
image: ignore
image_comparison: current-image image_comparison: current-image
name: "{{ cname }}" name: "{{ cname }}"
state: started state: started
@ -2224,7 +2226,8 @@
- name: ignore_image (labels and env differ in image, image_comparison=desired-image) - name: ignore_image (labels and env differ in image, image_comparison=desired-image)
docker_container: docker_container:
image: "{{ docker_test_image_registry_nginx }}" image: "{{ docker_test_image_registry_nginx }}"
ignore_image: yes comparisons:
image: ignore
image_comparison: desired-image image_comparison: desired-image
name: "{{ cname }}" name: "{{ cname }}"
state: started state: started